Completed
Push — master ( f8807b...5769db )
by Nikola
06:22
created

Status   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 57
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A from() 0 4 1
A getIdentifier() 0 4 1
A getCurrent() 0 4 1
A getQuota() 0 4 1
A getResetAt() 0 4 1
A quotaExceeded() 0 4 1
A getRemainingAttempts() 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 int */
15
    protected $current;
16
17
    /** @var QuotaPolicy */
18
    protected $quotaPolicy;
19
20
    /** @var DateTimeImmutable */
21
    protected $resetAt;
22
23
    final protected function __construct(string $identifier, int $current, QuotaPolicy $quotaPolicy, DateTimeImmutable $resetAt)
24
    {
25
        $this->identifier = $identifier;
26
        $this->current = $current;
27
        $this->quotaPolicy = $quotaPolicy;
28
        $this->resetAt = $resetAt;
29
    }
30
31
    public static function from(string $identifier, int $current, QuotaPolicy $quotaPolicy, DateTimeImmutable $resetAt)
32
    {
33
        return new static($identifier, $current, $quotaPolicy, $resetAt);
34
    }
35
36
    public function getIdentifier(): string
37
    {
38
        return $this->identifier;
39
    }
40
41
    public function getCurrent(): int
42
    {
43
        return $this->current;
44
    }
45
46
    public function getQuota(): int
47
    {
48
        return $this->quotaPolicy->getQuota();
49
    }
50
51
    public function getResetAt(): DateTimeImmutable
52
    {
53
        return $this->resetAt;
54
    }
55
56
    public function quotaExceeded(): bool
57
    {
58
        return $this->current > $this->getQuota();
59
    }
60
61
    public function getRemainingAttempts(): int
62
    {
63
        return max(0, $this->getQuota() - $this->current);
64
    }
65
}
66