<?php
namespace App\Subscriber\Async;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Platform\QueueBundle\Event\AsyncEvent;
use Platform\QueueBundle\Model\AsyncMessage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
abstract class AbstractAsyncSubscriber implements EventSubscriberInterface
{
/**
* All extending classes need to set this value to something unique.
*/
public const EVENT = null;
/**
* The default priority to use for items of this type.
*/
public const DEFAULT_PRIORITY = null;
/**
* @var EntityManager
*/
protected EntityManager $em;
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents(): array
{
return static::EVENT ? [
static::EVENT => ['onEvent', 0],
] : [];
}
/**
* @param EntityManager $em
*/
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @param AsyncEvent $event
* @return void
*/
public function onEvent(AsyncEvent $event): void
{
if ( ! static::EVENT) {
return;
}
try {
$this->handleEvent($event);
} catch (\Exception $e) {
// TODO: an error happened, need to log it somewhere eventually...
}
}
/**
* @param AsyncEvent $event
* @return void
*/
abstract protected function handleEvent(AsyncEvent $event): void;
}