src/Serializer/InvoiceNormalizer.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Serializer;
  4. use App\Service\EncryptorDataUtils;
  5. use App\Service\InvoiceFilesService;
  6. use App\Utils\InvoiceUtils;
  7. use Evo\Infrastructure\MappingORM\Invoice;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  11. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  12. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  13. final class InvoiceNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  14. {
  15.     use NormalizerAwareTrait;
  16.     private const ALREADY_CALLED 'INVOICE_NORMALIZER_ALREADY_CALLED';
  17.     private InvoiceUtils $invoiceUtils;
  18.     private UrlGeneratorInterface $router;
  19.     private EncryptorDataUtils $encryptor;
  20.     private InvoiceFilesService $invoiceFilesService;
  21.     private Security $security;
  22.     public function __construct(
  23.         EncryptorDataUtils $encryptor,
  24.         InvoiceUtils $invoiceUtils,
  25.         UrlGeneratorInterface $router,
  26.         InvoiceFilesService $invoiceFilesService,
  27.         Security $security
  28.     ) {
  29.         $this->encryptor $encryptor;
  30.         $this->invoiceUtils $invoiceUtils;
  31.         $this->router $router;
  32.         $this->invoiceFilesService $invoiceFilesService;
  33.         $this->security $security;
  34.     }
  35.     public function supportsNormalization($data$format null, array $context = []): bool
  36.     {
  37.         if (isset($context[self::ALREADY_CALLED])) {
  38.             return false;
  39.         }
  40.         return $data instanceof Invoice;
  41.     }
  42.     public function normalize($object$format null, array $context = [])
  43.     {
  44.         $context[self::ALREADY_CALLED] = true;
  45.         $normalizedData $this->normalizer->normalize($object$format$context);
  46.         if (is_array($normalizedData) && !in_array('read_letter_forward'$context['groups'], true)) {
  47.             $isWaiting $this->invoiceUtils->isWaiting($object);
  48.             $normalizedData['isWaiting'] = $isWaiting;
  49.             $normalizedData['numberWithPrefix'] = 'FA-'.$object->getNumber();
  50.             $url $this->router->generate(
  51.                 'app_invoice_view',
  52.                 ['id' => $this->encryptor->encrypt($object->getId())],
  53.                 UrlGeneratorInterface::ABSOLUTE_URL
  54.             );
  55.             if (!empty($context['groups']) &&
  56.                 (in_array('read_zappier_invoice'$context['groups'], true)
  57.                 || in_array('admin:invoice:read'$context['groups'], true))
  58.             ) {
  59.                 $normalizedData['viewInvoicePath'] = $url.'/'.($object->getOrganization() ? $object->getOrganization()->getInvoiceToken() : '');
  60.             }
  61.             $normalizedData['path'] = $url;
  62.             $user $this->security->getUser();
  63.             if (null !== $user) {
  64.                 if (isset($context['groups']) && in_array('read_zappier_invoice'$context['groups'], true)) {
  65.                     unset($normalizedData['path'], $normalizedData['isWaiting']);
  66.                 } else {
  67.                     $normalizedData['invoiceDownloadLink'] = $this->invoiceFilesService->downloadInvoice($object->getPath(), true);
  68.                 }
  69.             }
  70.         }
  71.         return $normalizedData;
  72.     }
  73. }