@@ -25,20 +25,13 @@ class DoctrineDatabase extends Gateway
25
25
public const COLUMN_CREATED = 'created ' ;
26
26
public const COLUMN_DATA = 'data ' ;
27
27
28
- /** @var \Doctrine\DBAL\Connection */
29
- private $ connection ;
28
+ private Connection $ connection ;
30
29
31
- /**
32
- * @param \Doctrine\DBAL\Connection $connection
33
- */
34
30
public function __construct (Connection $ connection )
35
31
{
36
32
$ this ->connection = $ connection ;
37
33
}
38
34
39
- /**
40
- * {@inheritdoc}
41
- */
42
35
public function insert (CreateStruct $ createStruct ): int
43
36
{
44
37
$ query = $ this ->connection ->createQueryBuilder ();
@@ -62,9 +55,6 @@ public function insert(CreateStruct $createStruct): int
62
55
return (int ) $ this ->connection ->lastInsertId ();
63
56
}
64
57
65
- /**
66
- * {@inheritdoc}
67
- */
68
58
public function getNotificationById (int $ notificationId ): array
69
59
{
70
60
$ query = $ this ->connection ->createQueryBuilder ();
@@ -78,9 +68,6 @@ public function getNotificationById(int $notificationId): array
78
68
return $ query ->execute ()->fetchAll (PDO ::FETCH_ASSOC );
79
69
}
80
70
81
- /**
82
- * {@inheritdoc}
83
- */
84
71
public function updateNotification (Notification $ notification ): void
85
72
{
86
73
if (!isset ($ notification ->id ) || !is_numeric ($ notification ->id )) {
@@ -99,24 +86,52 @@ public function updateNotification(Notification $notification): void
99
86
$ query ->execute ();
100
87
}
101
88
102
- /**
103
- * {@inheritdoc}
104
- */
105
- public function countUserNotifications (int $ userId ): int
89
+ private function applyFilters ($ queryBuilder , array $ query ): void
106
90
{
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
109
123
->select ('COUNT( ' . self ::COLUMN_ID . ') ' )
110
124
->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 ' ))
112
126
->setParameter (':user_id ' , $ userId , PDO ::PARAM_INT );
113
127
114
- return (int )$ query ->execute ()->fetchColumn ();
128
+ if (!empty ($ query )) {
129
+ $ this ->applyFilters ($ queryBuilder , $ query );
130
+ }
131
+
132
+ return (int )$ queryBuilder ->execute ()->fetchColumn ();
115
133
}
116
134
117
- /**
118
- * {@inheritdoc}
119
- */
120
135
public function countUserPendingNotifications (int $ userId ): int
121
136
{
122
137
$ query = $ this ->connection ->createQueryBuilder ();
@@ -132,31 +147,29 @@ public function countUserPendingNotifications(int $userId): int
132
147
return (int )$ query ->execute ()->fetchColumn ();
133
148
}
134
149
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
139
151
{
140
- $ query = $ this ->connection ->createQueryBuilder ();
141
- $ query
152
+ $ queryBuilder = $ this ->connection ->createQueryBuilder ();
153
+ $ queryBuilder
142
154
->select (...$ this ->getColumns ())
143
155
->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 ' ))
145
157
->setFirstResult ($ offset );
146
158
159
+ if (!empty ($ query )) {
160
+ $ this ->applyFilters ($ queryBuilder , $ query );
161
+ }
162
+
147
163
if ($ limit > 0 ) {
148
- $ query ->setMaxResults ($ limit );
164
+ $ queryBuilder ->setMaxResults ($ limit );
149
165
}
150
166
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 );
153
169
154
- return $ query ->execute ()->fetchAll (PDO ::FETCH_ASSOC );
170
+ return $ queryBuilder ->execute ()->fetchAll (PDO ::FETCH_ASSOC );
155
171
}
156
172
157
- /**
158
- * {@inheritdoc}
159
- */
160
173
public function delete (int $ notificationId ): void
161
174
{
162
175
$ query = $ this ->connection ->createQueryBuilder ();
@@ -168,9 +181,6 @@ public function delete(int $notificationId): void
168
181
$ query ->execute ();
169
182
}
170
183
171
- /**
172
- * @return array
173
- */
174
184
private function getColumns (): array
175
185
{
176
186
return [
0 commit comments