vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/NotificationService.php line 23

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\NotificationService as NotificationServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
  10. use eZ\Publish\API\Repository\Values\Notification\Notification;
  11. use eZ\Publish\API\Repository\Events\Notification\BeforeCreateNotificationEvent;
  12. use eZ\Publish\API\Repository\Events\Notification\BeforeDeleteNotificationEvent;
  13. use eZ\Publish\API\Repository\Events\Notification\BeforeMarkNotificationAsReadEvent;
  14. use eZ\Publish\API\Repository\Events\Notification\CreateNotificationEvent;
  15. use eZ\Publish\API\Repository\Events\Notification\DeleteNotificationEvent;
  16. use eZ\Publish\API\Repository\Events\Notification\MarkNotificationAsReadEvent;
  17. use eZ\Publish\SPI\Repository\Decorator\NotificationServiceDecorator;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. class NotificationService extends NotificationServiceDecorator
  20. {
  21.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  22.     protected $eventDispatcher;
  23.     public function __construct(
  24.         NotificationServiceInterface $innerService,
  25.         EventDispatcherInterface $eventDispatcher
  26.     ) {
  27.         parent::__construct($innerService);
  28.         $this->eventDispatcher $eventDispatcher;
  29.     }
  30.     public function markNotificationAsRead(Notification $notification): void
  31.     {
  32.         $eventData = [$notification];
  33.         $beforeEvent = new BeforeMarkNotificationAsReadEvent(...$eventData);
  34.         $this->eventDispatcher->dispatch($beforeEvent);
  35.         if ($beforeEvent->isPropagationStopped()) {
  36.             return;
  37.         }
  38.         $this->innerService->markNotificationAsRead($notification);
  39.         $this->eventDispatcher->dispatch(
  40.             new MarkNotificationAsReadEvent(...$eventData)
  41.         );
  42.     }
  43.     public function createNotification(CreateStruct $createStruct): Notification
  44.     {
  45.         $eventData = [$createStruct];
  46.         $beforeEvent = new BeforeCreateNotificationEvent(...$eventData);
  47.         $this->eventDispatcher->dispatch($beforeEvent);
  48.         if ($beforeEvent->isPropagationStopped()) {
  49.             return $beforeEvent->getNotification();
  50.         }
  51.         $notification $beforeEvent->hasNotification()
  52.             ? $beforeEvent->getNotification()
  53.             : $this->innerService->createNotification($createStruct);
  54.         $this->eventDispatcher->dispatch(
  55.             new CreateNotificationEvent($notification, ...$eventData)
  56.         );
  57.         return $notification;
  58.     }
  59.     public function deleteNotification(Notification $notification): void
  60.     {
  61.         $eventData = [$notification];
  62.         $beforeEvent = new BeforeDeleteNotificationEvent(...$eventData);
  63.         $this->eventDispatcher->dispatch($beforeEvent);
  64.         if ($beforeEvent->isPropagationStopped()) {
  65.             return;
  66.         }
  67.         $this->innerService->deleteNotification($notification);
  68.         $this->eventDispatcher->dispatch(
  69.             new DeleteNotificationEvent(...$eventData)
  70.         );
  71.     }
  72. }