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_PropelSpool is a spool that uses Propel.
13
 *
14
 * Example schema:
15
 *
16
 *  mail_message:
17
 *   message:    { type: clob }
18
 *   created_at: ~
19
 *
20
 * @package    symfony
21
 * @subpackage mailer
22
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
23
 * @version    SVN: $Id: Swift_PropelSpool.class.php 30529 2010-08-04 16:30:29Z fabien $
24
 */
25
class Swift_PropelSpool extends Swift_ConfigurableSpool
26
{
27
  protected
28
    $model = null,
29
    $column = null,
30
    $method = null;
31
 
32
  /**
33
   * Constructor.
34
   *
35
   * @param string The Propel model to use to store the messages (MailMessage by default)
36
   * @param string The column name to use for message storage (message by default)
37
   * @param string The method to call to retrieve the messages to send (optional)
38
   */
39
  public function __construct($model = 'MailMessage', $column = 'message', $method = 'doSelect')
40
  {
41
    $this->model = $model;
42
    $this->column = $column;
43
    $this->method = $method;
44
  }
45
 
46
  /**
47
   * Tests if this Transport mechanism has started.
48
   *
49
   * @return boolean
50
   */
51
  public function isStarted()
52
  {
53
    return true;
54
  }
55
 
56
  /**
57
   * Starts this Transport mechanism.
58
   */
59
  public function start()
60
  {
61
  }
62
 
63
  /**
64
   * Stops this Transport mechanism.
65
   */
66
  public function stop()
67
  {
68
  }
69
 
70
  /**
71
   * Stores a message in the queue.
72
   *
73
   * @param Swift_Mime_Message $message The message to store
74
   */
75
  public function queueMessage(Swift_Mime_Message $message)
76
  {
77
    $object = new $this->model;
78
 
79
    if (!$object instanceof BaseObject)
80
    {
81
      throw new InvalidArgumentException('The mailer message object must be a BaseObject object.');
82
    }
83
 
84
    $model = constant($this->model.'::PEER');
85
    $method = 'set'.call_user_func(array($model, 'translateFieldName'), $this->column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
86
 
87
    $object->$method(serialize($message));
88
    $object->save();
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
    $criteria = new Criteria();
102
    $criteria->setLimit($this->getMessageLimit());
103
 
104
    $model = constant($this->model.'::PEER');
105
    $objects = call_user_func(array($model, $this->method), $criteria);
106
 
107
    if (!$transport->isStarted())
108
    {
109
      $transport->start();
110
    }
111
 
112
    $method = 'get'.call_user_func(array($model, 'translateFieldName'), $this->column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
113
    $count = 0;
114
    $time = time();
115
    foreach ($objects as $object)
116
    {
117
      if (is_resource($object->getMessage()))
118
      {
119
        $message = unserialize(stream_get_contents($object->getMessage()));
120
      }
121
      else
122
      {
123
          $message = unserialize($object->getMessage());
124
      }
125
 
126
      $object->delete();
127
 
128
      try
129
      {
130
        $count += $transport->send($message, $failedRecipients);
131
      }
132
      catch (Exception $e)
133
      {
134
        // TODO: What to do with errors?
135
      }
136
 
137
      if ($this->getTimeLimit() && (time() - $time) >= $this->getTimeLimit())
138
      {
139
        break;
140
      }
141
    }
142
 
143
    return $count;
144
  }
145
}