<?phpdeclare(strict_types=1);namespace Hitso\Bundle\CatalogBundle\EventListener;use Doctrine\ORM\EntityManagerInterface;use Doctrine\ORM\Event\LifecycleEventArgs;use Hitso\Bundle\CatalogBundle\Entity\Product;use Hitso\Bundle\CatalogBundle\Entity\Section\ProductPageSection;use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;use Hitso\Bundle\CommonBundle\Entity\ChangeLog;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;/** * Class ChangeLogEventListener * * @package Hitso\Bundle\CatalogBundle\EventListener */class ChangeLogEventListener implements EventSubscriberInterface{ /** * @var ManagerCollection */ protected $managerCollection; /** * @var TokenStorageInterface */ protected $tokenStorage; /** * @var EntityAuthorizationChecker */ protected $entityAuthorizationChecker; /** * @var EntityManagerInterface */ protected $doctrine; /** * ProductChangeLogEventListener constructor. * * @param ManagerCollection $managerCollection */ public function __construct( EntityManagerInterface $doctrine, ManagerCollection $managerCollection, TokenStorageInterface $tokenStorage, EntityAuthorizationChecker $entityAuthorizationChecker ) { $this->doctrine = $doctrine; $this->managerCollection = $managerCollection; $this->tokenStorage = $tokenStorage; $this->entityAuthorizationChecker = $entityAuthorizationChecker; } /** * @return array */ public static function getSubscribedEvents() { return [ 'prePersist', ]; } /** * @param LifecycleEventArgs $eventArgs * * @throws \Exception */ public function prePersist(LifecycleEventArgs $eventArgs): void { /** * @var ChangeLog */ $entity = $eventArgs->getEntity(); if ($entity instanceof ChangeLog && $entity->getObjectClass() === Product::class) { /** * @var Product */ $product = $eventArgs->getEntityManager() ->getRepository(Product::class) ->find((int) $entity->getObjectId()); if ($product) { $data = $entity->getData(); $this->prepareAttributes($product, $data); $this->prepareSections($product, $data); $entity->setData($data); } } } /** * @param Product $product * @param array|null $data */ private function prepareAttributes( Product $product, ?array &$data ) { $attributes = $product->getAttributes(); $data['attributes'] = []; foreach ($attributes as $productAttribute) { if ($productAttribute->getId()) { $data['attributes'][$productAttribute->getId()] = $productAttribute; } } } /** * @param Product $product * @param array|null $data */ private function prepareSections( Product $product, ?array &$data ) { $sections = $product->getSections(); $data['sections'] = []; foreach ($sections as $section) { if ($section instanceof ProductPageSection) { $data['sections'][$section->getId()] = $section; } } }}