src/EventSubscriber/Import/EntityMapper/BaseMapperSubscriber.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Import\EntityMapper;
  3. use App\Application\EntityImportBundle\Event\EntityMapperEvent;
  4. use App\Util\Helper\StringUtils;
  5. /**
  6.  * Class BaseMapperSubscriber.
  7.  */
  8. abstract class BaseMapperSubscriber extends AbstractMapperSubscriber
  9. {
  10.     /**
  11.      * Responsible for determine whether or not to proceed with the event.
  12.      *
  13.      * This method will be overridden in the event subscriber for the
  14.      * other entity of type product - TV Series, since it follows
  15.      * almost the same structure.
  16.      *
  17.      * If you change anything in here for some reason, make sure to
  18.      * double check the other subscriber as well.
  19.      *
  20.      * @param \App\Application\EntityImportBundle\Event\EntityMapperEvent $event
  21.      *   The instance of the dispatched event
  22.      *
  23.      * @return bool
  24.      *   The result check
  25.      */
  26.     abstract protected function supports(EntityMapperEvent $event): bool;
  27.     /**
  28.      * {@inheritdoc}
  29.      *
  30.      * @uses \App\EventSubscriber\Import\EntityMapper\ProductCast\EntityMapperSubscriber::onEntityPrePersist()
  31.      * @uses \App\EventSubscriber\Import\EntityMapper\ProductMovie\EntityMapperSubscriber::onEntityPrePersist()
  32.      */
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             EntityMapperEvent::class => 'onEntityPrePersist',
  37.         ];
  38.     }
  39.     /**
  40.      * Public callback for the event.
  41.      *
  42.      * Responsible for providing additional logic to a set
  43.      * of entities sharing common logic.
  44.      * Such case would be to populate the "createdBy" or
  45.      * "updatedBy" columns for instance.
  46.      *
  47.      * @param \App\Application\EntityImportBundle\Event\EntityMapperEvent $event
  48.      *   The instance of the dispatched event
  49.      */
  50.     public function onEntityPrePersist(EntityMapperEvent $event): void
  51.     {
  52.         if (!$this->supports($event)) {
  53.             return;
  54.         }
  55.         $entity $event->getEntity();
  56.         $data $event->getData();
  57.         foreach ($event->getConfigurationProperties() as $index => $property) {
  58.             $callbackMethod StringUtils::camelize(sprintf('handle_%s'$property->getName()));
  59.             if (method_exists($this$callbackMethod)) {
  60.                 $this->{$callbackMethod}($entity$property$data);
  61.             }
  62.         }
  63.         $this->getEntityManager()->persist($entity);
  64.         $this->onMappingComplete($entity$data);
  65.     }
  66.     /**
  67.      * Custom method called once all the mapping is completed.
  68.      *
  69.      * Can be used to determine some specific state of the entity
  70.      * and perform one final operation.
  71.      *
  72.      * @param object $entity
  73.      *   The instance of the managed entity
  74.      * @param array $data
  75.      *   The array containing the current row parsed data
  76.      */
  77.     protected function onMappingComplete(object $entity, array $data): void
  78.     {
  79.         // To be overridden if necessary in order to perform some
  80.         // additional logic once all callbacks have been executed.
  81.     }
  82. }