Completed
Push — 5.x ( d88d99...752aee )
by Lars
36:33 queued 21:35
created

Swift_Transport_FailoverTransport   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 42.31 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 11
c 2
b 1
f 0
lcom 1
cbo 3
dl 33
loc 78
ccs 29
cts 29
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C send() 33 33 7
A _getNextTransport() 0 8 2
A _killCurrentTransport() 0 5 1

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
 * Contains a list of redundant Transports so when one fails, the next is used.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Transport_FailoverTransport extends Swift_Transport_LoadBalancedTransport
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
     * Registered transport currently used.
20
     *
21
     * @var Swift_Transport
22
     */
23
    private $_currentTransport;
24
25
    /**
26
     * Creates a new FailoverTransport.
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Send the given Message.
35
     *
36
     * Recipient/sender data will be retrieved from the Message API.
37 8
     * The return value is the number of recipients who were accepted for delivery.
38
     *
39 8
     * @param Swift_Mime_Message $message
40 8
     * @param string[]           $failedRecipients An array of failures by-reference
41 8
     *
42
     * @return int
43 8
     * @throws Swift_TransportException
44 8
     */
45 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...
46 8
    {
47 8
        $maxTransports = count($this->_transports);
48 8
        $sent = 0;
49
        $this->_lastUsedTransport = null;
50 8
51 6
        for ($i = 0; $i < $maxTransports
52 4
                     && $transport = $this->_getNextTransport(); ++$i) {
53
            try {
54 4
                if (!$transport->isStarted()) {
55
                    $transport->start();
56
                }
57 7
58 5
                $sent = $transport->send($message, $failedRecipients);
59
                if ($sent) {
60 7
                    $this->_lastUsedTransport = $transport;
61
62 5
                    return $sent;
63 3
                }
64
65 3
            } catch (Swift_TransportException $e) {
66
                $this->_killCurrentTransport();
67
            }
68 2
        }
69
70
        if (count($this->_transports) === 0) {
71 8
            throw new Swift_TransportException(
72
                'All Transports in FailoverTransport failed, or no Transports available'
73 8
            );
74 8
        }
75 8
76
        return $sent;
77 8
    }
78
79
    protected function _getNextTransport()
80 5
    {
81
        if (!isset($this->_currentTransport)) {
82 5
            $this->_currentTransport = parent::_getNextTransport();
83 5
        }
84 5
85
        return $this->_currentTransport;
86
    }
87
88
    protected function _killCurrentTransport()
89
    {
90
        $this->_currentTransport = null;
91
        parent::_killCurrentTransport();
92
    }
93
}
94