src/EventSubscriber/Import/EntityMapper/ProductTag/EntityPreInitSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Import\EntityMapper\ProductTag;
  3. use App\Application\EntityImportBundle\Event\EntityPreInitEvent;
  4. use App\Entity\SonataClassificationCategory;
  5. use App\Entity\Product;
  6. use App\Entity\ProductPreInitImport;
  7. use App\Entity\VOD\Title;
  8. use App\EventSubscriber\Import\EntityMapper\BasePreInitSubscriber;
  9. class EntityPreInitSubscriber extends BasePreInitSubscriber
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             EntityPreInitEvent::class => 'onEntityPreInit',
  15.         ];
  16.     }
  17.     public function onEntityPreInit(EntityPreInitEvent $event): void
  18.     {
  19.         if (ProductPreInitImport::class !== $event->getTargetClassName()) {
  20.             return;
  21.         }
  22.         $configuredProperties $event->getConfigurationProperties();
  23.         $titleCodeProperty $this->filterConfiguredProperty('titleCode'$configuredPropertiestrue);
  24.         $tagsProperty $this->filterConfiguredProperty('tag'$configuredPropertiestrue);
  25.         if (!isset($titleCodeProperty$tagsProperty)) {
  26.             $event->setSkipRowProcessing(true);
  27.             return;
  28.         }
  29.         $titleCode $this->getValue($titleCodeProperty$event->getData());
  30.         $tags explode(','$this->getValue($tagsProperty$event->getData()));
  31.         if (!$titleCode || !$tags) {
  32.             $event->setSkipRowProcessing(true);
  33.             return;
  34.         }
  35.         $productRepo $this->getEntityManager()->getRepository(Product::class);
  36.         $entity $productRepo->findOneByNavCode($titleCode);
  37.         if ($entity instanceof Product) {
  38.             $tagRepo $this->getEntityManager()->getRepository(SonataClassificationCategory::class);
  39.             $tags $tagRepo->findBy([
  40.                 'context' => 'tag',
  41.                 'slug' => $tags,
  42.             ]);
  43.             $isProductUpdated false;
  44.             foreach ($tags as $tag) {
  45.                 if (!$this->isTagExists($entity$tag)) {
  46.                     $entity->getTags()->add($tag);
  47.                     $isProductUpdated true;
  48.                 }
  49.             }
  50.             if (!$isProductUpdated) {
  51.                 $event->setSkipRowProcessing(true);
  52.                 return;
  53.             }
  54.             $this->updateVodTitles($entity);
  55.         }
  56.         $event->setEntity($entity);
  57.     }
  58.     private function isTagExists(Product $entitySonataClassificationCategory $tag): bool
  59.     {
  60.         return (bool) $entity->getTags()->filter(fn (SonataClassificationCategory $category) => $category === $tag)->toArray();
  61.     }
  62.     public function updateVodTitles(Product $entity): void
  63.     {
  64.         /** @var Title $vodTitle */
  65.         foreach ($entity->getVodTitles() as $vodTitle) {
  66.             // In the definition of the field in DB there is no configuration to use CURRENT_TIMESTAMP on every update
  67.             $vodTitle->setUpdatedAt(new \DateTime());
  68.             $this->getEntityManager()->persist($vodTitle);
  69.         }
  70.         $this->getEntityManager()->flush();
  71.     }
  72. }