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

FixedWindowThrottler::getCachedHitCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
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\Throttler;
27
28
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException;
29
30
final class FixedWindowThrottler extends AbstractWindowThrottler implements RetriableThrottlerInterface
31
{
32
    const TIME_CACHE_KEY = ':time';
33
    const HITS_CACHE_KEY = ':hits';
34
35
    /**
36
     * @var int|null
37
     */
38
    private $hitCount;
39
40
    /**
41
     * @inheritdoc
42
     */
43 12
    public function hit()
44
    {
45 12
        $this->setCachedHitCount($this->count() + 1);
46
47
        // Update the window start time if the previous window has passed, or no cached window exists
48
        try {
49 12
            if (($this->timeProvider->now() - $this->cache->get($this->key.self::TIME_CACHE_KEY)) > $this->timeLimit) {
50 1
                $this->cache->set($this->key.self::TIME_CACHE_KEY, $this->timeProvider->now(), $this->cacheTtl);
51 1
            }
52 12
        } catch (ItemNotFoundException $exception) {
53 11
            $this->cache->set($this->key.self::TIME_CACHE_KEY, $this->timeProvider->now(), $this->cacheTtl);
54
        }
55
56 12
        return $this;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 18 View Code Duplication
    public function count()
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...
63
    {
64
        try {
65 18
            if (($this->timeProvider->now() - $this->cache->get($this->key.self::TIME_CACHE_KEY)) > $this->timeLimit) {
66 5
                return 0;
67
            }
68
69 13
            return $this->getCachedHitCount();
70 12
        } catch (ItemNotFoundException $exception) {
71 12
            return 0;
72
        }
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 3
    public function clear()
79
    {
80 3
        $this->setCachedHitCount(0);
81 3
        $this->cache->set($this->key.self::TIME_CACHE_KEY, $this->timeProvider->now(), $this->cacheTtl);
82 3
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 7 View Code Duplication
    public function getRetryTimeout()
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...
88
    {
89 7
        if ($this->check()) {
90 6
            return 0;
91
        }
92
93
        // Return the time until the current window ends
94
        // Try/catch for the ItemNotFoundException is not required, in that case $this->check() will return true
95 2
        return 1e3 * ($this->timeLimit - $this->timeProvider->now() + $this->cache->get($this->key.self::TIME_CACHE_KEY));
96
    }
97
98
    /**
99
     * @return int
100
     *
101
     * @throws ItemNotFoundException
102
     */
103 13
    private function getCachedHitCount()
104
    {
105 13
        if (null !== $this->hitCount) {
106 11
            return $this->hitCount;
107
        }
108
109 2
        return $this->cache->get($this->key.self::HITS_CACHE_KEY);
110
    }
111
112
    /**
113
     * @param int $hitCount
114
     */
115 13
    private function setCachedHitCount($hitCount)
116
    {
117 13
        $this->hitCount = $hitCount;
118 13
        $this->cache->set($this->key.self::HITS_CACHE_KEY, $hitCount, $this->cacheTtl);
119 13
    }
120
}
121