symfony_example_app/src/Service/DTO/QRCodeEntityConverter.php

60 lines
2.3 KiB
PHP

<?php
namespace App\Service\DTO;
use App\Entity\DTO\QRCode\QRCode;
use App\Entity\DTO\QRCode\QRCodeMoney;
use App\Entity\DTO\QRCode\QRCodePaymentIdentification;
use App\Entity\DTO\QRCode\QRCodeQROptions;
use Nubium\Exception\ThisShouldNeverHappenException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final readonly class QRCodeEntityConverter
{
public function __construct(private ValidatorInterface $validator) {
}
public function convert(\App\Entity\Input\QRCode\QRCode $code): QRCode {
if (count($this->validator->validate($code)) !== 0) {
throw new \InvalidArgumentException("QRCode entity is not valid");
}
return new QRCode(
$code->getIban() ?? throw new ThisShouldNeverHappenException("iban not set"),
\DateTimeImmutable::createFromMutable($code->getDueDate() ?? throw new ThisShouldNeverHappenException("due date not set")),
$code->getMessage() ?? throw new ThisShouldNeverHappenException("message not set"),
$this->convertMoney($code->getMoney() ?? throw new ThisShouldNeverHappenException("money not set")),
$this->convertCodeOptions($code->getCodeQROptions() ?? throw new ThisShouldNeverHappenException("codeQROptions not set")),
$this->convertIdentification($code->getPaymentIdentification())
);
}
protected function convertMoney(\App\Entity\Input\QRCode\QRCodeMoney $money): QRCodeMoney
{
return new QRCodeMoney(
$money->getAmount() ?? throw new ThisShouldNeverHappenException("amount not set"),
$money->getCurrency() ?? throw new ThisShouldNeverHappenException("currency not set")
);
}
protected function convertCodeOptions(\App\Entity\Input\QRCode\QRCodeQROptions $options): QRCodeQROptions
{
return new QRCodeQROptions(
$options->getScale(),
$options->getMargin()
);
}
protected function convertIdentification(?\App\Entity\Input\QRCode\QRCodePaymentIdentification $ident): ?QRCodePaymentIdentification {
return match ($ident) {
null => null,
default => new QRCodePaymentIdentification(
$ident->getVariableSymbol(),
$ident->getSpecificSymbol(),
$ident->getConstantSymbol()
)
};
}
}