Completed
Push — master ( 2e36b4...671712 )
by Nikola
04:21
created

Status::getIdentifier()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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 bool */
15
    protected $success;
16
17
    /** @var int */
18
    protected $limit;
19
20
    /** @var int */
21
    protected $remainingAttempts;
22
23
    /** @var DateTimeImmutable */
24
    protected $resetAt;
25
26 3
    final protected function __construct(string $identifier, bool $success, int $limit, int $remainingAttempts, DateTimeImmutable $resetAt)
27
    {
28 3
        $this->identifier = $identifier;
29 3
        $this->success = $success;
30 3
        $this->limit = $limit;
31 3
        $this->remainingAttempts = $remainingAttempts;
32 3
        $this->resetAt = $resetAt;
33 3
    }
34
35 3
    public static function from(string $identifier, int $current, int $limit, int $resetTime)
36
    {
37 3
        return new static(
38 3
            $identifier,
39 3
            $current <= $limit,
40 3
            $limit,
41 3
            max(0, $limit - $current),
42 3
            new DateTimeImmutable("@$resetTime")
43
        );
44
    }
45
46 1
    public function getIdentifier(): string
47
    {
48 1
        return $this->identifier;
49
    }
50
51 3
    public function limitExceeded(): bool
52
    {
53 3
        return !$this->success;
54
    }
55
56 1
    public function getLimit(): int
57
    {
58 1
        return $this->limit;
59
    }
60
61 3
    public function getRemainingAttempts(): int
62
    {
63 3
        return $this->remainingAttempts;
64
    }
65
66
    public function getResetAt(): DateTimeImmutable
67
    {
68
        return $this->resetAt;
69
    }
70
}
71