EventIndicator::current()   A
last analyzed

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
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Date: 16.11.18
4
 * Time: 13:41
5
 */
6
7
namespace AlecRabbit\Event;
8
9
class EventIndicator
10
{
11
    public const THRESHOLD = SECONDS_IN_01MIN;
12
13
    /** @var int */
14
    private $lastEventTimestamp;
15
    /** @var int */
16
    private $threshold;
17
18
    /**
19
     * EventIndicator constructor.
20
     * @param int|null $threshold
21
     */
22 9
    public function __construct(? int $threshold = null)
23
    {
24 9
        $this->countEvent();
25 9
        $this->threshold = $threshold ?? static::THRESHOLD;
26 9
    }
27
28 9
    public function countEvent($event = null)
29
    {
30 9
        $this->lastEventTimestamp = $this->current();
31 9
        return $event;
32
    }
33
34
    /**
35
     * @return int
36
     */
37 9
    private function current(): int
38
    {
39 9
        return time();
40
    }
41
42
    /**
43
     * @return bool
44
     */
45 2
    public function isNotOk(): bool
46
    {
47
        return
48 2
            !$this->isOk();
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 2
    public function isOk(): bool
55
    {
56
        return
57 2
            $this->current() - $this->lastEventTimestamp <= $this->threshold;
58
    }
59
}
60