<?php
namespace App\EventSubscriber\Import\EntityMapper\ProductAvailability;
use App\Application\EntityImportBundle\Event\EntityPreInitEvent;
use App\Application\EntityImportBundle\ValueObject\ConfigItemPropertiesValue;
use App\Entity\Product;
use App\Entity\ProductAvailability;
use App\EventSubscriber\Import\EntityMapper\BasePreInitSubscriber;
class EntityPreInitSubscriber extends BasePreInitSubscriber
{
private $productEntity;
private $territory;
private $window;
private $data;
public static function getSubscribedEvents(): array
{
return [
EntityPreInitEvent::class => 'onEntityPreInit',
];
}
public function onEntityPreInit(EntityPreInitEvent $event): void
{
if (
Product::class !== $event->getTargetClassName()
|| 'titleCode' !== $event->getPrimaryProperty()->getName()
) {
return;
}
$configuredProperties = $event->getConfigurationProperties();
[$titleCodeProperty, $windowProperty, $territoryProperty] = [
$event->getPrimaryProperty(),
$this->filterConfiguredProperty('window', $configuredProperties, true),
$this->filterConfiguredProperty('territory', $configuredProperties, true),
];
if (null === $titleCodeProperty || null === $windowProperty || null === $territoryProperty) {
$event->setSkipRowProcessing(true);
return;
}
$this->data = $event->getData();
if (!$titleCode = $this->getValue($titleCodeProperty, $this->data)) {
$event->setSkipRowProcessing(true);
return;
}
if (!$this->window = $this->getValue($windowProperty, $this->data)) {
$event->setSkipRowProcessing(true);
return;
}
if (!$this->territory = $this->getValue($territoryProperty, $this->data)) {
$event->setSkipRowProcessing(true);
return;
}
$repository = $this->getEntityManager()->getRepository(Product::class);
if (!$this->productEntity = $repository->findOneByNavCode($titleCode)) {
$event->setSkipRowProcessing(true);
return;
}
$startDateProperty = $this->filterConfiguredProperty('startDate', $configuredProperties);
$endDateProperty = $this->filterConfiguredProperty('endDate', $configuredProperties);
$foundAvailability = false;
foreach ($this->productEntity->getProductAvailability() as $availability) {
/** @var \App\Entity\ProductAvailability $availability */
if ($this->window === $availability->getWindow() && $this->territory === $availability->getTerritory()) {
$this->setStartDate($availability, $startDateProperty);
$this->setEndDate($availability, $endDateProperty);
$foundAvailability = true;
$this->getEntityManager()->persist($availability);
}
}
if (!$foundAvailability) {
$this->createNewAvailability($startDateProperty, $endDateProperty);
}
$event->setEntity($this->productEntity);
}
private function createNewAvailability($startDateProperty, $endDateProperty)
{
$newProductAvailability = new ProductAvailability();
$newProductAvailability->setWindow($this->window);
$newProductAvailability->setTerritory($this->territory);
$this->setStartDate($newProductAvailability, $startDateProperty);
$this->setEndDate($newProductAvailability, $endDateProperty);
$this->productEntity->addProductAvailability($newProductAvailability);
}
private function setStartDate(ProductAvailability $entity, ConfigItemPropertiesValue $property): void
{
$value = $this->getValue($property, $this->data);
if ($value && $this->getAllowedNullableCharacter() === $value) {
$entity->setStartDate(null);
return;
}
if (!$dateTime = \DateTime::createFromFormat('d-m-Y', $value)) {
return;
}
$entity->setStartDate($dateTime);
}
private function setEndDate(ProductAvailability $entity, ConfigItemPropertiesValue $property): void
{
$value = $this->getValue($property, $this->data);
if ($value && $this->getAllowedNullableCharacter() === $value) {
$entity->setEndDate(null);
return;
}
if (!$dateTime = \DateTime::createFromFormat('d-m-Y', $value)) {
return;
}
$entity->setEndDate($dateTime);
}
}