src/App/Subscriber/Scheduler/AbstractSchedulerSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\Scheduler;
  3. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  4. use Platform\QueueBundle\Event\AsyncEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. /**
  7.  *
  8.  */
  9. abstract class AbstractSchedulerSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * All extending classes need to set this value to something unique.
  13.      * It is the name of the event to watch for that is going to be injected into the MQ by the Lambda functions.
  14.      */
  15.     public const EVENT null;
  16.     /**
  17.      * @var EntityManager
  18.      */
  19.     protected EntityManager $em;
  20.     /**
  21.      * {@inheritDoc}
  22.      */
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return static::EVENT ? [
  26.             static::EVENT => ['onEvent'0],
  27.         ] : [];
  28.     }
  29.     /**
  30.      * @param EntityManager $em
  31.      */
  32.     public function __construct(EntityManager $em)
  33.     {
  34.         $this->em $em;
  35.     }
  36.     /**
  37.      * @param AsyncEvent $event
  38.      * @return void
  39.      */
  40.     public function onEvent(AsyncEvent $event): void
  41.     {
  42.         if ( ! static::EVENT) {
  43.             return;
  44.         }
  45.         try {
  46.             $this->handleEvent($event);
  47.         } catch (\Exception $e) {
  48.             // TODO: an error happened, need to log it somewhere eventually...
  49.         }
  50.     }
  51.     /**
  52.      * @param AsyncEvent $event
  53.      * @return void
  54.      */
  55.     abstract protected function handleEvent(AsyncEvent $event): void;
  56. }