Passed
Branch master (eb5e46)
by Rick
01:49
created

CallbackTask::getCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2017 NanoSector
5
 *
6
 * You should have received a copy of the MIT license with the project.
7
 * See the LICENSE file for more information.
8
 */
9
10
namespace Yoshi2889\Tasks;
11
12
class CallbackTask implements TaskInterface
13
{
14
	/**
15
	 * @var callable
16
	 */
17
	protected $callback = null;
18
19
	/**
20
	 * @var int
21
	 */
22
	protected $expiryTime = 0;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $storedArguments = [];
28
29
	/**
30
	 * CallbackTask constructor.
31
	 *
32
	 * @param callable $callback
33
	 * @param int $time
34
	 * @param array $args
35
	 */
36
	public function __construct(callable $callback, int $time, array $args = [])
37
	{
38
		$this->callback = $callback;
39
		$this->setExpiryTime(time() + $time);
40
		$this->setStoredArguments($args);
41
	}
42
43
	public function run(): ?TaskInterface
44
	{
45
		$result = call_user_func_array($this->getCallback(), $this->getStoredArguments());
46
47
		if (!($result instanceof TaskInterface))
48
			return null;
49
50
		return $result;
51
	}
52
53
	public function cancel(): void
54
	{
55
		$this->callback = function () {};
56
		$this->setExpiryTime(time() - 1);
57
	}
58
59
	/**
60
	 * @return callable
61
	 */
62
	public function getCallback(): callable
63
	{
64
		return $this->callback;
65
	}
66
67
	/**
68
	 * @return int
69
	 */
70
	public function getExpiryTime(): int
71
	{
72
		return $this->expiryTime;
73
	}
74
75
	/**
76
	 * @param int $expiryTime
77
	 */
78
	public function setExpiryTime(int $expiryTime)
79
	{
80
		$this->expiryTime = $expiryTime;
81
	}
82
83
	/**
84
	 * @return array
85
	 */
86
	public function getStoredArguments(): array
87
	{
88
		return $this->storedArguments;
89
	}
90
91
	/**
92
	 * @param array $storedArguments
93
	 */
94
	public function setStoredArguments(array $storedArguments)
95
	{
96
		$this->storedArguments = $storedArguments;
97
	}
98
99
100
}