Completed
Push — master ( f80d0f...3008ec )
by Nikola
08:08
created

Status::getLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RateLimit;
6
7
use DateTimeImmutable;
8
9
class Status
10
{
11
    /** @var string */
12
    protected $identifier;
13
14
    /** @var int */
15
    protected $current;
16
17
    /** @var Rate */
18
    protected $rate;
19
20
    /** @var DateTimeImmutable */
21
    protected $resetAt;
22
23 10
    final protected function __construct(string $identifier, int $current, Rate $rate, DateTimeImmutable $resetAt)
24
    {
25 10
        $this->identifier = $identifier;
26 10
        $this->current = $current;
27 10
        $this->rate = $rate;
28 10
        $this->resetAt = $resetAt;
29 10
    }
30
31 10
    public static function from(string $identifier, int $current, Rate $rate, DateTimeImmutable $resetAt)
32
    {
33 10
        return new static($identifier, $current, $rate, $resetAt);
34
    }
35
36
    public function getIdentifier(): string
37
    {
38
        return $this->identifier;
39
    }
40
41 2
    public function getCurrent(): int
42
    {
43 2
        return $this->current;
44
    }
45
46 10
    public function getLimit(): int
47
    {
48 10
        return $this->rate->getOperations();
49
    }
50
51 4
    public function getResetAt(): DateTimeImmutable
52
    {
53 4
        return $this->resetAt;
54
    }
55
56 10
    public function limitExceeded(): bool
57
    {
58 10
        return $this->current > $this->getLimit();
59
    }
60
61 10
    public function getRemainingAttempts(): int
62
    {
63 10
        return max(0, $this->getLimit() - $this->current);
64
    }
65
}
66