Passed
Push — master ( 53910c...89ff52 )
by Biao
03:18
created

CronJob::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Timer;
4
5
abstract class CronJob implements CronJobInterface
6
{
7
    /**
8
     * Swoole timer id
9
     * @var int
10
     */
11
    protected $timerId;
12
13
    /**
14
     * The interval of Job in millisecond
15
     * @var int
16
     */
17
    protected $interval;
18
19
    /**
20
     * Whether run immediately after start
21
     * @var bool
22
     */
23
    protected $isImmediate;
24
25
    /**
26
     * CronJob constructor.
27
     * Optional:
28
     *     argument 1 is interval, int ms, default null, overridden by method interval()
29
     *     argument 2 is isImmediate, bool, default false, overridden by method isImmediate()
30
     */
31
    public function __construct()
32
    {
33
        $args = func_get_args();
34
        if (array_key_exists(0, $args)) {
35
            $this->interval = $args[0];
36
        }
37
        if (array_key_exists(1, $args)) {
38
            $this->isImmediate = $args[1];
39
        }
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function interval()
46
    {
47
        return $this->interval;
48
    }
49
50
    /**
51
     * @return bool $isImmediate
52
     */
53
    public function isImmediate()
54
    {
55
        return $this->isImmediate;
56
    }
57
58
    public function setTimerId($timerId)
59
    {
60
        $this->timerId = $timerId;
61
    }
62
63
    public function stop()
64
    {
65
        if (!empty($this->timerId)) {
66
            \swoole_timer_clear($this->timerId);
67
        }
68
    }
69
70
}