Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * Swift_DoctrineSpool is a spool that uses Doctrine.
13
 *
14
 * Example schema:
15
 *
16
 *  MailMessage:
17
 *   actAs: { Timestampable: ~ }
18
 *   columns:
19
 *     message: { type: clob, notnull: true }
20
 *
21
 * @package    symfony
22
 * @subpackage mailer
23
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
24
 * @version    SVN: $Id: Swift_DoctrineSpool.class.php 31249 2010-10-26 13:57:10Z fabien $
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
        // TODO: What to do with errors?
124
      }
125
 
126
      if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit())
127
      {
128
        break;
129
      }
130
    }
131
 
132
    return $count;
133
  }
134
}