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

TimedCounter::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 19.11.18
5
 * Time: 17:30
6
 */
7
declare(strict_types=1);
8
9
namespace AlecRabbit\Counters;
10
11
use AlecRabbit\Traits\Nameable;
12
13
class TimedCounter
14
{
15
    use Nameable;
16
17
    /** @var int */
18
    protected $lastTimestamp;
19
20
    /** @var array */
21
    protected $data = [];
22
23
    /** @var bool */
24
    protected $relativeMode = false;
25
26
    /** @var array */
27
    protected $periods;
28
29
    /**
30
     * BasicCounter constructor.
31
     * @param array|null $periods
32
     */
33 20
    public function __construct(?array $periods = null)
34
    {
35 20
        $this->setName(DEFAULT_NAME);
36 20
        $this->lastTimestamp = 0;
37 20
        $this->periods = $periods ?? PERIODS;
38 20
    }
39
40
    /**
41
     * Enables relative time mode
42
     * @return self
43
     */
44 17
    public function enableRelativeMode(): self
45
    {
46 17
        $this->relativeMode = true;
47 17
        return $this;
48
    }
49
50
    /**
51
     * @param int|null $time
52
     */
53 16
    public function add(?int $time = null): void
54
    {
55 16
        $baseTimes = $this->getBaseTimes($time);
56 16
        foreach ($baseTimes as $period => $timestamp) {
57 16
            $this->data[$period][$timestamp] =
58 16
                $this->data[$period][$timestamp] ?? 0;
59 16
            $this->data[$period][$timestamp]++;
60 16
            $this->trim($period);
61
        }
62 16
    }
63
64
    /**
65
     * @param int|null $timestamp
66
     * @return array
67
     */
68 19
    protected function getBaseTimes(?int $timestamp = null): array
69
    {
70 19
        $this->lastTimestamp = $time = $timestamp ?? time();
71 19
        $baseTimes = [];
72 19
        foreach ($this->periods as $period => $groupBy) {
73 19
            $baseTimes[$period] = base_timestamp($time, $groupBy);
74
        }
75 19
        return $baseTimes;
76
    }
77
78
    /**
79
     * @param int $period
80
     */
81 16
    protected function trim(int $period): void
82
    {
83 16
        if (null !== ($key = array_key_first($this->data[$period]))
84 16
            && ($key <= $this->getThreshold($period))) {
85 12
            unset($this->data[$period][$key]);
86
        }
87 16
    }
88
89
    /**
90
     * @param int $period
91
     * @return int
92
     */
93 19
    protected function getThreshold(int $period): int
94
    {
95
        return
96 19
            $this->getTime() - $period;
97
    }
98
99
    /**
100
     * @return int
101
     */
102 19
    protected function getTime(): int
103
    {
104
        return
105 19
            $this->relativeMode ? $this->lastTimestamp : time();
106
    }
107
108
    /**
109
     * @param bool|null $reset
110
     * @return array
111
     */
112 16
    public function getDataArray(?bool $reset = null): array
113
    {
114 16
        $r = [];
115 16
        foreach ($this->periods as $period => $groupBy) {
116 16
            if (0 < ($sum = array_sum($this->data[$period] ?? []))) {
117 16
                $r[$period] = $sum;
118
            }
119
        }
120 16
        if ($reset) {
121 1
            $this->reset();
122
        }
123 16
        return $r;
124
    }
125
126
    /**
127
     * Resets all data
128
     */
129 2
    protected function reset(): void
130
    {
131 2
        $this->data = [];
132 2
    }
133
134
    /**
135
     * @param bool|null $reset
136
     * @return object
137
     */
138 4
    public function getDataObject(?bool $reset = null): object
139
    {
140 4
        $r = new \stdClass();
141 4
        foreach ($this->periods as $period => $groupBy) {
142 4
            if (0 < ($sum = array_sum($this->data[$period] ?? []))) {
143 4
                $r->{$period} = $sum;
144
            }
145
        }
146 4
        if ($reset) {
147 1
            $this->reset();
148
        }
149 4
        return $r;
150
    }
151
152
    /**
153
     * @return array
154
     */
155 18
    public function getRawData(): array
156
    {
157 18
        return $this->data;
158
    }
159
}
160