|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Uecode\Bundle\QPushBundle\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use Monolog\Logger; |
|
7
|
|
|
use Uecode\Bundle\QPushBundle\Message\Message; |
|
8
|
|
|
use Uecode\Bundle\QPushBundle\Entity\DoctrineMessage; |
|
9
|
|
|
|
|
10
|
|
|
class DoctrineProvider extends AbstractProvider |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
protected $em; |
|
14
|
|
|
protected $repository; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor for Provider classes |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $name Name of the Queue the provider is for |
|
20
|
|
|
* @param array $options An array of configuration options for the Queue |
|
21
|
|
|
* @param mixed $client A Queue Client for the provider |
|
22
|
|
|
* @param Cache $cache An instance of Doctrine\Common\Cache\Cache |
|
23
|
|
|
* @param Logger $logger An instance of Symfony\Bridge\Mongolog\Logger |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($name, array $options, $client, Cache $cache, Logger $logger) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->name = $name; |
|
28
|
|
|
$this->options = $options; |
|
29
|
|
|
$this->cache = $cache; |
|
30
|
|
|
$this->logger = $logger; |
|
31
|
|
|
$this->em = $client; |
|
32
|
|
|
$this->repository = $this->em->getRepository('Uecode\Bundle\QPushBundle\Entity\DoctrineMessage'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns the name of the Queue that this Provider is for |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getName() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->name; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns the Queue Provider name |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getProvider() |
|
51
|
|
|
{ |
|
52
|
|
|
return 'Doctrine'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the Provider's Configuration Options |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getOptions() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->options; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the Cache service |
|
67
|
|
|
* |
|
68
|
|
|
* @return Cache |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCache() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->cache; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the Logger service |
|
77
|
|
|
* |
|
78
|
|
|
* @return Logger |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getLogger() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->logger; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get repository |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getRepository() |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$this->repository) { |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
return $this->repository; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Creates the Queue |
|
100
|
|
|
* |
|
101
|
|
|
* All Create methods are idempotent, if the resource exists, the current ARN |
|
102
|
|
|
* will be returned |
|
103
|
|
|
*/ |
|
104
|
|
|
public function create() |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Publishes a message to the Queue |
|
111
|
|
|
* |
|
112
|
|
|
* This method should return a string MessageId or Response |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $message The message to queue |
|
115
|
|
|
* @param array $options An array of options that override the queue defaults |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function publish(array $message, array $options = []) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!$this->em) { |
|
122
|
|
|
return ''; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$doctrineMessage = new DoctrineMessage(); |
|
126
|
|
|
$doctrineMessage->setQueue($this->name) |
|
127
|
|
|
->setDelivered(false) |
|
128
|
|
|
->setMessage($message) |
|
129
|
|
|
->setLength(strlen(serialize($message))); |
|
130
|
|
|
|
|
131
|
|
|
$this->em->persist($doctrineMessage); |
|
132
|
|
|
$this->em->flush(); |
|
133
|
|
|
|
|
134
|
|
|
return (string) $doctrineMessage->getId(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Polls the Queue for Messages |
|
139
|
|
|
* |
|
140
|
|
|
* Depending on the Provider, this method may keep the connection open for |
|
141
|
|
|
* a configurable amount of time, to allow for long polling. In most cases, |
|
142
|
|
|
* this method is not meant to be used to long poll indefinitely, but should |
|
143
|
|
|
* return in reasonable amount of time |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $options An array of options that override the queue defaults |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
public function receive(array $options = []) |
|
150
|
|
|
{ |
|
151
|
|
|
if (!$this->em) { |
|
152
|
|
|
return []; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$doctrineMessages = $this->repository->findBy( |
|
156
|
|
|
[ |
|
157
|
|
|
'delivered' => false, |
|
158
|
|
|
'queue' => $this->name |
|
159
|
|
|
], [ |
|
160
|
|
|
'id' => 'ASC' |
|
161
|
|
|
] |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
$messages = []; |
|
165
|
|
|
foreach ($doctrineMessages as $doctrineMessage) { |
|
166
|
|
|
$messages[] = new Message($doctrineMessage->getId(), $doctrineMessage->getMessage(), []); |
|
167
|
|
|
$doctrineMessage->setDelivered(true); |
|
168
|
|
|
} |
|
169
|
|
|
$this->em->flush(); |
|
170
|
|
|
|
|
171
|
|
|
return $messages; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Deletes the Queue Message |
|
176
|
|
|
* |
|
177
|
|
|
* @param mixed $id A message identifier or resource |
|
178
|
|
|
*/ |
|
179
|
|
|
public function delete($id) |
|
180
|
|
|
{ |
|
181
|
|
|
$doctrineMessage = $this->repository->findById($id); |
|
182
|
|
|
$doctrineMessage->setDelivered(true); |
|
183
|
|
|
$this->em->flush(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Destroys a Queue and clears any Queue related Cache |
|
188
|
|
|
* |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
public function destroy() |
|
192
|
|
|
{ |
|
193
|
|
|
$qb = $this->repository->createQueryBuilder('dm'); |
|
194
|
|
|
$qb->delete(); |
|
195
|
|
|
$qb->where('dm.queue = :queue'); |
|
196
|
|
|
$qb->setParameter('queue', $this->name); |
|
197
|
|
|
$qb->getQuery()->getResult(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns a specific message |
|
202
|
|
|
* |
|
203
|
|
|
* @param integer $id |
|
204
|
|
|
* |
|
205
|
|
|
* @return Message |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getById($id) |
|
208
|
|
|
{ |
|
209
|
|
|
return find($id); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/* |
|
213
|
|
|
* Returns a query of the message queue |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $contains |
|
216
|
|
|
* @param DateTime $from |
|
217
|
|
|
* @param DateTime $to |
|
218
|
|
|
* |
|
219
|
|
|
* @return Query |
|
220
|
|
|
*/ |
|
221
|
|
|
|
|
222
|
|
|
public function findBy($contains = null, $from = null, $to = null) |
|
223
|
|
|
{ |
|
224
|
|
|
|
|
225
|
|
|
$qb = $this->repository->createQueryBuilder('p'); |
|
226
|
|
|
$qb->select('p'); |
|
227
|
|
|
$qb->where('p.queue = :queue'); |
|
228
|
|
|
$qb->setParameter('queue', $this->name); |
|
229
|
|
|
|
|
230
|
|
|
if ($contains !== null) { |
|
231
|
|
|
$qb->addWhere('p.message like %:contains%'); |
|
232
|
|
|
$qb->setParameter('contains', $contains); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
if ($from) { |
|
236
|
|
|
$qb->addWhere('p.created > %:from%'); |
|
237
|
|
|
$qb->setParameter('from', $from); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
if ($to) { |
|
241
|
|
|
$qb->addWhere('p.created < %:to%'); |
|
242
|
|
|
$qb->setParameter('to', $to); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $qb->getQuery(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|