Completed
Push — master ( 624dd7...696191 )
by Nikola
04:34
created

Status::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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 7
    final protected function __construct(string $identifier, bool $success, int $limit, int $remainingAttempts, DateTimeImmutable $resetAt)
27
    {
28 7
        $this->identifier = $identifier;
29 7
        $this->success = $success;
30 7
        $this->limit = $limit;
31 7
        $this->remainingAttempts = $remainingAttempts;
32 7
        $this->resetAt = $resetAt;
33 7
    }
34
35 7
    public static function from(string $identifier, int $current, int $limit, DateTimeImmutable $resetAt)
36
    {
37 7
        return new static($identifier, $current <= $limit, $limit, max(0, $limit - $current), $resetAt);
38
    }
39
40
    public function getIdentifier(): string
41
    {
42
        return $this->identifier;
43
    }
44
45 7
    public function limitExceeded(): bool
46
    {
47 7
        return !$this->success;
48
    }
49
50 4
    public function getLimit(): int
51
    {
52 4
        return $this->limit;
53
    }
54
55 7
    public function getRemainingAttempts(): int
56
    {
57 7
        return $this->remainingAttempts;
58
    }
59
60 4
    public function getResetAt(): DateTimeImmutable
61
    {
62 4
        return $this->resetAt;
63
    }
64
}
65