. */ namespace SP\Domain\Notification\Providers; 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 function SP\__; use function SP\processException; /** * Class NotificationHandler * * @package SP\Domain\Providers\Notification */ final class NotificationHandler extends \SP\Domain\Common\Providers\Provider implements EventReceiver { use \SP\Domain\Common\Providers\EventsTrait; public const EVENTS = [ 'request.account', 'show.account.link', ]; private readonly string $events; public function __construct( Application $application, private readonly NotificationService $notificationService ) { parent::__construct($application); $this->setup(); } private function setup(): void { $this->events = $this->parseEventsToRegex(self::EVENTS); } /** * 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) { $notification = new Notification( [ 'type' => __('Request'), 'component' => __('Accounts'), 'userId' => $userId, 'description' => $eventMessage->composeHtml() ] ); $this->notify($notification); } } /** * @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]; $notification = new Notification( [ 'type' => __('Notification'), 'component' => __('Accounts'), 'userId' => $userId, 'description' => $eventMessage->composeHtml() ] ); $this->notify($notification); } } }