1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @copyright 2015 LibreWorks contributors |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A Swift spool that uses legacy Mongo drivers (http://docs.php.net/manual/en/book.mongo.php) |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 LibreWorks contributors |
12
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
class Swift_MongoSpool extends Swift_ConfigurableSpool |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \MongoCollection The collection of queued emails |
18
|
|
|
*/ |
19
|
|
|
protected $collection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Creates a new MongoSpool |
23
|
|
|
* |
24
|
|
|
* @param \MongoCollection $collection The collection for messages |
25
|
|
|
*/ |
26
|
|
|
public function __construct(\MongoCollection $collection) |
27
|
|
|
{ |
28
|
|
|
$this->collection = $collection; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Tests if this Spool mechanism has started. |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
public function isStarted() |
37
|
|
|
{ |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Starts this Spool mechanism. |
43
|
|
|
*/ |
44
|
|
|
public function start() |
45
|
|
|
{ |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Stops this Spool mechanism. |
50
|
|
|
*/ |
51
|
|
|
public function stop() |
52
|
|
|
{ |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Queues a message. |
57
|
|
|
* |
58
|
|
|
* @param Swift_Mime_Message $message The message to store |
59
|
|
|
* @return bool |
60
|
|
|
* @throws Swift_IoException |
61
|
|
|
*/ |
62
|
|
|
public function queueMessage(Swift_Mime_Message $message) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$doc = array( |
66
|
|
|
'message' => new \MongoBinData(serialize($message), 0) |
67
|
|
|
); |
68
|
|
|
$this->collection->insert($doc); |
69
|
|
|
return true; |
70
|
|
|
} catch (\Exception $e) { |
71
|
|
|
throw new Swift_IoException("Could not write to database", 0, $e); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Execute a recovery if for any reason a process is sending for too long. |
77
|
|
|
* |
78
|
|
|
* @param int $timeout in second Defaults is for very slow smtp responses |
79
|
|
|
* @throws Swift_IoException |
80
|
|
|
*/ |
81
|
|
|
public function recover($timeout = 900) |
82
|
|
|
{ |
83
|
|
|
$this->write( |
84
|
|
|
array('sentOn' => array('$lte' => new \MongoDate(time() - $timeout))), |
85
|
|
|
array('$set' => array('sentOn' => null)), |
86
|
|
|
array('multiple' => true) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sends messages using the given transport instance. |
92
|
|
|
* |
93
|
|
|
* @param Swift_Transport $transport A transport instance |
94
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
95
|
|
|
* @return int The number of sent e-mails |
96
|
|
|
* @throws Swift_IoException |
97
|
|
|
*/ |
98
|
|
|
public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) |
99
|
|
|
{ |
100
|
|
|
if (!$transport->isStarted()) { |
101
|
|
|
$transport->start(); |
102
|
|
|
} |
103
|
|
|
$limit = $this->getMessageLimit(); |
104
|
|
|
$results = $this->collection->find(array('sentOn' => null)); |
105
|
|
|
if ($limit > 0) { |
106
|
|
|
$results->limit($limit); |
107
|
|
|
} |
108
|
|
|
$failedRecipients = (array) $failedRecipients; |
109
|
|
|
$count = 0; |
110
|
|
|
$time = time(); |
111
|
|
|
$timeLimit = $this->getTimeLimit(); |
112
|
|
|
foreach ($results as $result) { |
113
|
|
|
if (!isset($result['message']) || !($result['message'] instanceof \MongoBinData)) { |
114
|
|
|
continue; |
115
|
|
|
} |
116
|
|
|
$id = $result['_id']; |
117
|
|
|
$this->write( |
118
|
|
|
array('_id' => $id), |
119
|
|
|
array('$set' => array('sentOn' => new \MongoDate())) |
120
|
|
|
); |
121
|
|
|
$message = unserialize($result['message']->bin); |
122
|
|
|
$count += $transport->send($message, $failedRecipients); |
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$this->collection->remove(array('_id' => $id)); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
throw new Swift_IoException("Could not update email sent on date", 0, $e); |
127
|
|
|
} |
128
|
|
|
if ($timeLimit && (time() - $time) >= $timeLimit) { |
129
|
|
|
break; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
return $count; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Executes a bulk write to MongoDB, wrapping exceptions. |
137
|
|
|
* |
138
|
|
|
* @param array $query |
139
|
|
|
* @param array $set |
140
|
|
|
* @param arary $options |
141
|
|
|
* @throws Swift_IoException if things go wrong |
142
|
|
|
*/ |
143
|
|
|
protected function write(array $query, array $set, array $options = array()) |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
return $this->collection->update($query, $set, $options); |
147
|
|
|
} catch (\Exception $e) { |
148
|
|
|
throw new Swift_IoException("Could not write to database", 0, $e); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.