Completed
Push — master ( 5e3462...974d4d )
by Nikola
04:09
created

Rate   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 52
ccs 20
cts 20
cp 1
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 getOperations() 0 4 1
A getInterval() 0 4 1
A perDay() 0 4 1
A custom() 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 10
    final protected function __construct(int $operations, int $interval)
18
    {
19 10
        Assertion::greaterThan($operations, 0, 'Quota must be greater than zero');
20 10
        Assertion::greaterThan($interval, 0, 'Seconds interval must be greater than zero');
21
22 10
        $this->operations = $operations;
23 10
        $this->interval = $interval;
24 10
    }
25
26 3
    public static function perSecond(int $operations)
27
    {
28 3
        return new static($operations, 1);
29
    }
30
31 2
    public static function perMinute(int $operations)
32
    {
33 2
        return new static($operations, 60);
34
    }
35
36 3
    public static function perHour(int $operations)
37
    {
38 3
        return new static($operations, 3600);
39
    }
40
41 1
    public static function perDay(int $operations)
42
    {
43 1
        return new static($operations, 86400);
44
    }
45
46 1
    public static function custom(int $operations, int $interval)
47
    {
48 1
        return new static($operations, $interval);
49
    }
50
51 10
    public function getOperations(): int
52
    {
53 10
        return $this->operations;
54
    }
55
56 10
    public function getInterval(): int
57
    {
58 10
        return $this->interval;
59
    }
60
}
61