src/Serializer/StoreNormalizer.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Serializer;
  3. use App\Traits\SentryNotifyTrait;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Evo\Infrastructure\MappingORM\Product;
  6. use Evo\Infrastructure\MappingORM\Store;
  7. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  8. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  9. use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
  10. use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
  11. class StoreNormalizer implements ContextAwareNormalizerInterfaceNormalizerAwareInterface
  12. {
  13.     use NormalizerAwareTrait;
  14.     use SentryNotifyTrait;
  15.     private const ALREADY_CALLED 'STORE_NORMALIZER_ALREADY_CALLED';
  16.     private EntityManagerInterface $em;
  17.     public function __construct(EntityManagerInterface $em)
  18.     {
  19.         $this->em $em;
  20.     }
  21.     /**
  22.      * @param Store $object
  23.      *
  24.      * @throws ExceptionInterface
  25.      */
  26.     public function normalize($object$format null, array $context = [])
  27.     {
  28.         $context[self::ALREADY_CALLED] = true;
  29.         $normalizedData $this->normalizer->normalize($object$format$context);
  30.         try {
  31.             if (($object instanceof Store) && is_array($normalizedData)) {
  32.                 if (null !== $object->getName()) {
  33.                     $product $this->em->getRepository(Product::class)->findStoreAdditionForStoreName($object->getName());
  34.                     if ($product) {
  35.                         $normalizedData['price'] = $product->getPriceWithoutTax();
  36.                     }
  37.                 }
  38.             }
  39.         } catch (\Exception $e) {
  40.             $this->captureSentryException($e);
  41.         }
  42.         return $normalizedData;
  43.     }
  44.     public function supportsNormalization($data$format null, array $context = []): bool
  45.     {
  46.         if (isset($context[self::ALREADY_CALLED])) {
  47.             return false;
  48.         }
  49.         return $data instanceof Store;
  50.     }
  51. }