Completed
Push — master ( c5c0ad...765df9 )
by Freek
01:12
created

RateLimited::timespanInSeconds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\RateLimitedMiddleware;
4
5
use Closure;
6
use Illuminate\Support\Facades\Redis;
7
8
class RateLimited
9
{
10
    /** @var bool/\Closure */
11
    protected $enabled = true;
12
13
    /** @var string */
14
    protected $connectionName = '';
15
16
    /** @var string */
17
    protected $key;
18
19
    /** @var int */
20
    protected $timeSpanInSeconds = 1;
21
22
    /** @var int */
23
    protected $allowedNumberOfJobsInTimeSpan = 5;
24
25
    /** @var int */
26
    protected $releaseInSeconds = 5;
27
28
    public function __construct()
29
    {
30
        $calledByClass = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'];
31
32
        $this->key($calledByClass);
33
    }
34
35
    /**
36
     * @param bool|\Closure $enabled
37
     *
38
     * @return $this
39
     */
40
    public function enabled($enabled = true)
41
    {
42
        $this->enabled = $enabled;
0 ignored issues
show
Documentation Bug introduced by
It seems like $enabled can also be of type object<Closure>. However, the property $enabled is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43
44
        return $this;
45
    }
46
47
    public function connectionName(string $connectionName)
48
    {
49
        $this->connectionName = $connectionName;
50
51
        return $this;
52
    }
53
54
    public function key(string $key)
55
    {
56
        $this->key = $key;
57
58
        return $this;
59
    }
60
61
    public function timespanInSeconds(int $timespanInSeconds)
62
    {
63
        $this->timeSpanInSeconds = $timespanInSeconds;
64
65
        return $this;
66
    }
67
68
    public function allow(int $allowedNumberOfJobsInTimeSpan)
69
    {
70
        $this->allowedNumberOfJobsInTimeSpan = $allowedNumberOfJobsInTimeSpan;
71
72
        return $this;
73
    }
74
75
    public function everySecond(int $timespanInSeconds = 1)
76
    {
77
        $this->timeSpanInSeconds = $timespanInSeconds;
78
79
        return $this;
80
    }
81
82
    public function everySeconds(int $timespanInSeconds)
83
    {
84
        return $this->everySecond($timespanInSeconds);
85
    }
86
87
    public function everyMinute(int $timespanInMinutes = 1)
88
    {
89
        return $this->everySecond($timespanInMinutes * 60);
90
    }
91
92
    public function everyMinutes(int $timespanInMinutes)
93
    {
94
        return $this->everySecond($timespanInMinutes * 60);
95
    }
96
97
    public function releaseAfterOneSecond()
98
    {
99
        return $this->releaseAfterSeconds(1);
100
    }
101
102
    public function releaseAfterSeconds(int $releaseInSeconds)
103
    {
104
        $this->releaseInSeconds = $releaseInSeconds;
105
106
        return $this;
107
    }
108
109
    public function releaseAfterOneMinute()
110
    {
111
        return $this->releaseAfterMinutes(1);
112
    }
113
114
    public function releaseAfterMinutes(int $releaseInSeconds)
115
    {
116
        return $this->releaseAfterSeconds($releaseInSeconds * 60);
117
    }
118
119
    public function handle($job, $next)
120
    {
121
        if ($this->enabled instanceof Closure) {
122
            $this->enabled = (bool)$this->enabled();
123
        }
124
125
        if (! $this->enabled) {
126
            return $next($job);
127
        }
128
129
        Redis::connection($this->connectionName)
130
            ->throttle($this->key)
131
            ->block(0)
132
            ->allow($this->allowedNumberOfJobsInTimeSpan)
133
            ->every($this->timeSpanInSeconds)
134
            ->then(function () use ($job, $next) {
135
                $next($job);
136
            }, function () use ($job) {
137
                $job->release($this->releaseInSeconds);
138
            });
139
    }
140
}
141