Completed
Pull Request — 5.x (#16)
by Lars
05:10
created

_throttleBytesPerMinute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 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
 * Throttles the rate at which emails are sent.
13
 *
14
 * @author Chris Corbyn
15
 */
16
class Swift_Plugins_ThrottlerPlugin extends Swift_Plugins_BandwidthMonitorPlugin implements Swift_Plugins_Sleeper, Swift_Plugins_Timer
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
    /** Flag for throttling in bytes per minute */
19
    const BYTES_PER_MINUTE = 0x01;
20
21
    /** Flag for throttling in emails per second (Amazon SES) */
22
    const MESSAGES_PER_SECOND = 0x11;
23
24
    /** Flag for throttling in emails per minute */
25
    const MESSAGES_PER_MINUTE = 0x10;
26
27
    /**
28
     * The Sleeper instance for sleeping.
29
     *
30
     * @var Swift_Plugins_Sleeper
31
     */
32
    private $_sleeper;
33
34
    /**
35
     * The Timer instance which provides the timestamp.
36
     *
37
     * @var Swift_Plugins_Timer
38
     */
39
    private $_timer;
40
41
    /**
42
     * The time at which the first email was sent.
43
     *
44
     * @var int
45
     */
46
    private $_start;
47
48
    /**
49
     * The rate at which messages should be sent.
50
     *
51
     * @var int
52
     */
53
    private $_rate;
54
55
    /**
56
     * The mode for throttling.
57
     *
58
     * This is {@link BYTES_PER_MINUTE} or {@link MESSAGES_PER_MINUTE}
59
     *
60
     * @var int
61
     */
62
    private $_mode;
63
64
    /**
65
     * An internal counter of the number of messages sent.
66
     *
67
     * @var int
68
     */
69
    private $_messages = 0;
70
71
    /**
72
     * Create a new ThrottlerPlugin.
73
     *
74
     * @param int                   $rate
75
     * @param int                   $mode,   defaults to {@link BYTES_PER_MINUTE}
0 ignored issues
show
Bug introduced by
There is no parameter named $mode,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @param Swift_Plugins_Sleeper $sleeper (only needed in testing)
77
     * @param Swift_Plugins_Timer   $timer   (only needed in testing)
78
     */
79 2
    public function __construct($rate, $mode = self::BYTES_PER_MINUTE, Swift_Plugins_Sleeper $sleeper = null, Swift_Plugins_Timer $timer = null)
80
    {
81 2
        $this->_rate = $rate;
82 2
        $this->_mode = $mode;
83 2
        $this->_sleeper = $sleeper;
84 2
        $this->_timer = $timer;
85 2
    }
86
87
    /**
88
     * Invoked immediately before the Message is sent.
89
     *
90
     * @param Swift_Events_SendEvent $evt
91
     */
92 2
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
93
    {
94 2
        $time = $this->getTimestamp();
95 2
        if (!isset($this->_start)) {
96 2
            $this->_start = $time;
97
        }
98 2
        $duration = $time - $this->_start;
99
100 2
        switch ($this->_mode) {
101
            case self::BYTES_PER_MINUTE :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
102 1
                $sleep = $this->_throttleBytesPerMinute($duration);
103 1
                break;
104
            case self::MESSAGES_PER_SECOND :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
105
                $sleep = $this->_throttleMessagesPerSecond($duration);
106
                break;
107
            case self::MESSAGES_PER_MINUTE :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108 1
                $sleep = $this->_throttleMessagesPerMinute($duration);
109 1
                break;
110
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
111
                $sleep = 0;
112
                break;
113
        }
114
115 2
        if ($sleep > 0) {
116 2
            $this->sleep($sleep);
117
        }
118 2
    }
119
120
    /**
121
     * Invoked when a Message is sent.
122
     *
123
     * @param Swift_Events_SendEvent $evt
124
     */
125 2
    public function sendPerformed(Swift_Events_SendEvent $evt)
126
    {
127 2
        parent::sendPerformed($evt);
128 2
        ++$this->_messages;
129 2
    }
130
131
    /**
132
     * Sleep for $seconds.
133
     *
134
     * @param int $seconds
135
     */
136 2
    public function sleep($seconds)
137
    {
138 2
        if (isset($this->_sleeper)) {
139 2
            $this->_sleeper->sleep($seconds);
140
        } else {
141
            sleep($seconds);
142
        }
143 2
    }
144
145
    /**
146
     * Get the current UNIX timestamp.
147
     *
148
     * @return int
149
     */
150 2
    public function getTimestamp()
151
    {
152 2
        if (isset($this->_timer)) {
153 2
            return $this->_timer->getTimestamp();
154
        }
155
156
        return time();
157
    }
158
159
    /**
160
     * Get a number of seconds to sleep for.
161
     *
162
     * @param int $timePassed
163
     *
164
     * @return int
165
     */
166 1
    private function _throttleBytesPerMinute($timePassed)
167
    {
168 1
        $expectedDuration = $this->getBytesOut() / ($this->_rate / 60);
169
170 1
        return (int) ceil($expectedDuration - $timePassed);
171
    }
172
173
    /**
174
     * Get a number of seconds to sleep for.
175
     *
176
     * @param int $timePassed
177
     *
178
     * @return int
179
     */
180
    private function _throttleMessagesPerSecond($timePassed)
181
    {
182
        $expectedDuration = $this->_messages / ($this->_rate);
183
184
        return (int) ceil($expectedDuration - $timePassed);
185
    }
186
187
    /**
188
     * Get a number of seconds to sleep for.
189
     *
190
     * @param int $timePassed
191
     *
192
     * @return int
193
     */
194 1
    private function _throttleMessagesPerMinute($timePassed)
195
    {
196 1
        $expectedDuration = $this->_messages / ($this->_rate / 60);
197
198 1
        return (int) ceil($expectedDuration - $timePassed);
199
    }
200
}
201