src/EventSubscriber/Import/EntityMapper/VOD/TitleChapters/EntityPreInitSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Import\EntityMapper\VOD\TitleChapters;
  3. use App\Application\EntityImportBundle\Event\EntityPreInitEvent;
  4. use App\Entity\SonataClassificationCategory;
  5. use App\Entity\VOD\Title;
  6. use App\Entity\VOD\TitleChapters;
  7. use App\Entity\VOD\TitlePreInitImport;
  8. use App\EventSubscriber\Import\EntityMapper\BasePreInitSubscriber;
  9. use App\Form\VOD\TitleChapterType;
  10. /**
  11.  * Class EntityPreInitSubscriber.
  12.  */
  13. class EntityPreInitSubscriber extends BasePreInitSubscriber
  14. {
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             EntityPreInitEvent::class => 'onEntityPreInit',
  19.         ];
  20.     }
  21.     public function onEntityPreInit(EntityPreInitEvent $event): void
  22.     {
  23.         if (TitlePreInitImport::class !== $event->getTargetClassName()) {
  24.             return;
  25.         }
  26.         $configuredProperties $event->getConfigurationProperties();
  27.         $extendedNavCodeProp $this->filterConfiguredProperty('extendedNavCode'$configuredPropertiestrue);
  28.         $chapterTypeSlugProp $this->filterConfiguredProperty('chapterType'$configuredPropertiestrue);
  29.         $offsetProp $this->filterConfiguredProperty('offset'$configuredPropertiestrue);
  30.         $endOffsetProp $this->filterConfiguredProperty('endOffset'$configuredPropertiestrue);
  31.         if (!isset($extendedNavCodeProp$chapterTypeSlugProp$offsetProp)) {
  32.             $event->setSkipRowProcessing(true);
  33.             return;
  34.         }
  35.         $extendedNavCode $this->getValue($extendedNavCodeProp$event->getData());
  36.         $chapterTypeSlug $this->getValue($chapterTypeSlugProp$event->getData());
  37.         $chapterType $this->getChapterTypeObj($chapterTypeSlug);
  38.         $offset $this->getValue($offsetProp$event->getData());
  39.         $endOffset $this->getValue($endOffsetProp$event->getData());
  40.         if (!$extendedNavCode || !$chapterType || !$offset) {
  41.             $event->setSkipRowProcessing(true);
  42.             return;
  43.         }
  44.         $titleRepo $this->getEntityManager()->getRepository(Title::class);
  45.         $entity $titleRepo->findOneBy(['extendedNavCode' => $extendedNavCode]);
  46.         if ($entity instanceof Title) {
  47.             //Update Vod title "modified" date
  48.             $entity->setUpdatedAt(new \DateTime());
  49.             /** @var TitleChapters $chapter */
  50.             foreach ($entity->getChapters() as $chapter) {
  51.                 if ($chapter->getType() === $chapterType) {
  52.                     // overwriting the existing titleChapter
  53.                     $chapter->setOffset($offset);
  54.                     $chapter->setEndOffset($endOffset);
  55.                     $event->setEntity($entity);
  56.                     return;
  57.                 }
  58.             }
  59.             // create a new titleChapter
  60.             $chapter = new TitleChapters();
  61.             $chapter->setTitle($entity);
  62.             $chapter->setType($chapterType);
  63.             $chapter->setOffset($offset);
  64.             $chapter->setEndOffset($endOffset);
  65.             $entity->addChapters($chapter);
  66.             $event->setEntity($entity);
  67.         }
  68.     }
  69.     /**
  70.      * @return TitleChapterType|null
  71.      */
  72.     private function getChapterTypeObj(string $chapterSlug): ?SonataClassificationCategory
  73.     {
  74.         $repo $this->getEntityManager()->getRepository(SonataClassificationCategory::class);
  75.         $chapterTypes $repo->findBy([
  76.             'context' => 'chapters',
  77.         ]);
  78.         foreach ($chapterTypes as $chapterType) {
  79.             if ($chapterType->getSlug() === $chapterSlug) {
  80.                 return $chapterType;
  81.             }
  82.         }
  83.         return null;
  84.     }
  85. }