| 1 |
<?php |
|---|
| 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 |
class Swift_DoctrineSpool extends Swift_ConfigurableSpool |
|---|
| 27 |
{ |
|---|
| 28 |
protected |
|---|
| 29 |
$model = null, |
|---|
| 30 |
$column = null, |
|---|
| 31 |
$method = null; |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
* Constructor. |
|---|
| 35 |
* |
|---|
| 36 |
* @param string The Doctrine model to use to store the messages (MailMessage by default) |
|---|
| 37 |
* @param string The column name to use for message storage (message by default) |
|---|
| 38 |
* @param string The method to call to retrieve the query to execute (optional) |
|---|
| 39 |
*/ |
|---|
| 40 |
public function __construct($model = 'MailMessage', $column = 'message', $method = 'createQuery') |
|---|
| 41 |
{ |
|---|
| 42 |
$this->model = $model; |
|---|
| 43 |
$this->column = $column; |
|---|
| 44 |
$this->method = $method; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
* Tests if this Transport mechanism has started. |
|---|
| 49 |
* |
|---|
| 50 |
* @return boolean |
|---|
| 51 |
*/ |
|---|
| 52 |
public function isStarted() |
|---|
| 53 |
{ |
|---|
| 54 |
return true; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
* Starts this Transport mechanism. |
|---|
| 59 |
*/ |
|---|
| 60 |
public function start() |
|---|
| 61 |
{ |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
* Stops this Transport mechanism. |
|---|
| 66 |
*/ |
|---|
| 67 |
public function stop() |
|---|
| 68 |
{ |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
* Stores a message in the queue. |
|---|
| 73 |
* |
|---|
| 74 |
* @param Swift_Mime_Message $message The message to store |
|---|
| 75 |
*/ |
|---|
| 76 |
public function queueMessage(Swift_Mime_Message $message) |
|---|
| 77 |
{ |
|---|
| 78 |
$object = new $this->model; |
|---|
| 79 |
|
|---|
| 80 |
if (!$object instanceof Doctrine_Record) |
|---|
| 81 |
{ |
|---|
| 82 |
throw new InvalidArgumentException('The mailer message object must be a Doctrine_Record object.'); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
$object->{$this->column} = serialize($message); |
|---|
| 86 |
$object->save(); |
|---|
| 87 |
|
|---|
| 88 |
$object->free(true); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Sends messages using the given transport instance. |
|---|
| 93 |
* |
|---|
| 94 |
* @param Swift_Transport $transport A transport instance |
|---|
| 95 |
* @param string[] &$failedRecipients An array of failures by-reference |
|---|
| 96 |
* |
|---|
| 97 |
* @return int The number of sent emails |
|---|
| 98 |
*/ |
|---|
| 99 |
public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) |
|---|
| 100 |
{ |
|---|
| 101 |
$table = Doctrine_Core::getTable($this->model); |
|---|
| 102 |
$objects = $table->{$this->method}()->limit($this->getMessageLimit())->execute(); |
|---|
| 103 |
|
|---|
| 104 |
if (!$transport->isStarted()) |
|---|
| 105 |
{ |
|---|
| 106 |
$transport->start(); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
$count = 0; |
|---|
| 110 |
$time = time(); |
|---|
| 111 |
foreach ($objects as $object) |
|---|
| 112 |
{ |
|---|
| 113 |
$message = unserialize($object->{$this->column}); |
|---|
| 114 |
|
|---|
| 115 |
$object->delete(); |
|---|
| 116 |
|
|---|
| 117 |
try |
|---|
| 118 |
{ |
|---|
| 119 |
$count += $transport->send($message, $failedRecipients); |
|---|
| 120 |
} |
|---|
| 121 |
catch (Exception $e) |
|---|
| 122 |
{ |
|---|
| 123 |
|
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit()) |
|---|
| 127 |
{ |
|---|
| 128 |
break; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
return $count; |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|