feat: better data wf
This commit is contained in:
58
src/Service/DTO/QRCodeEntityConverter.php
Normal file
58
src/Service/DTO/QRCodeEntityConverter.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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 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 \InvalidArgumentException("iban not set"),
|
||||
\DateTimeImmutable::createFromMutable($code->getDueDate() ?? throw new \InvalidArgumentException("due date not set")),
|
||||
$code->getMessage() ?? throw new \InvalidArgumentException("message not set"),
|
||||
$this->convertMoney($code->getMoney() ?? throw new \InvalidArgumentException("money not set")),
|
||||
$this->convertCodeOptions($code->getCodeQROptions() ?? throw new \InvalidArgumentException("codeQROptions not set")),
|
||||
$this->convertIdentification($code->getPaymentIdentification())
|
||||
);
|
||||
}
|
||||
|
||||
protected function convertMoney(\App\Entity\Input\QRCode\QRCodeMoney $money): QRCodeMoney
|
||||
{
|
||||
return new QRCodeMoney(
|
||||
$money->getAmount() ?? throw new \InvalidArgumentException("amount not set"),
|
||||
$money->getCurrency() ?? throw new \InvalidArgumentException("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()
|
||||
)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user