Completed
Push — master ( 553806...856339 )
by Krishnaprasad
02:34
created

LeakyBucketSettings::getTimeLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 Krishnaprasad MG <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Sunspikes\Ratelimit\Throttle\Settings;
27
28
final class LeakyBucketSettings implements ThrottleSettingsInterface
29
{
30
    /**
31
     * @var int|null
32
     */
33
    private $tokenLimit;
34
35
    /**
36
     * @var int|null
37
     */
38
    private $timeLimit;
39
40
    /**
41
     * @var int|null
42
     */
43
    private $threshold;
44
45
    /**
46
     * @var int|null
47
     */
48
    private $cacheTtl;
49
50
    /**
51
     * @param int|null $tokenLimit
52
     * @param int|null $timeLimit  In milliseconds
53
     * @param int|null $threshold
54
     * @param int|null $cacheTtl   In seconds
55
     */
56 15
    public function __construct($tokenLimit = null, $timeLimit = null, $threshold = null, $cacheTtl = null)
57
    {
58 15
        $this->tokenLimit = $tokenLimit;
59 15
        $this->timeLimit = $timeLimit;
60 15
        $this->threshold = $threshold;
61 15
        $this->cacheTtl = $cacheTtl;
62 15
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67 3
    public function merge(ThrottleSettingsInterface $settings)
68
    {
69 3 View Code Duplication
        if (!$settings instanceof self) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70 1
            throw new \InvalidArgumentException(
71 1
                sprintf('Unable to merge %s into %s', get_class($settings), get_class($this))
72 1
            );
73
        }
74
75 2
        return new self(
76 2
            null === $settings->getTokenLimit() ? $this->tokenLimit : $settings->getTokenLimit(),
77 2
            null === $settings->getTimeLimit() ? $this->timeLimit : $settings->getTimeLimit(),
78 2
            null === $settings->getThreshold() ? $this->threshold : $settings->getThreshold(),
79 2
            null === $settings->getCacheTtl() ? $this->cacheTtl : $settings->getCacheTtl()
80 2
        );
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 12
    public function isValid()
87
    {
88
        return
89 12
            null !== $this->tokenLimit &&
90 12
            null !== $this->timeLimit &&
91 12
            0 !== $this->timeLimit;
92
    }
93
94
    /**
95
     * @return int|null
96
     */
97 8
    public function getTokenLimit()
98
    {
99 8
        return $this->tokenLimit;
100
    }
101
102
    /**
103
     * @return int|null
104
     */
105 8
    public function getTimeLimit()
106
    {
107 8
        return $this->timeLimit;
108
    }
109
110
    /**
111
     * @return int|null
112
     */
113 8
    public function getThreshold()
114
    {
115 8
        return $this->threshold;
116
    }
117
118
    /**
119
     * @return int|null
120
     */
121 8
    public function getCacheTtl()
122
    {
123 8
        return $this->cacheTtl;
124
    }
125
}
126