-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdoctrine2.php
More file actions
349 lines (304 loc) · 10.6 KB
/
doctrine2.php
File metadata and controls
349 lines (304 loc) · 10.6 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* +----------------------------------------------------------------------+
* | PEAR :: Mail :: Queue :: Doctrine2 Container |
* +----------------------------------------------------------------------+
* | Copyright (c) 2012 Leander Damme |
* +----------------------------------------------------------------------+
* | All rights reserved. |
* | |
* | Redistribution and use in source and binary forms, with or without |
* | modification, are permitted provided that the following conditions |
* | are met: |
* | |
* | * Redistributions of source code must retain the above copyright |
* | notice, this list of conditions and the following disclaimer. |
* | * Redistributions in binary form must reproduce the above copyright |
* | notice, this list of conditions and the following disclaimer in |
* | the documentation and/or other materials provided with the |
* | distribution. |
* | * The names of its contributors may be used to endorse or promote |
* | products derived from this software without specific prior written |
* | permission. |
* | |
* | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
* | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
* | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
* | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
* | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
* | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
* | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
* | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
* | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
* | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
* | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
* | POSSIBILITY OF SUCH DAMAGE. |
* +----------------------------------------------------------------------+
* | Author: Leander Damme <leander at wesrc.com> |
* +----------------------------------------------------------------------+
*
* PHP Version 5.3
*
* @category Mail
* @package Mail_Queue
* @author Leander Damme <leander@wesrc.com>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @link http://pear.php.net/package/Mail_Queue
*/
namespace PEAR2\Mail\Queue\Container;
use PEAR2\Mail\Queue\Container;
use PEAR2\Mail\Queue\Exception;
use PEAR2\Mail\Queue;
use PEAR2\Mail\Queue\Body;
use PEAR2\Mail\Queue\Entity\Mail;
use PEAR2\Mail\Queue\Entity\Repository\MailRepository;
/**
* Storage driver for fetching mail queue data with Doctrine2
*
* This storage driver can use all databases which are supported
* by the DoctrineORM abstraction layer.
*
* @category Mail
* @package Mail_Queue
* @author Leander Damme <leander@wesrc.com>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Mail_Queue
*/
class Doctrine2 extends Container
{
var $errorMsg = 'doctrine failed: "%s", %s';
/**
* @var Doctrine\ORM\EntityManager
*/
protected $em;
/**
* @var Mail Mail
*/
protected $entity;
/**
* @var MailRepository MailRepository
*/
protected $repo;
/**
* @var
*/
protected $config;
/**
* Constructor
*
* Mail_Queue_Container_doctrine2()
*
* @param array $options An associative array of connection option.
*
*/
public function __construct(array $options)
{
if (!isset($options['doctrine_em'])) {
throw new Exception(
'No doctrine entity manager specified!',
Queue::ERROR_NO_OPTIONS
);
}
$this->em = $options['doctrine_em'];
$this->setOption();
$this->repo = $this->em->getRepository('PEAR2\Mail\Queue\Entity\Mail');
}
/**
* Get the Doctrine EntityManager
*
* @return \Doctrine\ORM\EntityManager
*/
public function getEntityManager()
{
if (isset($this->em)) {
return $this->em;
}
$this->init();
return $this->em;
}
/**
* Get the Doctrine EntityManager
*
* @param Doctrine\ORM\EntityManager $em EntityManager
*
* @return \PEAR2\Mail\Queue\Container\doctrine2
*/
public function setEntityManager($em)
{
$this->em = $em;
return $this;
}
/**
* Preload mail to queue.
*
* @return true
* @throws Exception
*/
protected function _preload()
{
$queueCollection = $this->repo->preload($this->limit, $this->offset,$this->try);
$this->_last_item = 0;
$this->queue_data = array(); //reset buffer
foreach($queueCollection as $mail){
$delete_after_send = (bool) $mail->__get('deleteAfterSend');
$this->queue_data[$this->_last_item] = new Body(
$mail->getId(),
$mail->getCreateTime(),
$mail->getTimeToSend(),
$mail->getSentTime(),
$mail->getIdUser(),
$mail->getIp(),
$mail->getSender(),
$this->_isSerialized($mail->getRecipient()) ? unserialize($mail->getRecipient()) : $mail->getRecipient(),
unserialize($mail->getHeaders()),
unserialize($mail->getBody()),
$delete_after_send,
$mail->getTrySent()
);
$this->_last_item++;
}
return true;
}
/**
* Put new mail in queue.
*
* Mail_Queue_Container::put()
*
* @param string $time_to_send When mail have to be send
* @param integer $id_user Sender id
* @param string $ip Sender ip
* @param string $from Sender e-mail
* @param string $to Reciepient e-mail
* @param string $hdrs Mail headers (in RFC)
* @param string $body Mail body (in RFC)
* @param bool $delete_after_send (not sure wether in RFC)
*
* @return bool True on success
**/
public function put($time_to_send, $id_user, $ip, $from, $to, $hdrs, $body, $delete_after_send)
{
$queueRecord = new Mail();
$queueRecord
->__set('createTime', new \DateTime())
->__set('timeToSend', new \DateTime($time_to_send))
->__set('idUser', $id_user)
->__set('ip', $ip)
->__set('sender', $from)
->__set('recipient', $to)
->__set('headers', $hdrs)
->__set('body', $body)
->__set('deleteAfterSend', ($delete_after_send ? 1 : 0));
$this->em->persist($queueRecord);
$this->em->flush();
return $queueRecord->__get('id');
}
/**
* Check how many times mail was sent.
*
* @param Body $mail object
*
* @return mixed Integer or false if error.
*/
public function countSend(Body $mail)
{
$count = $mail->_try();
$mailRecord = $this->repo->find($mail->getId());
if (null == $mailRecord) {
throw new Exception(
sprintf($this->errorMsg, $mail->getId(), 'no message with id'),
Queue::ERROR_QUERY_FAILED
);
}
$mailRecord->__set('trySent',$count);
$this->em->persist($mailRecord);
$this->em->flush();
return $count;
// TODO: Implement countSend() method.
}
/**
* Set mail as already sent.
*
* @param Body $mail object
*
* @return bool
*/
public function setAsSent(Body $mail)
{
$mailRecord = $this->repo->find($mail->getId());
if (null == $mailRecord) {
throw new Exception(
sprintf($this->errorMsg, $mail->getId(), 'no message with id'),
Queue::ERROR_QUERY_FAILED
);
}
$now = new \DateTime();
$mailRecord->__set('sentTime',$now);
$this->em->persist($mailRecord);
$this->em->flush();
return true;
}
/**
* Return mail by id $id (bypass mail_queue)
*
* @param integer $id Mail ID
*
* @return mixed Mail object or false on error.
*/
public function getMailById($id)
{
$mailRecord = $this->repo->find($id);
if (null == $mailRecord) {
throw new Exception(
sprintf($this->errorMsg, $id, 'no message with id'),
Queue::ERROR_QUERY_FAILED
);
}
return new Body(
$mailRecord->getId(),
$mailRecord->getCreateTime(),
$mailRecord->getTimeToSend(),
$mailRecord->getTimeToSend(),
$mailRecord->getIdUser(),
$mailRecord->getIp(),
$mailRecord->getSender(),
$this->_isSerialized($mailRecord->getRecipient()) ? unserialize($mailRecord->getRecipient()) : $mailRecord->getRecipient(),
unserialize($mailRecord->getHeaders()),
unserialize($mailRecord->getBody()),
$mailRecord->getDeleteAfterSend(),
$mailRecord->getTrySent()
);
}
/**
* Return the number of emails currently in the queue.
*
* @return int
* @throws Exception
*/
public function getQueueCount()
{
$count = $this->repo->getQueueCount();
return (int) $count;
// TODO: Implement getQueueCount() method.
}
/**
* Remove from queue mail with $id identifier.
*
* @param integer $id Mail ID
*
* @return bool True on success ale false.
*/
public function deleteMail($id)
{
$mailRecord = $this->repo->find($id);
if (null == $mailRecord) {
throw new Exception(
sprintf($this->errorMsg, $id, 'no message with id'),
Queue::ERROR_QUERY_FAILED
);
}
$this->em->remove($mailRecord);
$this->em->flush();
return true;
}
}