TimeKeeper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 14
eloc 21
c 2
b 0
f 1
dl 0
loc 68
ccs 29
cts 29
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 3 1
A getExpiration() 0 3 1
A __construct() 0 3 1
A create() 0 3 1
A setExpiration() 0 5 1
A start() 0 3 1
A getRemainingSeconds() 0 9 4
A _getTimeDiff() 0 7 2
A isExpired() 0 8 2
1
<?php
2
3
namespace hamburgscleanest\GuzzleAdvancedThrottle;
4
5
use DateTimeImmutable;
6
7
class TimeKeeper
8
{
9
    private int $_expirationIntervalSeconds;
10
    private DateTimeImmutable | null $_expiresAt = null;
0 ignored issues
show
Bug introduced by
The type hamburgscleanest\GuzzleAdvancedThrottle\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12 44
    public function __construct(int $intervalInSeconds)
13
    {
14 44
        $this->_expirationIntervalSeconds = $intervalInSeconds;
15 44
    }
16
17 1
    public static function create(int $intervalInSeconds): self
18
    {
19 1
        return new static($intervalInSeconds);
20
    }
21
22 28
    public function getExpiration(): ?DateTimeImmutable
23
    {
24 28
        return $this->_expiresAt;
25
    }
26
27 4
    public function setExpiration(DateTimeImmutable $expiresAt): self
28
    {
29 4
        $this->_expiresAt = $expiresAt;
30
31 4
        return $this;
32
    }
33
34 33
    private function _getTimeDiff(): ?int
35
    {
36 33
        if ($this->_expiresAt === null) {
37 1
            return null;
38
        }
39
40 33
        return $this->_expiresAt->getTimestamp() - SystemClock::create()->now()->getTimestamp();
41
    }
42
43 32
    public function getRemainingSeconds(): int
44
    {
45 32
        if ($this->_expiresAt === null) {
46 1
            return $this->_expirationIntervalSeconds;
47
        }
48
49 31
        $diff = $this->_getTimeDiff();
50
51 31
        return $diff !== null && $diff >= 0 ? $diff : $this->_expirationIntervalSeconds;
52
    }
53
54 29
    public function isExpired(): bool
55
    {
56 29
        $diff = $this->_getTimeDiff();
57 29
        if ($diff === null) {
58 1
            return false;
59
        }
60
61 29
        return $diff <= 0;
62
    }
63
64 4
    public function reset(): void
65
    {
66 4
        $this->_expiresAt = null;
67 4
    }
68
69
    /**
70
     * Initialize the expiration date for the request timer.
71
     */
72 35
    public function start(): void
73
    {
74 35
        $this->_expiresAt = SystemClock::create()->advanceSeconds($this->_expirationIntervalSeconds)->now();
75 35
    }
76
}
77