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

Rate::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.1406
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