. */ namespace SP\Providers\Notification; use Exception; use SP\Core\Application; use SP\Core\Events\Event; use SP\Domain\Core\Events\EventReceiver; use SP\Domain\Notification\Models\Notification; use SP\Domain\Notification\Ports\NotificationService; use SP\Providers\EventsTrait; use SP\Providers\Provider; use function SP\__; use function SP\processException; /** * Class NotificationHandler * * @package SP\Providers\Notification */ final class NotificationHandler extends Provider implements EventReceiver { use EventsTrait; public const EVENTS = [ 'request.account', 'show.account.link', ]; private string $events; public function __construct( Application $application, private readonly NotificationService $notificationService ) { parent::__construct($application); } /** * Devuelve los eventos que implementa el observador en formato cadena * * @return string */ public function getEventsString(): string { return $this->events; } /** * Evento de actualización * * @param string $eventType Nombre del evento * @param Event $event Objeto del evento */ public function update(string $eventType, Event $event): void { switch ($eventType) { case 'request.account': $this->requestAccountNotification($event); break; case 'show.account.link': $this->showAccountLinkNotification($event); break; } } /** * @param Event $event */ private function requestAccountNotification(Event $event): void { $eventMessage = $event->getEventMessage(); $userIds = $eventMessage !== null ? $eventMessage->getExtra('userId') : []; foreach ($userIds as $userId) { $description = $eventMessage->composeHtml(); $notificationData = new Notification(); $notificationData->setType(__('Request')); $notificationData->setComponent(__('Accounts')); $notificationData->setUserId($userId); $notificationData->setDescription($eventMessage, true); $this->notify($notificationData); } } /** * @param Notification $notificationData */ private function notify(Notification $notificationData): void { try { $this->notificationService->create($notificationData); } catch (Exception $e) { processException($e); } } /** * @param Event $event */ private function showAccountLinkNotification(Event $event): void { $eventMessage = $event->getEventMessage(); $notify = $eventMessage !== null ? $eventMessage->getExtra('notify') : []; if ($notify[0] === true) { $userId = $eventMessage->getExtra('userId')[0]; $description = $eventMessage->composeHtml(); $notificationData = new Notification(); $notificationData->setType(__('Notification')); $notificationData->setComponent(__('Accounts')); $notificationData->setUserId($userId); $notificationData->setDescription($eventMessage, true); $this->notify($notificationData); } } public function initialize(): void { $this->events = $this->parseEventsToRegex(self::EVENTS); $this->initialized = true; } }