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

Swift_Transport_FailoverTransport::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
    /**
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