src/Serializer/LetterNormalizer.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Serializer;
  4. use App\Enum\LetterTypeEnum;
  5. use App\Service\EncryptorDataUtils;
  6. use Evo\Infrastructure\MappingORM\Letter;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  9. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  10. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. final class LetterNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  13. {
  14.     use NormalizerAwareTrait;
  15.     private const ALREADY_CALLED 'LETTER_NORMALIZER_ALREADY_CALLED';
  16.     private UrlGeneratorInterface $router;
  17.     private EncryptorDataUtils $encryptor;
  18.     private TranslatorInterface $translator;
  19.     public function __construct(
  20.         EncryptorDataUtils $encryptor,
  21.         UrlGeneratorInterface $router,
  22.         TranslatorInterface $translator
  23.     ) {
  24.         $this->encryptor $encryptor;
  25.         $this->router $router;
  26.         $this->translator $translator;
  27.     }
  28.     public function supportsNormalization($data$format null, array $context = []): bool
  29.     {
  30.         if (isset($context[self::ALREADY_CALLED])) {
  31.             return false;
  32.         }
  33.         return $data instanceof Letter;
  34.     }
  35.     public function normalize($object$format null, array $context = [])
  36.     {
  37.         $context[self::ALREADY_CALLED] = true;
  38.         $normalizedData $this->normalizer->normalize($object$format$context);
  39.         if (is_array($normalizedData)) {
  40.             $id $this->encryptor->encrypt($object->getId());
  41.             $urlFront $this->router->generate(
  42.                 'app_letter_view',
  43.                 ['id' => $id'type' => 'recto'],
  44.                 UrlGeneratorInterface::ABSOLUTE_URL
  45.             );
  46.             $urlFront str_replace('http://''https://'$urlFront);
  47.             $urlBack $this->router->generate(
  48.                 'app_letter_view',
  49.                 ['id' => $id'type' => 'verso'],
  50.                 UrlGeneratorInterface::ABSOLUTE_URL
  51.             );
  52.             $urlBack str_replace('http://''https://'$urlBack);
  53.             $urlContent $this->router->generate(
  54.                 'app_letter_view',
  55.                 ['id' => $id'type' => 'content'],
  56.                 UrlGeneratorInterface::ABSOLUTE_URL
  57.             );
  58.             $urlContent str_replace('http://''https://'$urlContent);
  59.             if (isset($normalizedData['envelopeFrontPathAWS'])) {
  60.                 $normalizedData['envelopeFrontUrl'] = $urlFront;
  61.             }
  62.             if (isset($normalizedData['envelopeBackPathAWS'])) {
  63.                 $normalizedData['envelopeBackUrl'] = $urlBack;
  64.             }
  65.             if (isset($normalizedData['contentPathAWS'])) {
  66.                 $normalizedData['contentUrl'] = $urlContent;
  67.             }
  68.             if (isset($context['groups']) && in_array('read_zappier_letter'$context['groups'], true)) {
  69.                 unset(
  70.                     $normalizedData['contentPathAWS'],
  71.                     $normalizedData['envelopeFrontPathAWS'],
  72.                     $normalizedData['envelopeBackPathAWS']
  73.                 );
  74.                 $normalizedData['type'] = $this->translator->trans(LetterTypeEnum::getReadableValue($normalizedData['type']));
  75.             }
  76.         }
  77.         return $normalizedData;
  78.     }
  79. }