Completed
Pull Request — master (#10)
by Krishnaprasad
08:08 queued 03:56
created

ElasticWindowThrottler::hit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
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\Throttler;
27
28
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException;
29
use Sunspikes\Ratelimit\Cache\ThrottlerCacheInterface;
30
use Sunspikes\Ratelimit\Throttle\Entity\CacheCount;
31
32
class ElasticWindowThrottler implements RetriableThrottlerInterface, \Countable
33
{
34
    /* @var ThrottlerCacheInterface */
35
    protected $cache;
36
    /* @var string */
37
    protected $key;
38
    /* @var int */
39
    protected $limit;
40
    /* @var int */
41
    protected $ttl;
42
    /* @var int */
43
    protected $counter;
44
45
    /**
46
     * @param ThrottlerCacheInterface $cache
47
     * @param string $key
48
     * @param int $limit
49
     * @param int $ttl
50
     */
51 7
    public function __construct(ThrottlerCacheInterface $cache, $key, $limit, $ttl)
52
    {
53 7
        $this->cache = $cache;
54 7
        $this->key = $key;
55 7
        $this->limit = $limit;
56 7
        $this->ttl = $ttl;
57 7
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 2
    public function access()
63
    {
64 2
        $status = $this->check();
65
66 2
        $this->hit();
67
68 2
        return $status;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 5 View Code Duplication
    public function hit()
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...
75
    {
76 5
        $this->counter = $this->count() + 1;
77 5
        $item = new CacheCount($this->counter, $this->ttl);
78
79 5
        $this->cache->setItem($this->key, $item);
80
81 5
        return $this;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 1 View Code Duplication
    public function clear()
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 1
        $this->counter = 0;
90 1
        $item = new CacheCount($this->counter, $this->ttl);
91
92 1
        $this->cache->setItem($this->key, $item);
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100 5
    public function count()
101
    {
102 5
        if (!is_null($this->counter)) {
103 5
            return $this->counter;
104
        }
105
106
        try {
107
            /** @var CacheCount $item */
108 5
            $item = $this->cache->getItem($this->key);
109
            $this->counter = $item->getCount();
110 5
        } catch (ItemNotFoundException $e) {
111 5
            $this->counter = 0;
112
        }
113
114 5
        return $this->counter;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 4
    public function check()
121
    {
122 4
        return ($this->count() < $this->limit);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function getTime()
129
    {
130
        return $this->ttl;
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function getLimit()
137
    {
138
        return $this->limit;
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144
    public function getRetryTimeout()
145
    {
146
        if ($this->check()) {
147
            return 0;
148
        }
149
150
        return self::SECOND_TO_MILLISECOND_MULTIPLIER * $this->ttl;
151
    }
152
}
153