Skip to content

Commit 3b8e815

Browse files
committed
IBX-9060: Added $query parameter for filtering notifications count and loading
1 parent 793ee0e commit 3b8e815

File tree

14 files changed

+156
-167
lines changed

14 files changed

+156
-167
lines changed

src/contracts/Persistence/Notification/Handler.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,11 @@ public function countPendingNotifications(int $ownerId): int;
5151
public function getNotificationById(int $notificationId): Notification;
5252

5353
/**
54-
* @param int $userId
55-
* @param int $offset
56-
* @param int $limit
57-
*
5854
* @return \Ibexa\Contracts\Core\Persistence\Notification\Notification[]
5955
*/
60-
public function loadUserNotifications(int $userId, int $offset, int $limit): array;
56+
public function loadUserNotifications(int $userId, int $offset, int $limit, array $query = []): array;
6157

62-
/**
63-
* @param int $currentUserId
64-
*
65-
* @return int
66-
*/
67-
public function countNotifications(int $currentUserId): int;
58+
public function countNotifications(int $currentUserId, array $query = []): int;
6859

6960
/**
7061
* @param \Ibexa\Contracts\Core\Repository\Values\Notification\Notification $notification

src/contracts/Repository/Decorator/NotificationServiceDecorator.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public function __construct(NotificationService $innerService)
2525

2626
public function loadNotifications(
2727
int $offset,
28-
int $limit
28+
int $limit,
29+
array $query = []
2930
): NotificationList {
30-
return $this->innerService->loadNotifications($offset, $limit);
31+
return $this->innerService->loadNotifications($offset, $limit, $query);
3132
}
3233

3334
public function getNotification(int $notificationId): Notification
@@ -50,9 +51,9 @@ public function getPendingNotificationCount(): int
5051
return $this->innerService->getPendingNotificationCount();
5152
}
5253

53-
public function getNotificationCount(): int
54+
public function getNotificationCount(array $query = []): int
5455
{
55-
return $this->innerService->getNotificationCount();
56+
return $this->innerService->getNotificationCount($query);
5657
}
5758

5859
public function createNotification(CreateStruct $createStruct): Notification

src/contracts/Repository/NotificationService.php

+2-15
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@
1818
*/
1919
interface NotificationService
2020
{
21-
/**
22-
* Get currently logged user notifications.
23-
*
24-
* @param int $offset the start offset for paging
25-
* @param int $limit the number of notifications returned
26-
*
27-
* @return \Ibexa\Contracts\Core\Repository\Values\Notification\NotificationList
28-
*/
29-
public function loadNotifications(int $offset, int $limit): NotificationList;
21+
public function loadNotifications(int $offset, int $limit, array $query = []): NotificationList;
3022

3123
/**
3224
* Load single notification (by ID).
@@ -64,12 +56,7 @@ public function markNotificationAsUnread(Notification $notification): void;
6456
*/
6557
public function getPendingNotificationCount(): int;
6658

67-
/**
68-
* Get count of total users notifications.
69-
*
70-
* @return int
71-
*/
72-
public function getNotificationCount(): int;
59+
public function getNotificationCount(array $query = []): int;
7360

7461
/**
7562
* Creates a new notification.

src/lib/Persistence/Cache/NotificationHandler.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ public function countPendingNotifications(int $ownerId): int
9898
return $count;
9999
}
100100

101-
/**
102-
* {@inheritdoc}
103-
*/
104-
public function countNotifications(int $ownerId): int
101+
public function countNotifications(int $ownerId, array $query = []): int
105102
{
103+
$cacheKeyParams = [$ownerId];
104+
if (!empty($query)) {
105+
$cacheKeyParams[] = json_encode($query);
106+
}
107+
106108
$cacheItem = $this->cache->getItem(
107-
$this->cacheIdentifierGenerator->generateKey(self::NOTIFICATION_COUNT_IDENTIFIER, [$ownerId], true)
109+
$this->cacheIdentifierGenerator->generateKey(self::NOTIFICATION_COUNT_IDENTIFIER, $cacheKeyParams, true)
108110
);
109111

110112
$count = $cacheItem->get();
@@ -114,9 +116,10 @@ public function countNotifications(int $ownerId): int
114116

115117
$this->logger->logCall(__METHOD__, [
116118
'ownerId' => $ownerId,
119+
'query' => $query,
117120
]);
118121

119-
$count = $this->persistenceHandler->notificationHandler()->countNotifications($ownerId);
122+
$count = $this->persistenceHandler->notificationHandler()->countNotifications($ownerId, $query);
120123

121124
$cacheItem->set($count);
122125
$this->cache->save($cacheItem);
@@ -150,18 +153,16 @@ public function getNotificationById(int $notificationId): Notification
150153
return $notification;
151154
}
152155

153-
/**
154-
* {@inheritdoc}
155-
*/
156-
public function loadUserNotifications(int $userId, int $offset, int $limit): array
156+
public function loadUserNotifications(int $userId, int $offset, int $limit, array $query = []): array
157157
{
158158
$this->logger->logCall(__METHOD__, [
159159
'ownerId' => $userId,
160160
'offset' => $offset,
161161
'limit' => $limit,
162+
'query' => $query,
162163
]);
163164

164-
return $this->persistenceHandler->notificationHandler()->loadUserNotifications($userId, $offset, $limit);
165+
return $this->persistenceHandler->notificationHandler()->loadUserNotifications($userId, $offset, $limit, $query);
165166
}
166167
}
167168

src/lib/Persistence/Legacy/Notification/Gateway.php

+7-14
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ abstract public function getNotificationById(int $notificationId): array;
4141
*/
4242
abstract public function updateNotification(Notification $notification): void;
4343

44-
/**
45-
* @param int $userId
46-
*
47-
* @return int
48-
*/
49-
abstract public function countUserNotifications(int $userId): int;
44+
abstract public function countUserNotifications(int $userId, array $query = []): int;
5045

5146
/**
5247
* Count users unread Notifications.
@@ -57,14 +52,12 @@ abstract public function countUserNotifications(int $userId): int;
5752
*/
5853
abstract public function countUserPendingNotifications(int $userId): int;
5954

60-
/**
61-
* @param int $userId
62-
* @param int $offset
63-
* @param int $limit
64-
*
65-
* @return array
66-
*/
67-
abstract public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array;
55+
abstract public function loadUserNotifications(
56+
int $userId,
57+
int $offset = 0,
58+
int $limit = -1,
59+
array $query = []
60+
): array;
6861

6962
/**
7063
* @param int $notificationId

src/lib/Persistence/Legacy/Notification/Gateway/DoctrineDatabase.php

+52-42
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,13 @@ class DoctrineDatabase extends Gateway
2525
public const COLUMN_CREATED = 'created';
2626
public const COLUMN_DATA = 'data';
2727

28-
/** @var \Doctrine\DBAL\Connection */
29-
private $connection;
28+
private Connection $connection;
3029

31-
/**
32-
* @param \Doctrine\DBAL\Connection $connection
33-
*/
3430
public function __construct(Connection $connection)
3531
{
3632
$this->connection = $connection;
3733
}
3834

39-
/**
40-
* {@inheritdoc}
41-
*/
4235
public function insert(CreateStruct $createStruct): int
4336
{
4437
$query = $this->connection->createQueryBuilder();
@@ -62,9 +55,6 @@ public function insert(CreateStruct $createStruct): int
6255
return (int) $this->connection->lastInsertId();
6356
}
6457

65-
/**
66-
* {@inheritdoc}
67-
*/
6858
public function getNotificationById(int $notificationId): array
6959
{
7060
$query = $this->connection->createQueryBuilder();
@@ -78,9 +68,6 @@ public function getNotificationById(int $notificationId): array
7868
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
7969
}
8070

81-
/**
82-
* {@inheritdoc}
83-
*/
8471
public function updateNotification(Notification $notification): void
8572
{
8673
if (!isset($notification->id) || !is_numeric($notification->id)) {
@@ -99,24 +86,52 @@ public function updateNotification(Notification $notification): void
9986
$query->execute();
10087
}
10188

102-
/**
103-
* {@inheritdoc}
104-
*/
105-
public function countUserNotifications(int $userId): int
89+
private function applyFilters($queryBuilder, array $query): void
10690
{
107-
$query = $this->connection->createQueryBuilder();
108-
$query
91+
if (isset($query['type'])) {
92+
$queryBuilder
93+
->andWhere($queryBuilder->expr()->like(self::COLUMN_TYPE, ':type'))
94+
->setParameter(':type', '%' . $query['type'] . '%');
95+
}
96+
97+
if (isset($query['status'])) {
98+
if (in_array('read', $query['status'], true)) {
99+
$queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':status_read'))
100+
->setParameter(':status_read', false); // 'read' corresponds to is_pending = false
101+
}
102+
if (in_array('unread', $query['status'], true)) {
103+
$queryBuilder->andWhere($queryBuilder->expr()->eq(self::COLUMN_IS_PENDING, ':status_unread'))
104+
->setParameter(':status_unread', true); // 'unread' corresponds to is_pending = true
105+
}
106+
}
107+
108+
if (isset($query['created_from'])) {
109+
$queryBuilder->andWhere($queryBuilder->expr()->gte(self::COLUMN_CREATED, ':created_from'))
110+
->setParameter(':created_from', $query['created_from'], PDO::PARAM_INT);
111+
}
112+
113+
if (isset($query['created_to'])) {
114+
$queryBuilder->andWhere($queryBuilder->expr()->lte(self::COLUMN_CREATED, ':created_to'))
115+
->setParameter(':created_to', $query['created_to'], PDO::PARAM_INT);
116+
}
117+
}
118+
119+
public function countUserNotifications(int $userId, array $query = []): int
120+
{
121+
$queryBuilder = $this->connection->createQueryBuilder();
122+
$queryBuilder
109123
->select('COUNT(' . self::COLUMN_ID . ')')
110124
->from(self::TABLE_NOTIFICATION)
111-
->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
125+
->where($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
112126
->setParameter(':user_id', $userId, PDO::PARAM_INT);
113127

114-
return (int)$query->execute()->fetchColumn();
128+
if (!empty($query)) {
129+
$this->applyFilters($queryBuilder, $query);
130+
}
131+
132+
return (int)$queryBuilder->execute()->fetchColumn();
115133
}
116134

117-
/**
118-
* {@inheritdoc}
119-
*/
120135
public function countUserPendingNotifications(int $userId): int
121136
{
122137
$query = $this->connection->createQueryBuilder();
@@ -132,31 +147,29 @@ public function countUserPendingNotifications(int $userId): int
132147
return (int)$query->execute()->fetchColumn();
133148
}
134149

135-
/**
136-
* {@inheritdoc}
137-
*/
138-
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array
150+
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1, array $query = []): array
139151
{
140-
$query = $this->connection->createQueryBuilder();
141-
$query
152+
$queryBuilder = $this->connection->createQueryBuilder();
153+
$queryBuilder
142154
->select(...$this->getColumns())
143155
->from(self::TABLE_NOTIFICATION)
144-
->where($query->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
156+
->where($queryBuilder->expr()->eq(self::COLUMN_OWNER_ID, ':user_id'))
145157
->setFirstResult($offset);
146158

159+
if (!empty($query)) {
160+
$this->applyFilters($queryBuilder, $query);
161+
}
162+
147163
if ($limit > 0) {
148-
$query->setMaxResults($limit);
164+
$queryBuilder->setMaxResults($limit);
149165
}
150166

151-
$query->orderBy(self::COLUMN_ID, 'DESC');
152-
$query->setParameter(':user_id', $userId, PDO::PARAM_INT);
167+
$queryBuilder->orderBy(self::COLUMN_ID, 'DESC');
168+
$queryBuilder->setParameter(':user_id', $userId, PDO::PARAM_INT);
153169

154-
return $query->execute()->fetchAll(PDO::FETCH_ASSOC);
170+
return $queryBuilder->execute()->fetchAll(PDO::FETCH_ASSOC);
155171
}
156172

157-
/**
158-
* {@inheritdoc}
159-
*/
160173
public function delete(int $notificationId): void
161174
{
162175
$query = $this->connection->createQueryBuilder();
@@ -168,9 +181,6 @@ public function delete(int $notificationId): void
168181
$query->execute();
169182
}
170183

171-
/**
172-
* @return array
173-
*/
174184
private function getColumns(): array
175185
{
176186
return [

src/lib/Persistence/Legacy/Notification/Gateway/ExceptionConversion.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public function updateNotification(Notification $notification): void
5252
}
5353
}
5454

55-
public function countUserNotifications(int $userId): int
55+
public function countUserNotifications(int $userId, array $query = []): int
5656
{
5757
try {
58-
return $this->innerGateway->countUserNotifications($userId);
58+
return $this->innerGateway->countUserNotifications($userId, $query);
5959
} catch (DBALException | PDOException $e) {
6060
throw DatabaseException::wrap($e);
6161
}
@@ -70,10 +70,10 @@ public function countUserPendingNotifications(int $userId): int
7070
}
7171
}
7272

73-
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1): array
73+
public function loadUserNotifications(int $userId, int $offset = 0, int $limit = -1, array $query = []): array
7474
{
7575
try {
76-
return $this->innerGateway->loadUserNotifications($userId, $offset, $limit);
76+
return $this->innerGateway->loadUserNotifications($userId, $offset, $limit, $query);
7777
} catch (DBALException | PDOException $e) {
7878
throw DatabaseException::wrap($e);
7979
}

src/lib/Persistence/Legacy/Notification/Handler.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,18 @@ public function updateNotification(APINotification $apiNotification, UpdateStruc
8989
return $this->getNotificationById($notification->id);
9090
}
9191

92-
/**
93-
* {@inheritdoc}
94-
*/
95-
public function countNotifications(int $userId): int
92+
public function countNotifications(int $userId, array $query = []): int
9693
{
97-
return $this->gateway->countUserNotifications($userId);
94+
return $this->gateway->countUserNotifications($userId, $query);
9895
}
9996

10097
/**
10198
* {@inheritdoc}
10299
*/
103-
public function loadUserNotifications(int $userId, int $offset, int $limit): array
100+
public function loadUserNotifications(int $userId, int $offset, int $limit, array $query = []): array
104101
{
105102
return $this->mapper->extractNotificationsFromRows(
106-
$this->gateway->loadUserNotifications($userId, $offset, $limit)
103+
$this->gateway->loadUserNotifications($userId, $offset, $limit, $query)
107104
);
108105
}
109106

0 commit comments

Comments
 (0)