feat: add CachedQRCodeProvider
This commit is contained in:
12
src/Service/CacheableQRCodeGeneratorInterface.php
Normal file
12
src/Service/CacheableQRCodeGeneratorInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\QRCode\QRCode;
|
||||
|
||||
interface CacheableQRCodeGeneratorInterface extends QRCodeGeneratorInterface
|
||||
{
|
||||
// We can't calculate cache key directly from QRCode
|
||||
public function getCacheKey(QRCode $entity): string;
|
||||
}
|
||||
39
src/Service/CachedQRCodeGenerator.php
Normal file
39
src/Service/CachedQRCodeGenerator.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\QRCode\QRCode;
|
||||
use Exception;
|
||||
use Psr\Cache\InvalidArgumentException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
|
||||
final readonly class CachedQRCodeGenerator implements QRCodeGeneratorInterface
|
||||
{
|
||||
private \DateInterval $cacheDuration;
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(private CacheableQRCodeGeneratorInterface $innerGenerator, private CacheInterface $cache, private LoggerInterface $logger, string $cacheDuration)
|
||||
{
|
||||
$this->cacheDuration = new \DateInterval($cacheDuration);
|
||||
}
|
||||
|
||||
public function generateQRCodeFromEntity(QRCode $entity): string
|
||||
{
|
||||
$key = $this->innerGenerator->getCacheKey($entity);
|
||||
return $this->cache->get($key, function(ItemInterface $c) use ($entity) {
|
||||
$this->logger->debug("cache miss for key " . $c->getKey(), [
|
||||
'entity' => $entity,
|
||||
]);
|
||||
|
||||
$c->expiresAfter($this->cacheDuration);
|
||||
|
||||
return $this->innerGenerator->generateQRCodeFromEntity($entity);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,12 @@ declare(strict_types=1);
|
||||
namespace App\Service\Remote;
|
||||
|
||||
use App\Entity\QRCode\QRCode;
|
||||
use App\Service\QRCodeGeneratorInterface;
|
||||
use App\Service\CacheableQRCodeGeneratorInterface;
|
||||
use App\Service\Remote\Edge\QRCodeEntityConverter;
|
||||
use App\Service\Remote\Exception\AuthorizeException;
|
||||
use App\Service\Remote\Exception\UsetrenoQRCodeException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class UsetrenoQRCodeProvider implements QRCodeGeneratorInterface
|
||||
class UsetrenoQRCodeProvider implements CacheableQRCodeGeneratorInterface
|
||||
{
|
||||
const QRCODE_API = 'https://topapi.top-test.cz/chameleon/api/v1/qr-code/create-for-bank-account-payment';
|
||||
const QRCODE_METHOD = 'POST';
|
||||
@@ -86,4 +85,14 @@ class UsetrenoQRCodeProvider implements QRCodeGeneratorInterface
|
||||
|
||||
return $data->data->base64Data;
|
||||
}
|
||||
|
||||
public function getCacheKey(QRCode $entity): string
|
||||
{
|
||||
$edgeEntity = $this->codeEntityConverter->convert($entity);
|
||||
$encodedEntity = json_encode($edgeEntity);
|
||||
if ($encodedEntity === false) {
|
||||
throw new \RuntimeException("Can't serialize edge entity");
|
||||
}
|
||||
return base64_encode($encodedEntity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user