<?php
namespace App\EventSubscriber\Import\EntityMapper;
use App\Application\EntityImportBundle\Event\EntityMapperEvent;
use App\Util\Helper\StringUtils;
/**
* Class BaseMapperSubscriber.
*/
abstract class BaseMapperSubscriber extends AbstractMapperSubscriber
{
/**
* Responsible for determine whether or not to proceed with the event.
*
* This method will be overridden in the event subscriber for the
* other entity of type product - TV Series, since it follows
* almost the same structure.
*
* If you change anything in here for some reason, make sure to
* double check the other subscriber as well.
*
* @param \App\Application\EntityImportBundle\Event\EntityMapperEvent $event
* The instance of the dispatched event
*
* @return bool
* The result check
*/
abstract protected function supports(EntityMapperEvent $event): bool;
/**
* {@inheritdoc}
*
* @uses \App\EventSubscriber\Import\EntityMapper\ProductCast\EntityMapperSubscriber::onEntityPrePersist()
* @uses \App\EventSubscriber\Import\EntityMapper\ProductMovie\EntityMapperSubscriber::onEntityPrePersist()
*/
public static function getSubscribedEvents(): array
{
return [
EntityMapperEvent::class => 'onEntityPrePersist',
];
}
/**
* Public callback for the event.
*
* Responsible for providing additional logic to a set
* of entities sharing common logic.
* Such case would be to populate the "createdBy" or
* "updatedBy" columns for instance.
*
* @param \App\Application\EntityImportBundle\Event\EntityMapperEvent $event
* The instance of the dispatched event
*/
public function onEntityPrePersist(EntityMapperEvent $event): void
{
if (!$this->supports($event)) {
return;
}
$entity = $event->getEntity();
$data = $event->getData();
foreach ($event->getConfigurationProperties() as $index => $property) {
$callbackMethod = StringUtils::camelize(sprintf('handle_%s', $property->getName()));
if (method_exists($this, $callbackMethod)) {
$this->{$callbackMethod}($entity, $property, $data);
}
}
$this->getEntityManager()->persist($entity);
$this->onMappingComplete($entity, $data);
}
/**
* Custom method called once all the mapping is completed.
*
* Can be used to determine some specific state of the entity
* and perform one final operation.
*
* @param object $entity
* The instance of the managed entity
* @param array $data
* The array containing the current row parsed data
*/
protected function onMappingComplete(object $entity, array $data): void
{
// To be overridden if necessary in order to perform some
// additional logic once all callbacks have been executed.
}
}