Passed
Push — master ( 93c262...ab8fda )
by Alec
02:30
created

EventIndicator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 49
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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