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

Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace eZ\Publish\Core\Persistence\Legacy\Notification\Gateway;
10
11 use Doctrine\DBAL\Connection;
12 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
13 use eZ\Publish\Core\Persistence\Legacy\Notification\Gateway;
14 use eZ\Publish\SPI\Persistence\Notification\CreateStruct;
15 use eZ\Publish\SPI\Persistence\Notification\Notification;
16 use PDO;
17
18 class DoctrineDatabase extends Gateway
19 {
20 const TABLE_NOTIFICATION = 'eznotification';
21 const COLUMN_ID = 'id';
22 const COLUMN_OWNER_ID = 'owner_id';
23 const COLUMN_IS_PENDING = 'is_pending';
24 const COLUMN_TYPE = 'type';
25 const COLUMN_CREATED = 'created';
26 const COLUMN_DATA = 'data';
27
29 private $connection;
30
34 public function __construct(Connection $connection)
35 {
36 $this->connection = $connection;
37 }
38
42 public function insert(CreateStruct $createStruct): int
43 {
44 $query = $this->connection->createQueryBuilder();
45 $query
46 ->insert(self::TABLE_NOTIFICATION)
47 ->values([
48 self::COLUMN_IS_PENDING => ':is_pending',
49 self::COLUMN_OWNER_ID => ':user_id',
50 self::COLUMN_CREATED => ':created',
51 self::COLUMN_TYPE => ':type',
52 self::COLUMN_DATA => ':data',
53 ])
54 ->setParameter(':is_pending', $createStruct->isPending, PDO::PARAM_BOOL)
55 ->setParameter(':user_id', $createStruct->ownerId, PDO::PARAM_INT)
56 ->setParameter(':created', $createStruct->created, PDO::PARAM_INT)
57 ->setParameter(':type', $createStruct->type, PDO::PARAM_STR)
58 ->setParameter(':data', json_encode($createStruct->data), PDO::PARAM_STR);
59
60 $query->execute();
61
62 return (int) $this->connection->lastInsertId();
63 }
64
68 public function getNotificationById(int $notificationId): array
69 {
70 $query = $this->connection->createQueryBuilder();
71 $query
72 ->select(...$this->getColumns())
73 ->from(self::TABLE_NOTIFICATION)
74 ->where($query->expr()->eq(self::COLUMN_ID, ':id'));
75
76 $query->setParameter(':id', $notificationId, PDO::PARAM_INT);
77
78 return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
79 }
80
84 public function updateNotification(Notification $notification): void
85 {
86 if (!isset($notification->id) || !is_numeric($notification->id)) {
87 throw new InvalidArgumentException(self::COLUMN_ID, 'Cannot update Notification');
88 }
89
90 $query = $this->connection->createQueryBuilder();
91
92 $query
93 ->update(self::TABLE_NOTIFICATION)
94 ->set(self::COLUMN_IS_PENDING, ':is_pending')
95 ->where($query->expr()->eq(self::COLUMN_ID, ':id'))
96 ->setParameter(':is_pending', $notification->isPending, PDO::PARAM_BOOL)
97 ->setParameter(':id', $notification->id, PDO::PARAM_INT);
98
99 $query->execute();
100 }
101
105 public function countUserNotifications(int $userId): int
106 {
107 $query = $this->connection->createQueryBuilder();
108 $query
109 ->select('COUNT(' . self::COLUMN_ID . ')')
110 ->from(self::TABLE_NOTIFICATION)
111 ->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
112 ->setParameter(':user_id', $userId, PDO::PARAM_INT);
113
114 return (int)$query->execute()->fetchColumn();
115 }
116
120 public function countUserPendingNotifications(int $userId): int
121 {
122 $query = $this->connection->createQueryBuilder();
123 $expr = $query->expr();
124 $query
125 ->select('COUNT(' . self::COLUMN_ID . ')')
126 ->from(self::TABLE_NOTIFICATION)
127 ->where($expr->eq(self::COLUMN_OWNER_ID, ':user_id'))
128 ->andWhere($expr->eq(self::COLUMN_IS_PENDING, ':is_pending'))
129 ->setParameter(':is_pending', true, PDO::PARAM_BOOL)
130 ->setParameter(':user_id', $userId, PDO::PARAM_INT);
131
132 return (int)$query->execute()->fetchColumn();
133 }
134
138 public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array
139 {
140 $query = $this->connection->createQueryBuilder();
141 $query
142 ->select(...$this->getColumns())
143 ->from(self::TABLE_NOTIFICATION)
144 ->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
145 ->setFirstResult($offset);
146
147 if ($limit > 0) {
148 $query->setMaxResults($limit);
149 }
150
151 $query->orderBy(self::COLUMN_ID, 'DESC');
152 $query->setParameter(':user_id', $userId, PDO::PARAM_INT);
153
154 return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
155 }
156
160 public function delete(int $notificationId): void
161 {
162 $query = $this->connection->createQueryBuilder();
163 $query
164 ->delete(self::TABLE_NOTIFICATION)
165 ->where($query->expr()->eq(self::COLUMN_ID, ':id'))
166 ->setParameter(':id', $notificationId, PDO::PARAM_INT);
167
168 $query->execute();
169 }
170
174 private function getColumns(): array
175 {
176 return [
177 self::COLUMN_ID,
178 self::COLUMN_OWNER_ID,
179 self::COLUMN_IS_PENDING,
180 self::COLUMN_TYPE,
181 self::COLUMN_CREATED,
182 self::COLUMN_DATA,
183 ];
184 }
185 }
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:19
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\getColumns
getColumns()
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:174
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\__construct
__construct(Connection $connection)
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:34
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_CREATED
const COLUMN_CREATED
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:25
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\insert
insert(CreateStruct $createStruct)
{Store Notification ValueObject in persistent storage.int}
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:42
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_IS_PENDING
const COLUMN_IS_PENDING
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:23
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\updateNotification
updateNotification(Notification $notification)
{Update Notification ValueObject in persistent storage.There's no edit feature but it's essential to ...
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:84
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\getNotificationById
getNotificationById(int $notificationId)
{Get Notification by its id.array}
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:68
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\countUserNotifications
countUserNotifications(int $userId)
{int}
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:105
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_OWNER_ID
const COLUMN_OWNER_ID
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:22
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\loadUserNotifications
loadUserNotifications(int $userId, int $offset=0, int $limit=-1)
{array}
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:138
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_TYPE
const COLUMN_TYPE
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:24
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\countUserPendingNotifications
countUserPendingNotifications(int $userId)
{Count users unread Notifications.int}
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:120
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_DATA
const COLUMN_DATA
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:26
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\$connection
$connection
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:29
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\COLUMN_ID
const COLUMN_ID
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:21
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway\DoctrineDatabase\TABLE_NOTIFICATION
const TABLE_NOTIFICATION
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:20
eZ\Publish\Core\Persistence\Legacy\Notification\Gateway
Definition: Persistence/Legacy/Notification/Gateway.php:15
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\Core\Persistence\Legacy\Notification\Gateway
Definition: Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php:9
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.