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

Status   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.47%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 56
ccs 17
cts 19
cp 0.8947
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A limitExceeded() 0 4 1
A getLimit() 0 4 1
A getRemainingAttempts() 0 4 1
A getResetAt() 0 4 1
A __construct() 0 8 1
A from() 0 4 1
A getIdentifier() 0 4 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