Completed
Push — 5.x ( ce3e85...d88d99 )
by Lars
27:25 queued 12:23
created

Swift_Transport_LoadBalancedTransport   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 179
Duplicated Lines 17.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.98%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 22
c 2
b 1
f 0
lcom 1
cbo 2
dl 32
loc 179
ccs 53
cts 57
cp 0.9298
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setTransports() 0 5 1
A getTransports() 0 4 1
A getLastUsedTransport() 0 4 1
A isStarted() 0 4 1
A start() 0 4 1
A stop() 0 6 2
C send() 32 32 7
A registerPlugin() 0 6 2
A _getNextTransport() 0 9 2
A _killCurrentTransport() 0 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
40
     * Constructor.
41
     */
42
    public function __construct()
43
    {
44 22
    }
45
46 22
    /**
47 22
     * Set $transports to delegate to.
48 22
     *
49
     * @param Swift_Transport[] $transports
50
     */
51
    public function setTransports(array $transports)
52
    {
53
        $this->_transports = $transports;
54
        $this->_deadTransports = array();
55
    }
56
57
    /**
58
     * Get $transports to delegate to.
59
     *
60
     * @return Swift_Transport[]
61
     */
62
    public function getTransports()
63
    {
64
        return array_merge($this->_transports, $this->_deadTransports);
65
    }
66
67
    /**
68
     * Get the Transport used in the last successful send operation.
69
     *
70
     * @return Swift_Transport
71
     */
72
    public function getLastUsedTransport()
73
    {
74
        return $this->_lastUsedTransport;
75 4
    }
76
77 4
    /**
78
     * Test if this Transport mechanism has started.
79
     *
80
     * @return bool
81
     */
82
    public function isStarted()
83 20
    {
84
        return count($this->_transports) > 0;
85 20
    }
86 20
87
    /**
88
     * Start this Transport mechanism.
89
     */
90
    public function start()
91 2
    {
92
        $this->_transports = array_merge($this->_transports, $this->_deadTransports);
93 2
    }
94 2
95 2
    /**
96 2
     * Stop this Transport mechanism.
97
     */
98
    public function stop()
99
    {
100
        foreach ($this->_transports as $transport) {
101
            $transport->stop();
102
        }
103
    }
104
105
    /**
106
     * Send the given Message.
107
     *
108
     * Recipient/sender data will be retrieved from the Message API.
109
     * The return value is the number of recipients who were accepted for delivery.
110
     *
111 10
     * @param Swift_Mime_Message $message
112
     * @param string[]           $failedRecipients An array of failures by-reference
113 10
     *
114 10
     * @return int
115 10
     *
116
     * @throws Swift_TransportException
117 10
     */
118 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...
119
    {
120 10
        $maxTransports = count($this->_transports);
121 10
        $sent = 0;
122 10
        $this->_lastUsedTransport = null;
123
124 10
        for ($i = 0; $i < $maxTransports
125 8
                     && $transport = $this->_getNextTransport(); ++$i) {
126 7
            try {
127 7
                if (!$transport->isStarted()) {
128
                    $transport->start();
129
                }
130 7
131 5
                $sent = $transport->send($message, $failedRecipients);
132
                if ($sent) {
133 7
                    $this->_lastUsedTransport = $transport;
134
                    break;
135 10
                }
136 3
137
            } catch (Swift_TransportException $e) {
138 3
                $this->_killCurrentTransport();
139
            }
140
        }
141 8
142
        if (count($this->_transports) === 0) {
143
            throw new Swift_TransportException(
144
                'All Transports in LoadBalancedTransport failed, or no Transports available'
145
            );
146
        }
147
148
        return $sent;
149 2
    }
150
151 2
    /**
152 2
     * Register a plugin.
153 2
     *
154 2
     * @param Swift_Events_EventListener $plugin
155
     */
156
    public function registerPlugin(Swift_Events_EventListener $plugin)
157
    {
158
        foreach ($this->_transports as $transport) {
159
            $transport->registerPlugin($plugin);
160
        }
161 18
    }
162
163 18
    /**
164 18
     * Rotates the transport list around and returns the first instance.
165 18
     *
166 18
     * @return Swift_Transport
167
     */
168 18
    protected function _getNextTransport()
169
    {
170
        $next = array_shift($this->_transports);
171
        if ($next) {
172
            $this->_transports[] = $next;
173
        }
174 10
175
        return $next;
176 10
    }
177 10
178
    /**
179
     * Tag the currently used (top of stack) transport as dead/useless.
180 10
     */
181 10
    protected function _killCurrentTransport()
182
    {
183
        $transport = array_pop($this->_transports);
184 10
        if ($transport) {
185 10
186 10
            try {
187
                $transport->stop();
188
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
189
            }
190
191
            $this->_deadTransports[] = $transport;
192
        }
193
    }
194
}
195