Completed
Push — master ( 7353e9...0c9da4 )
by BENOIT
01:14
created

Counter::isExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BenTools\GuzzleHttp\Middleware\Storage;
4
5
class Counter implements \Serializable, \JsonSerializable, \Countable
6
{
7
    /**
8
     * @var float
9
     */
10
    private $expiresIn;
11
12
    /**
13
     * @var float
14
     */
15
    private $expiresAt;
16
17
    /**
18
     * @var int
19
     */
20
    private $counter;
21
22
    /**
23
     * Counter constructor.
24
     * @param float $startTime
0 ignored issues
show
Bug introduced by
There is no parameter named $startTime. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     * @param int $nbRequests
0 ignored issues
show
Bug introduced by
There is no parameter named $nbRequests. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     */
27
    public function __construct(float $expiresIn)
28
    {
29
        $this->expiresIn = $expiresIn;
30
        $this->reset();
31
    }
32
33
    private function reset()
34
    {
35
        $this->counter = 0;
36
        $this->expiresAt = microtime(true) + $this->expiresIn;
37
    }
38
39
    /**
40
     * Increment counter.
41
     */
42
    public function increment()
43
    {
44
        if ($this->isExpired()) {
45
            $this->reset();
46
        } else {
47
            $this->counter++;
48
        }
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function count(): int
55
    {
56
        if ($this->isExpired()) {
57
            $this->reset();
58
        }
59
        return $this->counter;
60
    }
61
62
    /**
63
     * @return float
64
     */
65
    public function getRemainingTime()
66
    {
67
        return (float) max(0, $this->expiresAt - microtime(true));
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isExpired()
74
    {
75
        return 0.0 === $this->getRemainingTime();
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function serialize()
82
    {
83
        return json_encode($this);
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function unserialize($serialized)
90
    {
91
        $data = json_decode($serialized, true);
92
        $this->expiresAt = $data['e'];
93
        $this->expiresIn = $data['i'];
94
        $this->counter = $data['n'];
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100
    public function jsonSerialize()
101
    {
102
        return [
103
            'i' => $this->expiresIn,
104
            'e' => $this->expiresAt,
105
            'n' => $this->counter,
106
        ];
107
    }
108
}
109