. */ namespace SP\Providers\Notification; use SP\Core\Events\Event; use SP\Core\Events\EventReceiver; use SP\DataModel\NotificationData; use SP\Providers\Provider; use SP\Services\Notification\NotificationService; use SplSubject; /** * Class NotificationHandler * * @package SP\Providers\Notification */ class NotificationHandler extends Provider implements EventReceiver { const EVENTS = [ 'request.account', 'show.account.link' ]; /** * @var NotificationService */ protected $notificationService; /** * @var string */ protected $events; /** * Inicialización del observador */ public function init() { // TODO: Implement init() method. } /** * Evento de actualización * * @param string $eventType Nombre del evento * @param Event $event Objeto del evento */ public function updateEvent($eventType, Event $event) { switch ($eventType) { case 'request.account': $this->requestAccountNotification($event); break; case 'show.account.link': $this->showAccountLinkNotification($event); break; } } /** * @param Event $event */ protected function requestAccountNotification(Event $event) { $eventMessage = $event->getEventMessage(); $data = $eventMessage->getData(); foreach ($data['userId'] as $userId) { $notificationData = new NotificationData(); $notificationData->setType(__('Solicitud')); $notificationData->setComponent(__('Cuentas')); $notificationData->setUserId($userId); $notificationData->setDescription($eventMessage); $this->notify($notificationData); } } /** * @param NotificationData $notificationData */ protected function notify(NotificationData $notificationData) { try { $this->notificationService->create($notificationData); } catch (\Exception $e) { processException($e); } } /** * @param Event $event */ protected function showAccountLinkNotification(Event $event) { $eventMessage = $event->getEventMessage(); $data = $eventMessage->getData(); if ($data['notify'] === true) { $notificationData = new NotificationData(); $notificationData->setType(__('Notificación')); $notificationData->setComponent(__('Cuentas')); $notificationData->setUserId($data['userId']); $notificationData->setDescription($eventMessage); $this->notify($notificationData); } } /** * Devuelve los eventos que implementa el observador * * @return array */ public function getEvents() { return self::EVENTS; } /** * Devuelve los eventos que implementa el observador en formato cadena * * @return string */ public function getEventsString() { return $this->events; } /** * Receive update from subject * * @link http://php.net/manual/en/splobserver.update.php * @param SplSubject $subject
* The SplSubject notifying the observer of an update. *
* @return void * @since 5.1.0 */ public function update(SplSubject $subject) { // TODO: Implement update() method. } protected function initialize() { $this->notificationService = $this->dic->get(NotificationService::class); $this->events = str_replace('.', '\\.', implode('|', self::EVENTS)); } }