Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/Repository/NotificationService.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Publish\Core\Repository;
10
11 use DateTime;
12 use eZ\Publish\API\Repository\NotificationService as NotificationServiceInterface;
13 use eZ\Publish\API\Repository\PermissionResolver;
14 use eZ\Publish\API\Repository\Values\Notification\CreateStruct as APICreateStruct;
15 use eZ\Publish\API\Repository\Values\Notification\Notification as APINotification;
16 use eZ\Publish\API\Repository\Values\Notification\NotificationList;
17 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
18 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
19 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
20 use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
21 use eZ\Publish\SPI\Persistence\Notification\Handler;
22 use eZ\Publish\SPI\Persistence\Notification\Notification;
23 use eZ\Publish\SPI\Persistence\Notification\UpdateStruct;
24
25 class NotificationService implements NotificationServiceInterface
26 {
28 protected $persistenceHandler;
29
31 protected $permissionResolver;
32
37 public function __construct(Handler $persistenceHandler, PermissionResolver $permissionResolver)
38 {
39 $this->persistenceHandler = $persistenceHandler;
40 $this->permissionResolver = $permissionResolver;
41 }
42
46 public function loadNotifications(int $offset = 0, int $limit = 25): NotificationList
47 {
48 $currentUserId = $this->getCurrentUserId();
49
50 $list = new NotificationList();
51 $list->totalCount = $this->persistenceHandler->countNotifications($currentUserId);
52 if ($list->totalCount > 0) {
53 $list->items = array_map(function (Notification $spiNotification) {
54 return $this->buildDomainObject($spiNotification);
55 }, $this->persistenceHandler->loadUserNotifications($currentUserId, $offset, $limit));
56 }
57
58 return $list;
59 }
60
64 public function createNotification(APICreateStruct $createStruct): APINotification
65 {
66 $spiCreateStruct = new CreateStruct();
67
68 if (empty($createStruct->ownerId)) {
69 throw new InvalidArgumentException('ownerId', $createStruct->ownerId);
70 }
71
72 $spiCreateStruct->ownerId = $createStruct->ownerId;
73
74 if (empty($createStruct->type)) {
75 throw new InvalidArgumentException('type', $createStruct->type);
76 }
77
78 $spiCreateStruct->type = $createStruct->type;
79 $spiCreateStruct->isPending = (bool) $createStruct->isPending;
80 $spiCreateStruct->data = $createStruct->data;
81 $spiCreateStruct->created = (new DateTime())->getTimestamp();
82
83 return $this->buildDomainObject(
84 $this->persistenceHandler->createNotification($spiCreateStruct)
85 );
86 }
87
91 public function getNotification(int $notificationId): APINotification
92 {
93 $notification = $this->persistenceHandler->getNotificationById($notificationId);
94
95 $currentUserId = $this->getCurrentUserId();
96 if (!$notification->ownerId || $currentUserId != $notification->ownerId) {
97 throw new NotFoundException('Notification', $notificationId);
98 }
99
100 return $this->buildDomainObject($notification);
101 }
102
106 public function markNotificationAsRead(APINotification $notification): void
107 {
108 $currentUserId = $this->getCurrentUserId();
109
110 if (!$notification->id) {
111 throw new NotFoundException('Notification', $notification->id);
112 }
113
114 if ($notification->ownerId !== $currentUserId) {
115 throw new UnauthorizedException($notification->id, 'Notification');
116 }
117
118 if (!$notification->isPending) {
119 return;
120 }
121
122 $updateStruct = new UpdateStruct();
123 $updateStruct->isPending = false;
124
125 $this->persistenceHandler->updateNotification($notification, $updateStruct);
126 }
127
131 public function getPendingNotificationCount(): int
132 {
133 return $this->persistenceHandler->countPendingNotifications(
134 $this->getCurrentUserId()
135 );
136 }
137
141 public function getNotificationCount(): int
142 {
143 return $this->persistenceHandler->countNotifications(
144 $this->getCurrentUserId()
145 );
146 }
147
151 public function deleteNotification(APINotification $notification): void
152 {
153 $this->persistenceHandler->delete($notification);
154 }
155
163 protected function buildDomainObject(Notification $spiNotification): APINotification
164 {
165 return new APINotification([
166 'id' => $spiNotification->id,
167 'ownerId' => $spiNotification->ownerId,
168 'isPending' => $spiNotification->isPending,
169 'type' => $spiNotification->type,
170 'created' => new DateTime("@{$spiNotification->created}"),
171 'data' => $spiNotification->data,
172 ]);
173 }
174
175 private function getCurrentUserId(): int
176 {
177 return $this->permissionResolver
178 ->getCurrentUserReference()
179 ->getUserId();
180 }
181 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Notification\CreateStruct
Definition: API/Repository/Values/Notification/CreateStruct.php:14
eZ\Publish\API\Repository\Values\Notification\NotificationList
Definition: NotificationList.php:16
eZ\Publish\API\Repository\Values\Notification\Notification
Definition: API/Repository/Values/Notification/Notification.php:24
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\Repository\NotificationService
Definition: Core/Repository/NotificationService.php:26
eZ\Publish\Core\Repository\NotificationService\deleteNotification
deleteNotification(APINotification $notification)
{}
Definition: Core/Repository/NotificationService.php:151
eZ\Publish\Core\Repository\NotificationService\getPendingNotificationCount
getPendingNotificationCount()
{Get count of unread users notifications.int}
Definition: Core/Repository/NotificationService.php:131
eZ\Publish\Core\Repository\NotificationService\$permissionResolver
$permissionResolver
Definition: Core/Repository/NotificationService.php:31
eZ\Publish\Core\Repository\NotificationService\getNotification
getNotification(int $notificationId)
{Load single notification (by ID).Notification ID}
Definition: Core/Repository/NotificationService.php:91
eZ\Publish\Core\Repository\NotificationService\buildDomainObject
buildDomainObject(Notification $spiNotification)
Builds Notification domain object from ValueObject returned by Persistence API.
Definition: Core/Repository/NotificationService.php:163
eZ\Publish\Core\Repository\NotificationService\$persistenceHandler
$persistenceHandler
Definition: Core/Repository/NotificationService.php:28
eZ\Publish\Core\Repository\NotificationService\getNotificationCount
getNotificationCount()
{Get count of total users notifications.int}
Definition: Core/Repository/NotificationService.php:141
eZ\Publish\Core\Repository\NotificationService\markNotificationAsRead
markNotificationAsRead(APINotification $notification)
{}
Definition: Core/Repository/NotificationService.php:106
eZ\Publish\Core\Repository\NotificationService\getCurrentUserId
getCurrentUserId()
Definition: Core/Repository/NotificationService.php:175
eZ\Publish\Core\Repository\NotificationService\createNotification
createNotification(APICreateStruct $createStruct)
{}
Definition: Core/Repository/NotificationService.php:64
eZ\Publish\Core\Repository\NotificationService\loadNotifications
loadNotifications(int $offset=0, int $limit=25)
{Get currently logged user notifications.the start offset for paging the number of notifications retu...
Definition: Core/Repository/NotificationService.php:46
eZ\Publish\Core\Repository\NotificationService\__construct
__construct(Handler $persistenceHandler, PermissionResolver $permissionResolver)
Definition: Core/Repository/NotificationService.php:37
eZ\Publish\SPI\Persistence\Bookmark\CreateStruct
Definition: SPI/Persistence/Bookmark/CreateStruct.php:14
eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct
Definition: Content/Location/UpdateStruct.php:12
eZ\Publish\SPI\Persistence\Notification\CreateStruct
Definition: SPI/Persistence/Notification/CreateStruct.php:14
eZ\Publish\SPI\Persistence\Notification\Notification
Definition: SPI/Persistence/Notification/Notification.php:14
eZ\Publish\SPI\Persistence\Notification\UpdateStruct
Definition: Notification/UpdateStruct.php:14
eZ\Publish\API\Repository\NotificationService
Service to manager user notifications.
Definition: API/Repository/NotificationService.php:20
eZ\Publish\API\Repository\PermissionResolver
This service provides methods for resolving permissions.
Definition: API/Repository/PermissionResolver.php:17
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\SPI\Persistence\Notification\Handler
Definition: SPI/Persistence/Notification/Handler.php:14
eZ\Publish\Core\Repository
Definition: Core/Repository/BookmarkService.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.