src/Hitso/Bundle/CatalogBundle/EventListener/ChangeLogEventListener.php line 76

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CatalogBundle\EventListener;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Doctrine\ORM\Event\LifecycleEventArgs;
  6. use Hitso\Bundle\CatalogBundle\Entity\Product;
  7. use Hitso\Bundle\CatalogBundle\Entity\Section\ProductPageSection;
  8. use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
  9. use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;
  10. use Hitso\Bundle\CommonBundle\Entity\ChangeLog;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. /**
  14.  * Class ChangeLogEventListener
  15.  *
  16.  * @package Hitso\Bundle\CatalogBundle\EventListener
  17.  */
  18. class ChangeLogEventListener implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var ManagerCollection
  22.      */
  23.     protected $managerCollection;
  24.     /**
  25.      * @var TokenStorageInterface
  26.      */
  27.     protected $tokenStorage;
  28.     /**
  29.      * @var EntityAuthorizationChecker
  30.      */
  31.     protected $entityAuthorizationChecker;
  32.     /**
  33.      * @var EntityManagerInterface
  34.      */
  35.     protected $doctrine;
  36.     /**
  37.      * ProductChangeLogEventListener constructor.
  38.      *
  39.      * @param ManagerCollection $managerCollection
  40.      */
  41.     public function __construct(
  42.         EntityManagerInterface $doctrine,
  43.         ManagerCollection $managerCollection,
  44.         TokenStorageInterface $tokenStorage,
  45.         EntityAuthorizationChecker $entityAuthorizationChecker
  46.     ) {
  47.         $this->doctrine                   $doctrine;
  48.         $this->managerCollection          $managerCollection;
  49.         $this->tokenStorage               $tokenStorage;
  50.         $this->entityAuthorizationChecker $entityAuthorizationChecker;
  51.     }
  52.     /**
  53.      * @return array
  54.      */
  55.     public static function getSubscribedEvents()
  56.     {
  57.         return [
  58.             'prePersist',
  59.         ];
  60.     }
  61.     /**
  62.      * @param LifecycleEventArgs $eventArgs
  63.      *
  64.      * @throws \Exception
  65.      */
  66.     public function prePersist(LifecycleEventArgs $eventArgs): void
  67.     {
  68.         /**
  69.          * @var ChangeLog
  70.          */
  71.         $entity $eventArgs->getEntity();
  72.         if ($entity instanceof ChangeLog && $entity->getObjectClass() === Product::class) {
  73.             /**
  74.              * @var Product
  75.              */
  76.             $product $eventArgs->getEntityManager()
  77.                 ->getRepository(Product::class)
  78.                 ->find((int) $entity->getObjectId());
  79.             if ($product) {
  80.                 $data $entity->getData();
  81.                 $this->prepareAttributes($product$data);
  82.                 $this->prepareSections($product$data);
  83.                 $entity->setData($data);
  84.             }
  85.         }
  86.     }
  87.     /**
  88.      * @param Product    $product
  89.      * @param array|null $data
  90.      */
  91.     private function prepareAttributes(
  92.         Product $product,
  93.         ?array &$data
  94.     ) {
  95.         $attributes $product->getAttributes();
  96.         $data['attributes'] = [];
  97.         foreach ($attributes as $productAttribute) {
  98.             if ($productAttribute->getId()) {
  99.                 $data['attributes'][$productAttribute->getId()] = $productAttribute;
  100.             }
  101.         }
  102.     }
  103.     /**
  104.      * @param Product    $product
  105.      * @param array|null $data
  106.      */
  107.     private function prepareSections(
  108.         Product $product,
  109.         ?array &$data
  110.     ) {
  111.         $sections $product->getSections();
  112.         $data['sections'] = [];
  113.         foreach ($sections as $section) {
  114.             if ($section instanceof ProductPageSection) {
  115.                 $data['sections'][$section->getId()] = $section;
  116.             }
  117.         }
  118.     }
  119. }