Timer::cancel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Dazzle\Loop\Timer;
4
5
use Dazzle\Loop\LoopModelInterface;
6
7
class Timer implements TimerInterface
8
{
9
    /**
10
     * @var float
11
     */
12
    const MIN_INTERVAL = 1e-6;
13
14
    /**
15
     * @var LoopModelInterface
16
     */
17
    protected $loop;
18
19
    /**
20
     * @var float
21
     */
22
    protected $interval;
23
24
    /**
25
     * @var callable
26
     */
27
    protected $callback;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $periodic;
33
34
    /**
35
     * @var mixed|null
36
     */
37
    protected $data;
38
39
    /**
40
     * @param LoopModelInterface $loop
41
     * @param $interval
42
     * @param callable $callback
43
     * @param bool $periodic
44
     * @param mixed|null $data
45
     */
46 42
    public function __construct(LoopModelInterface $loop, $interval, callable $callback, $periodic = false, $data = null)
47
    {
48 42
        if ($interval < self::MIN_INTERVAL)
49
        {
50 3
            $interval = self::MIN_INTERVAL;
51
        }
52
53 42
        $this->loop = $loop;
54 42
        $this->interval = (float) $interval;
55 42
        $this->callback = $callback;
56 42
        $this->periodic = (bool) $periodic;
57 42
        $this->data = $data;
58 42
    }
59
60
    /**
61
     *
62
     */
63 26
    public function __destruct()
64
    {
65 26
        unset($this->loop);
66 26
        unset($this->interval);
67 26
        unset($this->callback);
68 26
        unset($this->periodic);
69 26
        unset($this->data);
70 26
    }
71
72
    /**
73
     * @override
74
     * @inheritDoc
75
     */
76 1
    public function getLoop()
77
    {
78 1
        return $this->loop;
79
    }
80
81
    /**
82
     * @override
83
     * @inheritDoc
84
     */
85 22
    public function getInterval()
86
    {
87 22
        return $this->interval;
88
    }
89
90
    /**
91
     * @override
92
     * @inheritDoc
93
     */
94 5
    public function getCallback()
95
    {
96 5
        return $this->callback;
97
    }
98
99
    /**
100
     * @override
101
     * @inheritDoc
102
     */
103 2
    public function getData()
104
    {
105 2
        return $this->data;
106
    }
107
108
    /**
109
     * @override
110
     * @inheritDoc
111
     */
112 1
    public function setData($data)
113
    {
114 1
        $this->data = $data;
115 1
    }
116
117
    /**
118
     * @override
119
     * @inheritDoc
120
     */
121 6
    public function isPeriodic()
122
    {
123 6
        return $this->periodic;
124
    }
125
126
    /**
127
     * @override
128
     * @inheritDoc
129
     */
130 1
    public function isActive()
131
    {
132 1
        return $this->loop->isTimerActive($this);
133
    }
134
135
    /**
136
     * @override
137
     * @inheritDoc
138
     */
139 1
    public function cancel()
140
    {
141 1
        if (isset($this->loop))
142
        {
143 1
            $this->loop->cancelTimer($this);
144
        }
145 1
    }
146
}
147