Completed
Pull Request — master (#9)
by Krishnaprasad
10:11
created

LeakyBucketThrottler::setUsedCapacity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
final class LeakyBucketThrottler extends AbstractWindowThrottler implements ThrottlerInterface
29
{
30
    /**
31
     * @inheritdoc
32
     */
33
    public function hit(): ThrottlerInterface
34
    {
35
        $tokenCount = $this->count();
36
37
        $this->setUsedCapacity($tokenCount + 1);
38
39
        if (0 < $wait = $this->getWaitTime($tokenCount)) {
40
            $this->timeProvider->usleep(self::MILLISECOND_TO_MICROSECOND_MULTIPLIER * $wait);
0 ignored issues
show
Bug introduced by
The property timeProvider does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        }
42
43
        return $wait;
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function count()
50
    {
51
        try {
52
            $cachedTime = $this->cache->get($this->getTimeCacheKey());
0 ignored issues
show
Bug introduced by
The property cache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
            $timeSinceLastRequest = self::SECOND_TO_MILLISECOND_MULTIPLIER * ($this->timeProvider->now() - $cachedTime);
54
55
            if ($timeSinceLastRequest > $this->timeLimit) {
0 ignored issues
show
Bug introduced by
The property timeLimit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
                return 0;
57
            }
58
59
            $lastTokenCount = $this->cache->get($this->getTokenCacheKey());
60
        } catch (ItemNotFoundException $exception) {
0 ignored issues
show
Bug introduced by
The class Sunspikes\Ratelimit\Thro...r\ItemNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
61
            $this->clear(); //Clear the bucket
62
63
            return 0;
64
        }
65
66
        // Return the `used` token count, minus the amount of tokens which have been `refilled` since the previous request
67
        return  (int) max(0, ceil($lastTokenCount - ($this->tokenlimit * $timeSinceLastRequest / ($this->timeLimit))));
0 ignored issues
show
Bug introduced by
The property tokenlimit does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function check()
74
    {
75
        return 0 === $this->getWaitTime($this->count());
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 16
    public function getTime()
82
    {
83
        return $this->timeLimit;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function getLimit()
90 16
    {
91 16
        return $this->tokenlimit;
92 16
    }
93 16
94 16
    /**
95 16
     * @inheritdoc
96 16
     */
97 16 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...
98
    {
99
        if ($this->threshold > $this->count() + 1) {
0 ignored issues
show
Bug introduced by
The property threshold does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
            return 0;
101
        }
102 3
103
        return (int) ceil($this->timeLimit / $this->tokenlimit);
104 3
    }
105
106
    /**
107
     * @param int $tokenCount
108
     *
109
     * @return int
110 8
     */
111 View Code Duplication
    private function getWaitTime($tokenCount)
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...
112 8
    {
113
        if ($this->threshold > $tokenCount) {
114 8
            return 0;
115
        }
116 8
117 2
        return (int) ceil($this->timeLimit / max(1, ($this->tokenlimit - $this->threshold)));
118 2
    }
119
120 8
    /**
121
     * @param int $tokens
122
     */
123
    private function setUsedCapacity($tokens)
124
    {
125
        $this->cache->set($this->getTokenCacheKey(), $tokens, $this->cacheTtl);
0 ignored issues
show
Bug introduced by
The property cacheTtl does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
126 7
        $this->cache->set($this->getTimeCacheKey(), $this->timeProvider->now(), $this->cacheTtl);
127
    }
128 7
129 7
    /**
130
     * @return string
131
     */
132
    private function getTokenCacheKey()
133
    {
134 14
        return $this->key.self::CACHE_KEY_TOKEN;
0 ignored issues
show
Bug introduced by
The property key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
135
    }
136
137 14
    /**
138 13
     * @return string
139
     */
140 13
    private function getTimeCacheKey()
141 4
    {
142
        return $this->key.self::CACHE_KEY_TIME;
143
    }
144
}
145