Completed
Push — 5.x ( 2b0825...7df5e8 )
by Lars
04:49
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 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
c 1
b 0
f 1
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
     * Check if this Transport mechanism is alive.
105
     *
106
     * If a Transport mechanism session is no longer functional, the method
107
     * returns FALSE. It is the responsibility of the developer to handle this
108
     * case and restart the Transport mechanism manually.
109
     *
110
     * @example
111
     *
112
     *   if (!$transport->ping()) {
113
     *      $transport->stop();
114
     *      $transport->start();
115
     *   }
116
     *
117
     * The Transport mechanism will be started, if it is not already.
118
     *
119
     * It is undefined if the Transport mechanism attempts to restart as long as
120
     * the return value reflects whether the mechanism is now functional.
121
     *
122
     * @return bool TRUE if the transport is alive
123
     */
124
    public function ping()
125
    {
126
        foreach ($this->transports as $transport) {
0 ignored issues
show
Bug introduced by
The property transports does not seem to exist. Did you mean _deadTransports?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
127
            if (!$transport->ping()) {
128
                $this->killCurrentTransport();
0 ignored issues
show
Bug introduced by
The method killCurrentTransport() does not exist on Swift_Transport_LoadBalancedTransport. Did you maybe mean _killCurrentTransport()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
129
            }
130
        }
131
132
        return count($this->transports) > 0;
0 ignored issues
show
Bug introduced by
The property transports does not seem to exist. Did you mean _deadTransports?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
    }
134
135
    /**
136
     * Send the given Message.
137
     *
138
     * Recipient/sender data will be retrieved from the Message API.
139
     * The return value is the number of recipients who were accepted for delivery.
140
     *
141
     * @param Swift_Mime_Message $message
142
     * @param string[]           $failedRecipients An array of failures by-reference
143
     *
144
     * @return int
145
     *
146
     * @throws Swift_TransportException
147
     */
148 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...
149
    {
150 10
        $maxTransports = count($this->_transports);
151 10
        $sent = 0;
152 10
        $this->_lastUsedTransport = null;
153
154 10
        for ($i = 0; $i < $maxTransports
155 10
                     && $transport = $this->_getNextTransport(); ++$i) {
156
            try {
157 10
                if (!$transport->isStarted()) {
158 10
                    $transport->start();
159
                }
160
161 10
                $sent = $transport->send($message, $failedRecipients);
162 8
                if ($sent) {
163 7
                    $this->_lastUsedTransport = $transport;
164 8
                    break;
165
                }
166
167 5
            } catch (Swift_TransportException $e) {
168 5
                $this->_killCurrentTransport();
169
            }
170
        }
171
172 10
        if (count($this->_transports) === 0) {
173 3
            throw new Swift_TransportException(
174 3
                'All Transports in LoadBalancedTransport failed, or no Transports available'
175
            );
176
        }
177
178 8
        return $sent;
179
    }
180
181
    /**
182
     * Register a plugin.
183
     *
184
     * @param Swift_Events_EventListener $plugin
185
     */
186 2
    public function registerPlugin(Swift_Events_EventListener $plugin)
187
    {
188 2
        foreach ($this->_transports as $transport) {
189 2
            $transport->registerPlugin($plugin);
190
        }
191 2
    }
192
193
    /**
194
     * Rotates the transport list around and returns the first instance.
195
     *
196
     * @return Swift_Transport
197
     */
198 18
    protected function _getNextTransport()
199
    {
200 18
        $next = array_shift($this->_transports);
201 18
        if ($next) {
202 18
            $this->_transports[] = $next;
203
        }
204
205 18
        return $next;
206
    }
207
208
    /**
209
     * Tag the currently used (top of stack) transport as dead/useless.
210
     */
211 10
    protected function _killCurrentTransport()
212
    {
213 10
        $transport = array_pop($this->_transports);
214 10
        if ($transport) {
215
216
            try {
217 10
                $transport->stop();
218 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...
219
            }
220
221 10
            $this->_deadTransports[] = $transport;
222
        }
223 10
    }
224
}
225