Completed
Push — 5.x ( 5d59f7...0928cf )
by Lars
04:34
created

Swift_Transport_LoadBalancedTransport::ping()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 2
Metric Value
cc 3
eloc 5
c 2
b 1
f 2
nc 3
nop 0
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of SwiftMailer.
5
 * (c) 2004-2009 Chris Corbyn
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
 * Redundantly and rotationally uses several Transports when sending.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Transport_LoadBalancedTransport implements Swift_Transport
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Transports which are deemed useless.
20
     *
21
     * @var Swift_Transport[]
22
     */
23
    private $_deadTransports = array();
24
25
    /**
26
     * The Transports which are used in rotation.
27
     *
28
     * @var Swift_Transport[]
29
     */
30
    protected $_transports = array();
31
32
    /**
33
     * The Transport used in the last successful send operation.
34
     *
35
     * @var Swift_Transport
36
     */
37
    protected $_lastUsedTransport;
38
39
    // needed as __construct is called from elsewhere explicitly
40 23
    public function __construct()
41
    {
42 23
    }
43
44
    /**
45
     * Set $transports to delegate to.
46
     *
47
     * @param Swift_Transport[] $transports
48
     */
49 22
    public function setTransports(array $transports)
50
    {
51 22
        $this->_transports = $transports;
52 22
        $this->_deadTransports = array();
53 22
    }
54
55
    /**
56
     * Get $transports to delegate to.
57
     *
58
     * @return Swift_Transport[]
59
     */
60
    public function getTransports()
61
    {
62
        return array_merge($this->_transports, $this->_deadTransports);
63
    }
64
65
    /**
66
     * Get the Transport used in the last successful send operation.
67
     *
68
     * @return Swift_Transport
69
     */
70
    public function getLastUsedTransport()
71
    {
72
        return $this->_lastUsedTransport;
73
    }
74
75
    /**
76
     * Test if this Transport mechanism has started.
77
     *
78
     * @return bool
79
     */
80 4
    public function isStarted()
81
    {
82 4
        return count($this->_transports) > 0;
83
    }
84
85
    /**
86
     * Start this Transport mechanism.
87
     */
88 20
    public function start()
89
    {
90 20
        $this->_transports = array_merge($this->_transports, $this->_deadTransports);
91 20
    }
92
93
    /**
94
     * Stop this Transport mechanism.
95
     */
96 2
    public function stop()
97
    {
98 2
        foreach ($this->_transports as $transport) {
99 2
            $transport->stop();
100
        }
101 2
    }
102
103
    /**
104
     * Send the given Message.
105
     *
106
     * Recipient/sender data will be retrieved from the Message API.
107
     * The return value is the number of recipients who were accepted for delivery.
108
     *
109
     * @param Swift_Mime_Message $message
110
     * @param string[]           $failedRecipients An array of failures by-reference
111
     *
112
     * @return int
113
     *
114
     * @throws Swift_TransportException
115
     */
116 10 View Code Duplication
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
    {
118 10
        $maxTransports = count($this->_transports);
119 10
        $sent = 0;
120 10
        $this->_lastUsedTransport = null;
121
122 10
        for ($i = 0; $i < $maxTransports
123 10
                     && $transport = $this->_getNextTransport(); ++$i) {
124
            try {
125 10
                if (!$transport->isStarted()) {
126 10
                    $transport->start();
127
                }
128
129 10
                $sent = $transport->send($message, $failedRecipients);
130 8
                if ($sent) {
131 7
                    $this->_lastUsedTransport = $transport;
132 8
                    break;
133
                }
134
135 5
            } catch (Swift_TransportException $e) {
136 5
                $this->_killCurrentTransport();
137
            }
138
        }
139
140 10
        if (count($this->_transports) === 0) {
141 3
            throw new Swift_TransportException(
142 3
                'All Transports in LoadBalancedTransport failed, or no Transports available'
143
            );
144
        }
145
146 8
        return $sent;
147
    }
148
149
    /**
150
     * Register a plugin.
151
     *
152
     * @param Swift_Events_EventListener $plugin
153
     */
154 2
    public function registerPlugin(Swift_Events_EventListener $plugin)
155
    {
156 2
        foreach ($this->_transports as $transport) {
157 2
            $transport->registerPlugin($plugin);
158
        }
159 2
    }
160
161
    /**
162
     * Rotates the transport list around and returns the first instance.
163
     *
164
     * @return Swift_Transport
165
     */
166 18
    protected function _getNextTransport()
167
    {
168 18
        $next = array_shift($this->_transports);
169 18
        if ($next) {
170 18
            $this->_transports[] = $next;
171
        }
172
173 18
        return $next;
174
    }
175
176
    /**
177
     * Tag the currently used (top of stack) transport as dead/useless.
178
     */
179 10
    protected function _killCurrentTransport()
180
    {
181 10
        $transport = array_pop($this->_transports);
182 10
        if ($transport) {
183
184
            try {
185 10
                $transport->stop();
186 10
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
187
            }
188
189 10
            $this->_deadTransports[] = $transport;
190
        }
191 10
    }
192
}
193