| 1 | <?php |
||
| 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 |
|
| 65 | |||
| 66 | public function getResetAt(): DateTimeImmutable |
||
| 67 | { |
||
| 68 | return $this->resetAt; |
||
| 70 | } |
||
| 71 |