src/App/Subscriber/Async/AbstractAsyncSubscriber.php line 49

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