Passed
Push — master ( 25257e...5200e3 )
by Alec
04:00
created

EventCounterDeprecated::reset()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 03.11.18
5
 * Time: 10:26
6
 */
7
declare(strict_types=1);
8
9
namespace AlecRabbit\Event;
10
11
use AlecRabbit\Counters\TimedCounterDeprecated;
12
13
class EventCounterDeprecated extends TimedCounterDeprecated
0 ignored issues
show
Deprecated Code introduced by
The class AlecRabbit\Counters\TimedCounterDeprecated has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

13
class EventCounterDeprecated extends /** @scrutinizer ignore-deprecated */ TimedCounterDeprecated
Loading history...
14
{
15
    /** @var array */
16
    protected $events = [];
17
18
    /**
19
     * @param int|null $time
20
     */
21 17
    public function addEvent(?int $time = null): void
22
    {
23 17
        $time = $this->getBaseTime($time);
24
        // Is there any event during [$time] period? If not initialize with 0
25 17
        $this->events[$time] = $this->events[$time] ?? 0;
26 17
        $this->events[$time]++;
27 17
        $this->trim();
28 17
    }
29
30 17
    private function trim(): void
31
    {
32 17
        if (null !== ($key = array_key_first($this->events))
33 17
            && ($key <= $this->getThreshold())) {
34 14
            unset($this->events[$key]);
35
        }
36 17
    }
37
38
    /**
39
     * @param bool|null $reset
40
     * @return int
41
     */
42 4
    public function getCalculatedEvents(?bool $reset = null): int
43
    {
44 4
        $r = 0;
45 4
        if (0 < ($sum = (int)array_sum($this->events))) {
46 4
            $r = $sum;
47
        }
48 4
        if ($reset) {
49 2
            $this->reset();
50
        }
51 4
        return $r;
52
    }
53
54 2
    protected function reset(): void
55
    {
56 2
        $this->events = [];
57 2
    }
58
59
    /**
60
     * @return array
61
     */
62 3
    public function getRawEventsData(): array
63
    {
64 3
        return $this->events;
65
    }
66
}
67