src/EventSubscriber/Import/EntityMapper/VOD/SVODOperatorDates/EntityPreInitSubscriber.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Import\EntityMapper\VOD\SVODOperatorDates;
  3. use App\Application\EntityImportBundle\Event\EntityPreInitEvent;
  4. use App\Entity\VOD\Operator;
  5. use App\Entity\VOD\Title;
  6. use App\Entity\VOD\TitleSvodOperatorDates;
  7. use App\EventSubscriber\Import\EntityMapper\BasePreInitSubscriber;
  8. use App\Repository\VOD\OperatorRepository;
  9. use App\Repository\VOD\SvodOperatorDatesRepository;
  10. use App\Repository\VOD\TitleRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. /**
  13.  * Class EntityPreInitSubscriber.
  14.  */
  15. class EntityPreInitSubscriber extends BasePreInitSubscriber
  16. {
  17.     /**
  18.      * The instance of the title svod operator date repository.
  19.      *
  20.      * @var \App\Repository\VOD\SvodOperatorDatesRepository
  21.      */
  22.     private $svodOperatorRepository;
  23.     /**
  24.      * The instance of the operator repository.
  25.      *
  26.      * @var \App\Repository\VOD\OperatorRepository
  27.      */
  28.     private $operatorRepository;
  29.     /**
  30.      * The instance of the vod title repository.
  31.      *
  32.      * @var \App\Repository\VOD\TitleRepository
  33.      */
  34.     private $titleRepository;
  35.     /**
  36.      * EntityPreInitSubscriber constructor.
  37.      *
  38.      * @param \Doctrine\ORM\EntityManagerInterface $entityManager
  39.      *   The entity manager
  40.      * @param \App\Repository\VOD\SvodOperatorDatesRepository $svodOperatorRepository
  41.      *   The instance of the svod operator dates repository
  42.      * @param \App\Repository\VOD\OperatorRepository $operatorRepository
  43.      *   The instance of the operator repository
  44.      * @param \App\Repository\VOD\TitleRepository $titleRepository
  45.      *   The instance of the vod title repository
  46.      */
  47.     public function __construct(
  48.         EntityManagerInterface $entityManager,
  49.         SvodOperatorDatesRepository $svodOperatorRepository,
  50.         OperatorRepository $operatorRepository,
  51.         TitleRepository $titleRepository
  52.     ) {
  53.         parent::__construct($entityManager);
  54.         $this->svodOperatorRepository $svodOperatorRepository;
  55.         $this->operatorRepository $operatorRepository;
  56.         $this->titleRepository $titleRepository;
  57.     }
  58.     public static function getSubscribedEvents()
  59.     {
  60.         return [
  61.             EntityPreInitEvent::class => 'onEntityPreInit',
  62.         ];
  63.     }
  64.     /**
  65.      * Public callback for the pre-init event.
  66.      *
  67.      * Responsible for managing existing data assigned to a VOD title.
  68.      * The SVOD operator dates are managed within form tab inside the
  69.      * TitleAdmin and allows configuring the release and end dates
  70.      * per operator.
  71.      *
  72.      * This subscriber will load an SVODOperatorDate entity instance
  73.      * based on provided extended nav code and operator code.
  74.      *
  75.      * @param \App\Application\EntityImportBundle\Event\EntityPreInitEvent $event
  76.      *   The instance of the dispatched event
  77.      */
  78.     public function onEntityPreInit(EntityPreInitEvent $event): void
  79.     {
  80.         if (TitleSvodOperatorDates::class !== $event->getTargetClassName()) {
  81.             return;
  82.         }
  83.         $configuredProperties $event->getConfigurationProperties();
  84.         $extendedCodeProperty $this->filterConfiguredProperty('navCode'$configuredPropertiestrue);
  85.         $operatorCodeProperty $this->filterConfiguredProperty('operator'$configuredPropertiestrue);
  86.         $countryCodeProperty $this->filterConfiguredProperty('country'$configuredPropertiestrue);
  87.         // Obtain the reference to both configuration properties, as they are
  88.         // required in order to load their values and therefore the entity itself.
  89.         if (!isset($extendedCodeProperty$operatorCodeProperty$countryCodeProperty)) {
  90.             $event->setSkipRowProcessing(true);
  91.             return;
  92.         }
  93.         $extendedNavCode $this->getValue($extendedCodeProperty$event->getData());
  94.         $operatorCode $this->getValue($operatorCodeProperty$event->getData());
  95.         $country strtoupper($this->getValue($countryCodeProperty$event->getData()));
  96.         if (!$extendedNavCode || !$operatorCode || !$country) {
  97.             $event->setSkipRowProcessing(true);
  98.             return;
  99.         }
  100.         $entity $this->svodOperatorRepository
  101.             ->findByExtendedCodeAndOperatorCodeAndCountry($extendedNavCode$operatorCode$country);
  102.         if (!$entity instanceof TitleSvodOperatorDates) {
  103.             // No existing dates found for given title. So, create one instead.
  104.             // In order to proceed further though, we need to make sure we can
  105.             // obtain a reference to valid Title and Operator entities and country.
  106.             $operator $this->operatorRepository->findOneBy([
  107.                 'code' => $operatorCode,
  108.             ]);
  109.             $vodTitle $this->titleRepository->findOneBy([
  110.                 'extendedNavCode' => $extendedNavCode,
  111.             ]);
  112.             if (
  113.                 !$vodTitle instanceof Title
  114.                 || !$operator instanceof Operator
  115.                 || !$operator->hasCountryByCode($country)
  116.             ) {
  117.                 $event->setSkipRowProcessing(true);
  118.                 return;
  119.             }
  120.             $entity = new TitleSvodOperatorDates();
  121.             $entity->setTitle($vodTitle);
  122.             $entity->setOperator($operator);
  123.             $entity->setCountry($country);
  124.         }
  125.         // Handle processing of the passed data for each line of the parsed source file.
  126.         if (!$this->handleRow($entity$configuredProperties$event->getData())) {
  127.             $event->setSkipRowProcessing(true);
  128.             return;
  129.         }
  130.         $entity->setCreatedDate(new \DateTime());
  131.         // Finish the import operation.
  132.         $event->setEntity($entity);
  133.     }
  134.     /**
  135.      * Responsible for processing the data.
  136.      *
  137.      * @param \App\Entity\VOD\TitleSvodOperatorDates $entity
  138.      *   The product entity containing the censorship collection
  139.      * @param \App\Application\EntityImportBundle\ValueObject\ConfigItemPropertiesValue[] $configuredProperties
  140.      *   The collection with configured properties
  141.      * @param array $data
  142.      *   The parsed data array for the current row
  143.      */
  144.     protected function handleRow(
  145.         TitleSvodOperatorDates $entity,
  146.         array $configuredProperties,
  147.         array $data
  148.     ): ?TitleSvodOperatorDates {
  149.         $releaseDateProperty $this->filterConfiguredProperty('releaseDate'$configuredProperties);
  150.         $endDateProperty $this->filterConfiguredProperty('endDate'$configuredProperties);
  151.         // Skip processing only if both properties cannot be obtained.
  152.         if (null === $releaseDateProperty && null === $endDateProperty) {
  153.             return null;
  154.         }
  155.         // Handle date values. If the date is not valid, then DateTime object won't be initialized,
  156.         // resulting in exception being thrown. Simply return "null" and the current row will be
  157.         // marked as skipped.
  158.         if ($releaseDateProperty && ($releaseDate $this->getValue($releaseDateProperty$data))) {
  159.             try {
  160.                 if ($this->getAllowedNullableCharacter() === $releaseDate) {
  161.                     $this->setPropertyValue($entity$releaseDateProperty$releaseDate);
  162.                 } else {
  163.                     $entity->setReleaseDate(\DateTime::createFromFormat('d-m-Y'$releaseDate));
  164.                 }
  165.             } catch (\Exception $ex) {
  166.                 return null;
  167.             }
  168.         }
  169.         if ($endDateProperty && ($endDate $this->getValue($endDateProperty$data))) {
  170.             try {
  171.                 if ($this->getAllowedNullableCharacter() === $endDate) {
  172.                     $this->setPropertyValue($entity$endDateProperty$endDate);
  173.                 } else {
  174.                     $entity->setEndDate(\DateTime::createFromFormat('d-m-Y'$endDate));
  175.                 }
  176.             } catch (\Exception $ex) {
  177.                 return null;
  178.             }
  179.         }
  180.         return $entity;
  181.     }
  182. }