<?php
declare(strict_types=1);
namespace App\EventsSubscriber\Notification;
use App\Event\Notification\NotificationLogEvent;
use App\Event\Notification\PushNotificationEvent;
use App\Messenger\Notification\NotificationLogMessage;
use App\Messenger\Notification\PushNotificationMessage;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
class NotificationSubscriber implements EventSubscriberInterface
{
public function __construct(
private MessageBusInterface $bus,
private LoggerInterface $notifyLogger,
) {
}
public static function getSubscribedEvents(): array
{
return [
NotificationLogEvent::class => 'log',
PushNotificationEvent::class => 'push',
];
}
public function log(NotificationLogEvent $event)
{
foreach ($event->getRecipients() as $recipient) {
$this->bus->dispatch(new NotificationLogMessage($event->getType(), $recipient));
}
}
public function push(PushNotificationEvent $event)
{
$this->notifyLogger->info(
'Event: sending push notifications to users',
[
'payload' => $event->getPayload(),
'type' => $event->getType()->value,
]
);
$this->bus->dispatch(
new PushNotificationMessage(
$event->getType(),
$event->getPayload(),
...$event->getRecipients(),
)
);
}
}