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

Swift_Transport_FailoverTransport::ping()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 2
Bugs 1 Features 2
Metric Value
cc 4
eloc 9
c 2
b 1
f 2
nc 3
nop 0
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 4.0312
rs 9.2
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
    // needed as __construct is called from elsewhere explicitly
26 11
    public function __construct()
27
    {
28 11
        parent::__construct();
29 11
    }
30
31
    /**
32
     * Send the given Message.
33
     *
34
     * Recipient/sender data will be retrieved from the Message API.
35
     * The return value is the number of recipients who were accepted for delivery.
36
     *
37
     * @param Swift_Mime_Message $message
38
     * @param string[]           $failedRecipients An array of failures by-reference
39
     *
40
     * @return int
41
     * @throws Swift_TransportException
42
     */
43 8 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...
44
    {
45 8
        $maxTransports = count($this->_transports);
46 8
        $sent = 0;
47 8
        $this->_lastUsedTransport = null;
48
49 8
        for ($i = 0; $i < $maxTransports
50 8
                     && $transport = $this->_getNextTransport(); ++$i) {
51
            try {
52 8
                if (!$transport->isStarted()) {
53 8
                    $transport->start();
54
                }
55
56 8
                $sent = $transport->send($message, $failedRecipients);
57 6
                if ($sent) {
58 4
                    $this->_lastUsedTransport = $transport;
59
60 6
                    return $sent;
61
                }
62
63 5
            } catch (Swift_TransportException $e) {
64 5
                $this->_killCurrentTransport();
65
            }
66
        }
67
68 5
        if (count($this->_transports) === 0) {
69 3
            throw new Swift_TransportException(
70 3
                'All Transports in FailoverTransport failed, or no Transports available'
71
            );
72
        }
73
74 2
        return $sent;
75
    }
76
77 8
    protected function _getNextTransport()
78
    {
79 8
        if (!isset($this->_currentTransport)) {
80 8
            $this->_currentTransport = parent::_getNextTransport();
81
        }
82
83 8
        return $this->_currentTransport;
84
    }
85
86 5
    protected function _killCurrentTransport()
87
    {
88 5
        $this->_currentTransport = null;
89 5
        parent::_killCurrentTransport();
90 5
    }
91
}
92