src/EventSubscriber/MessengerDeDuplicationSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Messenger\DeDuplicationUniqueHashStamp;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Psr\Cache\InvalidArgumentException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
  8. class MessengerDeDuplicationSubscriber implements EventSubscriberInterface
  9. {
  10.     private CacheItemPoolInterface $cacheItemPool;
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             WorkerMessageHandledEvent::class => 'handleWorkerMessageHandled',
  15.         ];
  16.     }
  17.     public function __construct(CacheItemPoolInterface $cacheItemPool)
  18.     {
  19.         $this->cacheItemPool $cacheItemPool;
  20.     }
  21.     /**
  22.      * @throws InvalidArgumentException
  23.      */
  24.     public function handleWorkerMessageHandled(WorkerMessageHandledEvent $event): void
  25.     {
  26.         /** @var DeDuplicationUniqueHashStamp $deDuplicationStamp */
  27.         $deDuplicationStamp $event->getEnvelope()->last(DeDuplicationUniqueHashStamp::class);
  28.         if ($deDuplicationStamp instanceof DeDuplicationUniqueHashStamp && $uniqueKey $deDuplicationStamp->getUniqueKey()) {
  29.             $this->cacheItemPool->deleteItem($uniqueKey);
  30.         }
  31.     }
  32. }