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

TimedCounterDeprecated::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
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
/**
14
 * Class TimedCounterDeprecated
15
 * @package AlecRabbit\Counters
16
 * @deprecated
17
 */
18
class TimedCounterDeprecated
19
{
20
    protected const DEFAULT_NAME = 'default';
21
    protected const DEFAULT_LENGTH = 3600;
22
    protected const DEFAULT_GROUP_BY = 60;
23
24
    use Nameable;
25
26
    /** @var int */
27
    protected $length;
28
29
    /** @var int|null */
30
    protected $groupBy;
31
32
    /** @var int */
33
    protected $lastTimestamp;
34
35
    /** @var bool */
36
    protected $relativeMode;
37
38
    /**
39
     * BasicCounter constructor.
40
     * @param int|null $length
41
     * @param int|null $groupBy
42
     * @param bool $relativeMode
43
     */
44 32
    public function __construct(?int $length = P_01HOUR, ?int $groupBy = null, bool $relativeMode = false)
45
    {
46 32
        $this->setName(static::DEFAULT_NAME);
47
48 32
        $this->length = $length ?? static::DEFAULT_LENGTH;
49 32
        $this->groupBy = $groupBy;
50 32
        $this->relativeMode = $relativeMode;
51 32
        $this->lastTimestamp = 0;
52 32
    }
53
54
    /**
55
     * Enables relative time mode
56
     * @return self
57
     */
58 2
    public function enableRelativeMode(): self
59
    {
60 2
        return $this->setRelativeMode(true);
61
    }
62
63
    /**
64
     * Set to this mode to count by timestamps.
65
     *
66
     * @param bool $relative
67
     * @return self
68
     */
69 15
    public function setRelativeMode(bool $relative = true): self
70
    {
71 15
        $this->relativeMode = $relative;
72 15
        return $this;
73
    }
74
75
    /**
76
     * Disables relative time mode
77
     * @return self
78
     */
79 1
    public function disableRelativeMode(): self
80
    {
81 1
        return $this->setRelativeMode(false);
82
    }
83
84
    /**
85
     * @param int|null $timestamp
86
     * @return int
87
     */
88 30
    protected function getBaseTime(?int $timestamp = null): int
89
    {
90 30
        $this->lastTimestamp = $time = $timestamp ?? time();
91 30
        if (null !== $this->groupBy && $this->groupBy > ONE_SECOND) {
92 22
            $time = base_timestamp($time, $this->groupBy);
93
        }
94 30
        return $time;
95
    }
96
97
    /**
98
     * @return int
99
     */
100 17
    protected function getTime(): int
101
    {
102
        return
103 17
            $this->relativeMode ? $this->lastTimestamp : time();
104
    }
105
106
    /**
107
     * @return int
108
     */
109 17
    protected function getThreshold(): int
110
    {
111
        return
112 17
            $this->getTime() - $this->length;
113
    }
114
}
115