1<?php
2
3/*
4 * This file is part of SwiftMailer.
5 * (c) 2009 Fabien Potencier <fabien.potencier@gmail.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 * Stores Messages in a queue.
13 *
14 * @author Fabien Potencier
15 */
16class Swift_Transport_SpoolTransport implements Swift_Transport
17{
18    /** The spool instance */
19    private $_spool;
20
21    /** The event dispatcher from the plugin API */
22    private $_eventDispatcher;
23
24    /**
25     * Constructor.
26     */
27    public function __construct(Swift_Events_EventDispatcher $eventDispatcher, Swift_Spool $spool = null)
28    {
29        $this->_eventDispatcher = $eventDispatcher;
30        $this->_spool = $spool;
31    }
32
33    /**
34     * Sets the spool object.
35     *
36     * @param Swift_Spool $spool
37     *
38     * @return $this
39     */
40    public function setSpool(Swift_Spool $spool)
41    {
42        $this->_spool = $spool;
43
44        return $this;
45    }
46
47    /**
48     * Get the spool object.
49     *
50     * @return Swift_Spool
51     */
52    public function getSpool()
53    {
54        return $this->_spool;
55    }
56
57    /**
58     * Tests if this Transport mechanism has started.
59     *
60     * @return bool
61     */
62    public function isStarted()
63    {
64        return true;
65    }
66
67    /**
68     * Starts this Transport mechanism.
69     */
70    public function start()
71    {
72    }
73
74    /**
75     * Stops this Transport mechanism.
76     */
77    public function stop()
78    {
79    }
80
81    /**
82     * Sends the given message.
83     *
84     * @param Swift_Mime_Message $message
85     * @param string[]           $failedRecipients An array of failures by-reference
86     *
87     * @return int The number of sent e-mail's
88     */
89    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
90    {
91        if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
92            $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
93            if ($evt->bubbleCancelled()) {
94                return 0;
95            }
96        }
97
98        $success = $this->_spool->queueMessage($message);
99
100        if ($evt) {
101            $evt->setResult($success ? Swift_Events_SendEvent::RESULT_SPOOLED : Swift_Events_SendEvent::RESULT_FAILED);
102            $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
103        }
104
105        return 1;
106    }
107
108    /**
109     * Register a plugin.
110     *
111     * @param Swift_Events_EventListener $plugin
112     */
113    public function registerPlugin(Swift_Events_EventListener $plugin)
114    {
115        $this->_eventDispatcher->bindEventListener($plugin);
116    }
117}
118