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

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 20
rs 9.2
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
 * 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
     * Check if this Transport mechanism is alive.
33
     *
34
     * If a Transport mechanism session is no longer functional, the method
35
     * returns FALSE. It is the responsibility of the developer to handle this
36
     * case and restart the Transport mechanism manually.
37
     *
38
     * @example
39
     *
40
     *   if (!$transport->ping()) {
41
     *      $transport->stop();
42
     *      $transport->start();
43
     *   }
44
     *
45
     * The Transport mechanism will be started, if it is not already.
46
     *
47
     * It is undefined if the Transport mechanism attempts to restart as long as
48
     * the return value reflects whether the mechanism is now functional.
49
     *
50
     * @return bool TRUE if the transport is alive
51
     */
52
    public function ping()
53
    {
54
        $maxTransports = count($this->transports);
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...
55
        for ($i = 0; $i < $maxTransports
56
            && $transport = $this->getNextTransport(); ++$i) {
0 ignored issues
show
Bug introduced by
The method getNextTransport() does not exist on Swift_Transport_FailoverTransport. Did you maybe mean _getNextTransport()?

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...
57
            if ($transport->ping()) {
58
                return true;
59
            } else {
60
                $this->killCurrentTransport();
0 ignored issues
show
Bug introduced by
The method killCurrentTransport() does not exist on Swift_Transport_FailoverTransport. 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...
61
            }
62
        }
63
64
        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...
65
    }
66
67
    /**
68
     * Send the given Message.
69
     *
70
     * Recipient/sender data will be retrieved from the Message API.
71
     * The return value is the number of recipients who were accepted for delivery.
72
     *
73
     * @param Swift_Mime_Message $message
74
     * @param string[]           $failedRecipients An array of failures by-reference
75
     *
76
     * @return int
77
     * @throws Swift_TransportException
78
     */
79 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...
80
    {
81 8
        $maxTransports = count($this->_transports);
82 8
        $sent = 0;
83 8
        $this->_lastUsedTransport = null;
84
85 8
        for ($i = 0; $i < $maxTransports
86 8
                     && $transport = $this->_getNextTransport(); ++$i) {
87
            try {
88 8
                if (!$transport->isStarted()) {
89 8
                    $transport->start();
90
                }
91
92 8
                $sent = $transport->send($message, $failedRecipients);
93 6
                if ($sent) {
94 4
                    $this->_lastUsedTransport = $transport;
95
96 6
                    return $sent;
97
                }
98
99 5
            } catch (Swift_TransportException $e) {
100 5
                $this->_killCurrentTransport();
101
            }
102
        }
103
104 5
        if (count($this->_transports) === 0) {
105 3
            throw new Swift_TransportException(
106 3
                'All Transports in FailoverTransport failed, or no Transports available'
107
            );
108
        }
109
110 2
        return $sent;
111
    }
112
113 8
    protected function _getNextTransport()
114
    {
115 8
        if (!isset($this->_currentTransport)) {
116 8
            $this->_currentTransport = parent::_getNextTransport();
117
        }
118
119 8
        return $this->_currentTransport;
120
    }
121
122 5
    protected function _killCurrentTransport()
123
    {
124 5
        $this->_currentTransport = null;
125 5
        parent::_killCurrentTransport();
126 5
    }
127
}
128