Completed
Push — master ( 415c00...5e3462 )
by Nikola
13s queued 11s
created

Rate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 52
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

8 Methods

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