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

AbstractWindowSettings::getHitLimit()   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
abstract class AbstractWindowSettings implements ThrottleSettingsInterface
29
{
30
    /**
31
     * @var int|null
32
     */
33
    private $hitLimit;
34
35
    /**
36
     * @var int|null
37
     */
38
    private $timeLimit;
39
40
    /**
41
     * @var int|null
42
     */
43
    private $cacheTtl;
44
45
    /**
46
     * @param int|null $tokenLimit
47
     * @param int|null $timeLimit  In seconds
48
     * @param int|null $cacheTtl   In seconds
49
     */
50 36
    public function __construct($tokenLimit = null, $timeLimit = null, $cacheTtl = null)
51
    {
52 36
        $this->hitLimit = $tokenLimit;
53 36
        $this->timeLimit = $timeLimit;
54 36
        $this->cacheTtl = $cacheTtl;
55 36
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 6
    public function merge(ThrottleSettingsInterface $settings)
61
    {
62 6 View Code Duplication
        if (!$settings instanceof static) {
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...
63 2
            throw new \InvalidArgumentException(
64 2
                sprintf('Unable to merge %s into %s', get_class($settings), get_class($this))
65 2
            );
66
        }
67
68 4
        return new static(
69 4
            null === $settings->getHitLimit() ? $this->hitLimit : $settings->getHitLimit(),
70 4
            null === $settings->getTimeLimit() ? $this->timeLimit : $settings->getTimeLimit(),
71 4
            null === $settings->getCacheTtl() ? $this->cacheTtl : $settings->getCacheTtl()
72 4
        );
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 30
    public function isValid()
79
    {
80
        return
81 30
            null !== $this->hitLimit &&
82 30
            null !== $this->timeLimit &&
83 30
            0 !== $this->timeLimit;
84
    }
85
86
    /**
87
     * @return int|null
88
     */
89 24
    public function getHitLimit()
90
    {
91 24
        return $this->hitLimit;
92
    }
93
94
    /**
95
     * @return int|null
96
     */
97 24
    public function getTimeLimit()
98
    {
99 24
        return $this->timeLimit;
100
    }
101
102
    /**
103
     * @return int|null
104
     */
105 24
    public function getCacheTtl()
106
    {
107 24
        return $this->cacheTtl;
108
    }
109
}
110