src/Serializer/DocumentNormalizer.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Serializer;
  4. use App\Enum\DocumentTypeOrganizationEnum;
  5. use App\Service\EncryptorDataUtils;
  6. use Evo\Infrastructure\MappingORM\Document;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  11. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  12. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  13. final class DocumentNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  14. {
  15.     use NormalizerAwareTrait;
  16.     private const ALREADY_CALLED 'DOCUMENT_NORMALIZER_ALREADY_CALLED';
  17.     private ?Request $request null;
  18.     private UrlGeneratorInterface $router;
  19.     private EncryptorDataUtils $encryptor;
  20.     public function __construct(
  21.         RequestStack $requestStack,
  22.         EncryptorDataUtils $encryptor,
  23.         UrlGeneratorInterface $router
  24.     ) {
  25.         $this->encryptor $encryptor;
  26.         $this->request $requestStack->getMainRequest();
  27.         $this->router $router;
  28.     }
  29.     public function supportsNormalization($data$format null, array $context = []): bool
  30.     {
  31.         if (isset($context[self::ALREADY_CALLED])) {
  32.             return false;
  33.         }
  34.         return $data instanceof Document;
  35.     }
  36.     public function normalize($object$format null, array $context = [])
  37.     {
  38.         $context[self::ALREADY_CALLED] = true;
  39.         $normalizedData $this->normalizer->normalize($object$format$context);
  40.         $urlAPI $this->router->generate(
  41.             'app_document_view',
  42.             ['id' => $this->encryptor->encrypt($object->getId())]
  43.         );
  44.         $url $this->request->getSchemeAndHttpHost().$urlAPI;
  45.         if (is_array($normalizedData)) {
  46.             $url str_replace('http://''https://'$url);
  47.             $normalizedData['url'] = $url;
  48.             $normalizedData['extension'] = null;
  49.             if (null !== $object->getAwsPath()) {
  50.                 $_data explode('.'$object->getAwsPath());
  51.                 $normalizedData['extension'] = end($_data);
  52.             }
  53.             if (in_array($normalizedData['type'], DocumentTypeOrganizationEnum::DOMICILIATIONtrue)) {
  54.                 $normalizedData['category'] = 'DOMICILIATION';
  55.             } elseif (in_array($normalizedData['type'], DocumentTypeOrganizationEnum::ORGANIZATIONtrue)) {
  56.                 $normalizedData['category'] = 'ORGANIZATION';
  57.             } else {
  58.                 $normalizedData['category'] = 'OTHER';
  59.             }
  60.         }
  61.         return $normalizedData;
  62.     }
  63. }