TimerBox::contains()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Dazzle\Loop\Timer;
4
5
use SplObjectStorage;
6
use SplPriorityQueue;
7
8
class TimerBox
9
{
10
    /**
11
     * @var float
12
     */
13
    protected $time;
14
15
    /**
16
     * @var SplObjectStorage
17
     */
18
    protected $timers;
19
20
    /**
21
     * @var SplPriorityQueue
22
     */
23
    protected $scheduler;
24
25
    /**
26
     *
27
     */
28 67
    public function __construct()
29
    {
30 67
        $this->timers = new SplObjectStorage();
31 67
        $this->scheduler = new SplPriorityQueue();
32 67
    }
33
34
    /**
35
     * @return float
36
     */
37 48
    public function updateTime()
38
    {
39 48
        return $this->time = microtime(true);
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45 22
    public function getTime()
46
    {
47 22
        return $this->time ?: $this->updateTime();
48
    }
49
50
    /**
51
     * @param TimerInterface $timer
52
     * @return bool
53
     */
54 9
    public function contains(TimerInterface $timer)
55
    {
56 9
        return $this->timers->contains($timer);
57
    }
58
59
    /**
60
     * @param TimerInterface $timer
61
     */
62 20
    public function add(TimerInterface $timer)
63
    {
64 20
        $interval = $timer->getInterval();
65 20
        $scheduledAt = $interval + $this->getTime();
66
67 20
        $this->timers->attach($timer, $scheduledAt);
68 20
        $this->scheduler->insert($timer, -$scheduledAt);
69 20
    }
70
71
    /**
72
     * @param TimerInterface $timer
73
     */
74 6
    public function remove(TimerInterface $timer)
75
    {
76 6
        $this->timers->detach($timer);
77 6
    }
78
79
    /**
80
     * @return TimerInterface|null
81
     */
82 5
    public function getFirst()
83
    {
84 5
        while ($this->scheduler->count())
85
        {
86 4
            $timer = $this->scheduler->top();
87
88 4
            if ($this->timers->contains($timer))
89
            {
90 4
                return $this->timers[$timer];
91
            }
92
93
            $this->scheduler->extract();
94
        }
95
96 1
        return null;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102 2
    public function isEmpty()
103
    {
104 2
        return count($this->timers) === 0;
105
    }
106
107
    /**
108
     *
109
     */
110 38
    public function tick()
111
    {
112 38
        $time = $this->updateTime();
113 38
        $timers = $this->timers;
114 38
        $scheduler = $this->scheduler;
115
116 38
        while (!$scheduler->isEmpty())
117
        {
118 12
            $timer = $scheduler->top();
119
120 12
            if (!isset($timers[$timer]))
121
            {
122 2
                $scheduler->extract();
123 2
                $timers->detach($timer);
124 2
                continue;
125
            }
126
127 10
            if ($timers[$timer] >= $time)
128
            {
129 10
                break;
130
            }
131
132 4
            $scheduler->extract();
133
134 4
            $callback = $timer->getCallback();
135 4
            $callback($timer);
136
137 4
            if ($timer->isPeriodic() && isset($timers[$timer]))
138
            {
139 2
                $timers[$timer] = $scheduledAt = $timer->getInterval() + $time;
140 2
                $scheduler->insert($timer, -$scheduledAt);
141
            }
142
            else
143
            {
144 2
                $timers->detach($timer);
145
            }
146
        }
147 38
    }
148
}
149