<?php
namespace App\EventSubscriber\Import\EntityMapper\VOD\SVODOperatorDates;
use App\Application\EntityImportBundle\Event\EntityPreInitEvent;
use App\Entity\VOD\Operator;
use App\Entity\VOD\Title;
use App\Entity\VOD\TitleSvodOperatorDates;
use App\EventSubscriber\Import\EntityMapper\BasePreInitSubscriber;
use App\Repository\VOD\OperatorRepository;
use App\Repository\VOD\SvodOperatorDatesRepository;
use App\Repository\VOD\TitleRepository;
use Doctrine\ORM\EntityManagerInterface;
/**
* Class EntityPreInitSubscriber.
*/
class EntityPreInitSubscriber extends BasePreInitSubscriber
{
/**
* The instance of the title svod operator date repository.
*
* @var \App\Repository\VOD\SvodOperatorDatesRepository
*/
private $svodOperatorRepository;
/**
* The instance of the operator repository.
*
* @var \App\Repository\VOD\OperatorRepository
*/
private $operatorRepository;
/**
* The instance of the vod title repository.
*
* @var \App\Repository\VOD\TitleRepository
*/
private $titleRepository;
/**
* EntityPreInitSubscriber constructor.
*
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
* The entity manager
* @param \App\Repository\VOD\SvodOperatorDatesRepository $svodOperatorRepository
* The instance of the svod operator dates repository
* @param \App\Repository\VOD\OperatorRepository $operatorRepository
* The instance of the operator repository
* @param \App\Repository\VOD\TitleRepository $titleRepository
* The instance of the vod title repository
*/
public function __construct(
EntityManagerInterface $entityManager,
SvodOperatorDatesRepository $svodOperatorRepository,
OperatorRepository $operatorRepository,
TitleRepository $titleRepository
) {
parent::__construct($entityManager);
$this->svodOperatorRepository = $svodOperatorRepository;
$this->operatorRepository = $operatorRepository;
$this->titleRepository = $titleRepository;
}
public static function getSubscribedEvents()
{
return [
EntityPreInitEvent::class => 'onEntityPreInit',
];
}
/**
* Public callback for the pre-init event.
*
* Responsible for managing existing data assigned to a VOD title.
* The SVOD operator dates are managed within form tab inside the
* TitleAdmin and allows configuring the release and end dates
* per operator.
*
* This subscriber will load an SVODOperatorDate entity instance
* based on provided extended nav code and operator code.
*
* @param \App\Application\EntityImportBundle\Event\EntityPreInitEvent $event
* The instance of the dispatched event
*/
public function onEntityPreInit(EntityPreInitEvent $event): void
{
if (TitleSvodOperatorDates::class !== $event->getTargetClassName()) {
return;
}
$configuredProperties = $event->getConfigurationProperties();
$extendedCodeProperty = $this->filterConfiguredProperty('navCode', $configuredProperties, true);
$operatorCodeProperty = $this->filterConfiguredProperty('operator', $configuredProperties, true);
$countryCodeProperty = $this->filterConfiguredProperty('country', $configuredProperties, true);
// Obtain the reference to both configuration properties, as they are
// required in order to load their values and therefore the entity itself.
if (!isset($extendedCodeProperty, $operatorCodeProperty, $countryCodeProperty)) {
$event->setSkipRowProcessing(true);
return;
}
$extendedNavCode = $this->getValue($extendedCodeProperty, $event->getData());
$operatorCode = $this->getValue($operatorCodeProperty, $event->getData());
$country = strtoupper($this->getValue($countryCodeProperty, $event->getData()));
if (!$extendedNavCode || !$operatorCode || !$country) {
$event->setSkipRowProcessing(true);
return;
}
$entity = $this->svodOperatorRepository
->findByExtendedCodeAndOperatorCodeAndCountry($extendedNavCode, $operatorCode, $country);
if (!$entity instanceof TitleSvodOperatorDates) {
// No existing dates found for given title. So, create one instead.
// In order to proceed further though, we need to make sure we can
// obtain a reference to valid Title and Operator entities and country.
$operator = $this->operatorRepository->findOneBy([
'code' => $operatorCode,
]);
$vodTitle = $this->titleRepository->findOneBy([
'extendedNavCode' => $extendedNavCode,
]);
if (
!$vodTitle instanceof Title
|| !$operator instanceof Operator
|| !$operator->hasCountryByCode($country)
) {
$event->setSkipRowProcessing(true);
return;
}
$entity = new TitleSvodOperatorDates();
$entity->setTitle($vodTitle);
$entity->setOperator($operator);
$entity->setCountry($country);
}
// Handle processing of the passed data for each line of the parsed source file.
if (!$this->handleRow($entity, $configuredProperties, $event->getData())) {
$event->setSkipRowProcessing(true);
return;
}
$entity->setCreatedDate(new \DateTime());
// Finish the import operation.
$event->setEntity($entity);
}
/**
* Responsible for processing the data.
*
* @param \App\Entity\VOD\TitleSvodOperatorDates $entity
* The product entity containing the censorship collection
* @param \App\Application\EntityImportBundle\ValueObject\ConfigItemPropertiesValue[] $configuredProperties
* The collection with configured properties
* @param array $data
* The parsed data array for the current row
*/
protected function handleRow(
TitleSvodOperatorDates $entity,
array $configuredProperties,
array $data
): ?TitleSvodOperatorDates {
$releaseDateProperty = $this->filterConfiguredProperty('releaseDate', $configuredProperties);
$endDateProperty = $this->filterConfiguredProperty('endDate', $configuredProperties);
// Skip processing only if both properties cannot be obtained.
if (null === $releaseDateProperty && null === $endDateProperty) {
return null;
}
// Handle date values. If the date is not valid, then DateTime object won't be initialized,
// resulting in exception being thrown. Simply return "null" and the current row will be
// marked as skipped.
if ($releaseDateProperty && ($releaseDate = $this->getValue($releaseDateProperty, $data))) {
try {
if ($this->getAllowedNullableCharacter() === $releaseDate) {
$this->setPropertyValue($entity, $releaseDateProperty, $releaseDate);
} else {
$entity->setReleaseDate(\DateTime::createFromFormat('d-m-Y', $releaseDate));
}
} catch (\Exception $ex) {
return null;
}
}
if ($endDateProperty && ($endDate = $this->getValue($endDateProperty, $data))) {
try {
if ($this->getAllowedNullableCharacter() === $endDate) {
$this->setPropertyValue($entity, $endDateProperty, $endDate);
} else {
$entity->setEndDate(\DateTime::createFromFormat('d-m-Y', $endDate));
}
} catch (\Exception $ex) {
return null;
}
}
return $entity;
}
}