Completed
Pull Request — master (#23)
by Julián
04:25
created

Rate   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 57
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A perSecond() 0 4 1
A perMinute() 0 4 1
A perHour() 0 4 1
A perDay() 0 4 1
A custom() 0 4 1
A getOperations() 0 4 1
A getInterval() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RateLimit;
6
7
class Rate
8
{
9
    /** @var int */
10
    protected $operations;
11
12
    /** @var int */
13
    protected $interval;
14
15 10
    final protected function __construct(int $operations, int $interval)
16
    {
17 10
        if ($operations <= 0) {
18
            throw new \InvalidArgumentException('Quota must be greater than zero');
19
        }
20
21 10
        if ($interval <= 0) {
22
            throw new \InvalidArgumentException('Seconds interval must be greater than zero');
23
        }
24
25 10
        $this->operations = $operations;
26 10
        $this->interval = $interval;
27 10
    }
28
29 3
    public static function perSecond(int $operations)
30
    {
31 3
        return new static($operations, 1);
32
    }
33
34 2
    public static function perMinute(int $operations)
35
    {
36 2
        return new static($operations, 60);
37
    }
38
39 3
    public static function perHour(int $operations)
40
    {
41 3
        return new static($operations, 3600);
42
    }
43
44 1
    public static function perDay(int $operations)
45
    {
46 1
        return new static($operations, 86400);
47
    }
48
49 1
    public static function custom(int $operations, int $interval)
50
    {
51 1
        return new static($operations, $interval);
52
    }
53
54 10
    public function getOperations(): int
55
    {
56 10
        return $this->operations;
57
    }
58
59 10
    public function getInterval(): int
60
    {
61 10
        return $this->interval;
62
    }
63
}
64