src/EventsSubscriber/Notification/NotificationSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventsSubscriber\Notification;
  4. use App\Event\Notification\NotificationLogEvent;
  5. use App\Event\Notification\PushNotificationEvent;
  6. use App\Messenger\Notification\NotificationLogMessage;
  7. use App\Messenger\Notification\PushNotificationMessage;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Messenger\MessageBusInterface;
  11. class NotificationSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private MessageBusInterface $bus,
  15.         private LoggerInterface $notifyLogger,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             NotificationLogEvent::class => 'log',
  22.             PushNotificationEvent::class => 'push',
  23.         ];
  24.     }
  25.     public function log(NotificationLogEvent $event)
  26.     {
  27.         foreach ($event->getRecipients() as $recipient) {
  28.             $this->bus->dispatch(new NotificationLogMessage($event->getType(), $recipient));
  29.         }
  30.     }
  31.     public function push(PushNotificationEvent $event)
  32.     {
  33.         $this->notifyLogger->info(
  34.             'Event: sending push notifications to users',
  35.             [
  36.                 'payload' => $event->getPayload(),
  37.                 'type' => $event->getType()->value,
  38.             ]
  39.         );
  40.         $this->bus->dispatch(
  41.             new PushNotificationMessage(
  42.                 $event->getType(),
  43.                 $event->getPayload(),
  44.                 ...$event->getRecipients(),
  45.             )
  46.         );
  47.     }
  48. }