Completed
Push — master ( 0484f7...624dd7 )
by Nikola
09:44
created

Status   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 56
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A from() 0 4 1
A getIdentifier() 0 4 1
A limitExceeded() 0 4 1
A getLimit() 0 4 1
A getRemainingAttempts() 0 4 1
A getResetAt() 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 10
    /** @var DateTimeImmutable */
24
    protected $resetAt;
25 10
26 10
    final protected function __construct(string $identifier, bool $success, int $limit, int $remainingAttempts, DateTimeImmutable $resetAt)
27 10
    {
28 10
        $this->identifier = $identifier;
29 10
        $this->success = $success;
30
        $this->limit = $limit;
31 10
        $this->remainingAttempts = $remainingAttempts;
32
        $this->resetAt = $resetAt;
33 10
    }
34
35
    public static function from(string $identifier, int $current, int $limit, DateTimeImmutable $resetAt)
36
    {
37
        return new static($identifier, $current <= $limit, $limit, max(0, $limit - $current), $resetAt);
38
    }
39
40
    public function getIdentifier(): string
41 2
    {
42
        return $this->identifier;
43 2
    }
44
45
    public function limitExceeded(): bool
46 10
    {
47
        return !$this->success;
48 10
    }
49
50
    public function getLimit(): int
51 4
    {
52
        return $this->limit;
53 4
    }
54
55
    public function getRemainingAttempts(): int
56 10
    {
57
        return $this->remainingAttempts;
58 10
    }
59
60
    public function getResetAt(): DateTimeImmutable
61 10
    {
62
        return $this->resetAt;
63 10
    }
64
}
65