feat: add stub implementation for image

QRCodeFromEntity

fix: base64 StubQRCodeGenerator
This commit is contained in:
Ondrej Vlach 2024-01-17 11:49:52 +01:00
parent 9e96c007bd
commit 64a07b232a
Signed by: ovlach
GPG Key ID: 4FF1A23B4914DE70
8 changed files with 75 additions and 8 deletions

BIN
assets/images/wip.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -5,3 +5,7 @@ body {
.header {
background-color: skyblue;
}
.header-another-code {
font-size: 2rem;
}

View File

@ -27,8 +27,13 @@ services:
arguments:
$available_currencies: '%app.currencies%'
App\Service\StubQRCodeGenerator:
arguments:
$imagePath: '%kernel.project_dir%/assets/images/wip.png'
App\Service\CurrencyListerInterface: '@App\Service\StaticCurrencyLister'
App\Service\QRCodeQROptionsProviderInterface: '@App\Service\QRCodeQROptionsDefaultProvider'
App\Service\QRCodeGeneratorInterface: '@App\Service\StubQRCodeGenerator'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

View File

@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\QRCode\QRCode;
use App\Form\Type\QRCodeType;
use App\Service\QRCodeGeneratorInterface;
use App\Service\QRCodeQROptionsProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -11,14 +12,16 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class IndexController extends AbstractController {
public function __construct(private readonly QRCodeQROptionsProviderInterface $qrCodeQROptionsFactory) {}
#[Route('/', name: 'homepage')]
public function indexAction(Request $request, QRCodeQROptionsProviderInterface $qrCodeQROptionsFactory): Response
public function indexAction(
Request $request,
QRCodeGeneratorInterface $qrCodeGenerator,
): Response
{
$qrCode = new QRCode(
codeQROptions: $qrCodeQROptionsFactory->getDefault()
);
$qrCodeImage = null;
$qrCode = $this->createQrCodeEntity();
$form = $this->createForm(QRCodeType::class, $qrCode);
@ -26,13 +29,24 @@ class IndexController extends AbstractController {
$form->submit($request->request->all($form->getName()));
if ($form->isSubmitted() && $form->isValid()) {
// TODO: call service to generate
print_r($qrCode);
/* Puvodne jsem premyslel nad redirectem a ulozenim obrazku
do session, nicmene u takovehleho typu aplikace me to prijde naopak kontraproduktivni.
a zadani o tom mlci. Navic v pripade ze aplikace bude provozovana ve vice instancich
by se musela resit memcache, redis... */
$qrCodeImage = $qrCodeGenerator->generateQRCodeFromEntity($qrCode);
$form = $this->createForm(QRCodeType::class, $this->createQrCodeEntity());
}
}
return $this->render('index/homepage.html.twig', [
'form' => $form
'form' => $form,
'qrCodeImage' => $qrCodeImage
]);
}
private function createQrCodeEntity(): QRCode {
return new QRCode(
codeQROptions: $this->qrCodeQROptionsFactory->getDefault()
);
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\QRCode\QRCode;
interface QRCodeGeneratorInterface
{
/**
* Generates QR code from entity
* @param QRCode $entity
* @return string base64 encoded PNG
*/
public function generateQRCodeFromEntity(QRCode $entity): string;
}

View File

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\QRCode\QRCode;
readonly final class StubQRCodeGenerator implements QRCodeGeneratorInterface
{
public function __construct(private string $imagePath) {}
public function generateQRCodeFromEntity(QRCode $entity): string
{
$content = file_get_contents($this->imagePath);
return match ($content) {
false => throw new \InvalidArgumentException('Image path is invalid'),
default => base64_encode($content)
};
}
}

View File

@ -2,6 +2,12 @@
{% block body %}
<div class="container">
{% if qrCodeImage %}
<div class="d-flex p-3 justify-content-center qr-code-image">
<img src="data:image/png;base64,{{ qrCodeImage }}" alt="QR code" />
</div>
<h2 class="header-another-code">{{ "You can generate another QR code here" | trans }}:</h2>
{% endif %}
{{ form_start(form) }}
<div class="mb-3 col-auto">
{{ form_label(form.iban) }}

View File

@ -8,3 +8,4 @@ Specific symbol: "Specifický symbol"
Generate qr code: "Vygenerovat QR kód"
Iban: "IBAN"
QR code generator: "Generátor bankovních QR kódů"
You can generate another QR code here: "Další QR kód si můžete vygenerovat zde"