forked from fabarea/messenger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueRepository.php
More file actions
258 lines (223 loc) · 7.26 KB
/
Copy pathQueueRepository.php
File metadata and controls
258 lines (223 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace Fab\Messenger\Domain\Repository;
/*
* This file is part of the Fab/Messenger project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use Fab\Messenger\Utility\Algorithms;
use Fab\Messenger\Utility\TcaFieldsUtility;
use Random\RandomException;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
/**
* A repository for the Queue.
*/
class QueueRepository extends AbstractContentRepository
{
/**
* @var string
*/
protected string $tableName = 'tx_messenger_domain_model_queue';
protected QueryInterface $constraints;
public function __construct(private \TYPO3\CMS\Core\Database\ConnectionPool $connectionPool)
{
}
/**
* @throws Exception
* @throws DBALException
*/
public function findByUid(int $uid): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where(
$this->getQueryBuilder()
->expr()
->eq('uid', $this->getQueryBuilder()->expr()->literal($uid)),
);
$messages = $query->executeQuery()->fetchOne();
return is_array($messages) ? $messages : [];
}
/**
* @return QueryBuilder
*/
protected function getQueryBuilder(): QueryBuilder
{
/** @var ConnectionPool $connectionPool */
$connectionPool = $this->connectionPool;
return $connectionPool->getQueryBuilderForTable($this->tableName);
}
/**
* @throws Exception
* @throws DBALException
*/
public function findByUids(array $uids): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where($this->getQueryBuilder()->expr()->in('uid', $uids));
return $query->executeQuery()->fetchAllAssociative();
}
/**
* @throws DBALException
* @throws Exception
*/
public function findByUuid(string $uuid): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where(
$this->getQueryBuilder()
->expr()
->eq('uuid', $this->getQueryBuilder()->expr()->literal($uuid)),
);
$messages = $query->executeQuery()->fetchAllAssociative();
return is_array($messages) ? $messages : [];
}
public function findAll(): array
{
$query = $this->getQueryBuilder();
$query->select('*')->from($this->tableName);
return $query->executeQuery()->fetchAllAssociative();
}
/**
* @throws DBALException
* @throws Exception
*/
public function findOlderThanDays(int $days): array
{
$time = time() - $days * 86400;
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where('crdate < ' . $time);
$messages = $query->executeQuery()->fetchAllAssociative();
return is_array($messages) ? $messages : [];
}
/**
* @throws DBALException
*/
public function removeOlderThanDays(int $days): int
{
$time = time() - $days * 86400;
$query = $this->getQueryBuilder();
$query->delete($this->tableName)->where('crdate < ' . $time);
return $query->executeStatement();
}
/**
* @throws Exception
* @throws DBALException
*/
public function findByDemand(array $demand = [], array $orderings = [], int $offset = 0, int $limit = 0): array
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->select('*')->from($this->tableName);
$constraints = [];
if (!empty($demand['likes'])) {
foreach ($demand['likes'] as $field => $value) {
$constraints[] = $queryBuilder
->expr()
->like(
$field,
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards($value) . '%'),
);
}
$queryBuilder->where($queryBuilder->expr()->or(...$constraints));
}
if (!empty($demand['uids'])) {
$queryBuilder->andWhere($queryBuilder->expr()->in('uid', $demand['uids']));
}
if ($constraints) {
$queryBuilder->where($queryBuilder->expr()->or(...$constraints));
}
if ($orderings === []) {
$orderings = ['uid' => 'ASC'];
}
// We handle the sorting
$queryBuilder->addOrderBy(key($orderings), current($orderings));
if ($limit > 0) {
$queryBuilder->setMaxResults($limit);
}
return $queryBuilder->executeQuery()->fetchAllAssociative();
}
/**
* @throws RandomException
* @throws DBALException
*/
public function add(array $message): int
{
$values = [];
if (empty($message['uuid'])) {
$message['uuid'] = Algorithms::generateUUID();
}
// Make sure fields are allowed for this table.
$fields = TcaFieldsUtility::getFields($this->tableName);
foreach ($message as $fieldName => $value) {
if (in_array($fieldName, $fields, true) && is_string($value)) {
$values[$fieldName] = $value;
}
}
$query = $this->getQueryBuilder();
$query->insert($this->tableName)->values($values);
$result = $query->executeStatement();
if (!$result) {
throw new \RuntimeException('I could not save the message as "sent message"', 1_389_721_852);
}
return $result;
}
/**
* @throws Exception
* @throws DBALException
*/
public function findPendingMessages(int $limit): array
{
$query = $this->getQueryBuilder();
$query
->select('*')
->from($this->tableName)
->where('scheduled_distribution_time < ' . time())
->setMaxResults($limit);
$messages = $query->executeQuery()->fetchAllAssociative();
return is_array($messages) ? $messages : [];
}
/**
* @throws DBALException
*/
public function remove(array $message): int
{
$query = $this->getQueryBuilder();
$query->delete($this->tableName)->where('uid = ' . $message['uid']);
return $query->executeStatement();
}
/**
* @throws DBALException
*/
public function update(array $message): int
{
$query = $this->getQueryBuilder();
$query->update($this->tableName)->where('uid = ' . $message['uid']);
foreach ($message as $field => $value) {
$query->set($field, $value);
}
return $query->executeStatement();
}
public function deleteByUids(array $uids): int
{
$query = $this->getQueryBuilder();
$query->delete($this->tableName)->where($query->expr()->in('uid', $uids));
return $query->executeStatement();
}
}