Counter::resetsIn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Zenstruck\Governator;
4
5
/**
6
 * @author Kevin Bond <[email protected]>
7
 */
8
final class Counter
9
{
10
    private int $hits;
11
    private int $resetsAt;
12
13 117
    public function __construct(int $hits, int $resetsAt)
14
    {
15 117
        $this->hits = $hits;
16 117
        $this->resetsAt = $resetsAt;
17 117
    }
18
19 114
    public function hits(): int
20
    {
21 114
        return $this->hits;
22
    }
23
24 37
    public function resetsAt(): int
25
    {
26 37
        $currentTime = time();
27
28 37
        return $this->resetsAt < $currentTime ? $currentTime : $this->resetsAt;
29
    }
30
31 108
    public function resetsIn(): int
32
    {
33 108
        return \max(0, $this->resetsAt - time());
34
    }
35
36 42
    public function addHit(): self
37
    {
38 42
        $clone = clone $this;
39 42
        ++$clone->hits;
40
41 42
        return $clone;
42
    }
43
}
44