@@ -12,19 +12,19 @@ |
||
12 | 12 | |
13 | 13 | interface TaskInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @return int |
|
17 | - */ |
|
18 | - public function getExpiryTime(): int; |
|
15 | + /** |
|
16 | + * @return int |
|
17 | + */ |
|
18 | + public function getExpiryTime(): int; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Pass back a new instance of TaskInterface to run a new task. |
|
22 | - * @return null|TaskInterface |
|
23 | - */ |
|
24 | - public function run(): ?TaskInterface; |
|
20 | + /** |
|
21 | + * Pass back a new instance of TaskInterface to run a new task. |
|
22 | + * @return null|TaskInterface |
|
23 | + */ |
|
24 | + public function run(): ?TaskInterface; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return void |
|
28 | - */ |
|
29 | - public function cancel(): void; |
|
26 | + /** |
|
27 | + * @return void |
|
28 | + */ |
|
29 | + public function cancel(): void; |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -40,8 +40,9 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function add(TaskInterface $task): bool |
42 | 42 | { |
43 | - if ($this->exists($task)) |
|
44 | - return false; |
|
43 | + if ($this->exists($task)) { |
|
44 | + return false; |
|
45 | + } |
|
45 | 46 | |
46 | 47 | $this->tasks[] = $task; |
47 | 48 | |
@@ -55,8 +56,9 @@ discard block |
||
55 | 56 | */ |
56 | 57 | public function remove(TaskInterface $task): bool |
57 | 58 | { |
58 | - if (!$this->exists($task)) |
|
59 | - return false; |
|
59 | + if (!$this->exists($task)) { |
|
60 | + return false; |
|
61 | + } |
|
60 | 62 | |
61 | 63 | unset($this->tasks[array_search($task, $this->tasks)]); |
62 | 64 | |
@@ -77,16 +79,18 @@ discard block |
||
77 | 79 | { |
78 | 80 | foreach ($this->tasks as $task) |
79 | 81 | { |
80 | - if (time() < $task->getExpiryTime()) |
|
81 | - continue; |
|
82 | + if (time() < $task->getExpiryTime()) { |
|
83 | + continue; |
|
84 | + } |
|
82 | 85 | |
83 | 86 | $result = $task->run(); |
84 | 87 | |
85 | 88 | // It is removed first. |
86 | 89 | $this->remove($task); |
87 | 90 | |
88 | - if ($result instanceof TaskInterface) |
|
89 | - $this->add($result); |
|
91 | + if ($result instanceof TaskInterface) { |
|
92 | + $this->add($result); |
|
93 | + } |
|
90 | 94 | } |
91 | 95 | } |
92 | 96 | } |
93 | 97 | \ No newline at end of file |
@@ -13,55 +13,55 @@ discard block |
||
13 | 13 | |
14 | 14 | class TaskController |
15 | 15 | { |
16 | - /** |
|
17 | - * @var int |
|
18 | - */ |
|
19 | - protected $loopInterval = 1; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var TaskInterface[] |
|
23 | - */ |
|
24 | - protected $tasks = []; |
|
25 | - |
|
26 | - /** |
|
27 | - * TaskController constructor. |
|
28 | - * |
|
29 | - * @param LoopInterface $loop |
|
30 | - */ |
|
31 | - public function __construct(LoopInterface $loop) |
|
32 | - { |
|
33 | - $loop->addPeriodicTimer($this->loopInterval, [$this, 'runTasks']); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @param TaskInterface $task |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public function add(TaskInterface $task): bool |
|
42 | - { |
|
43 | - if ($this->exists($task)) |
|
44 | - return false; |
|
45 | - |
|
46 | - $this->tasks[] = $task; |
|
47 | - |
|
48 | - return true; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param TaskInterface $task |
|
53 | - * |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - public function remove(TaskInterface $task): bool |
|
57 | - { |
|
58 | - if (!$this->exists($task)) |
|
59 | - return false; |
|
60 | - |
|
61 | - unset($this->tasks[array_search($task, $this->tasks)]); |
|
62 | - |
|
63 | - return true; |
|
64 | - } |
|
16 | + /** |
|
17 | + * @var int |
|
18 | + */ |
|
19 | + protected $loopInterval = 1; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var TaskInterface[] |
|
23 | + */ |
|
24 | + protected $tasks = []; |
|
25 | + |
|
26 | + /** |
|
27 | + * TaskController constructor. |
|
28 | + * |
|
29 | + * @param LoopInterface $loop |
|
30 | + */ |
|
31 | + public function __construct(LoopInterface $loop) |
|
32 | + { |
|
33 | + $loop->addPeriodicTimer($this->loopInterval, [$this, 'runTasks']); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @param TaskInterface $task |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public function add(TaskInterface $task): bool |
|
42 | + { |
|
43 | + if ($this->exists($task)) |
|
44 | + return false; |
|
45 | + |
|
46 | + $this->tasks[] = $task; |
|
47 | + |
|
48 | + return true; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param TaskInterface $task |
|
53 | + * |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + public function remove(TaskInterface $task): bool |
|
57 | + { |
|
58 | + if (!$this->exists($task)) |
|
59 | + return false; |
|
60 | + |
|
61 | + unset($this->tasks[array_search($task, $this->tasks)]); |
|
62 | + |
|
63 | + return true; |
|
64 | + } |
|
65 | 65 | |
66 | 66 | public function removeAll() |
67 | 67 | { |
@@ -70,32 +70,32 @@ discard block |
||
70 | 70 | $task->cancel(); |
71 | 71 | $this->remove($task); |
72 | 72 | } |
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param TaskInterface $task |
|
77 | - * |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function exists(TaskInterface $task): bool |
|
81 | - { |
|
82 | - return in_array($task, $this->tasks); |
|
83 | - } |
|
84 | - |
|
85 | - public function runTasks() |
|
86 | - { |
|
87 | - foreach ($this->tasks as $task) |
|
88 | - { |
|
89 | - if (time() < $task->getExpiryTime()) |
|
90 | - continue; |
|
91 | - |
|
92 | - $result = $task->run(); |
|
93 | - |
|
94 | - // It is removed first. |
|
95 | - $this->remove($task); |
|
96 | - |
|
97 | - if ($result instanceof TaskInterface) |
|
98 | - $this->add($result); |
|
99 | - } |
|
100 | - } |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param TaskInterface $task |
|
77 | + * |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function exists(TaskInterface $task): bool |
|
81 | + { |
|
82 | + return in_array($task, $this->tasks); |
|
83 | + } |
|
84 | + |
|
85 | + public function runTasks() |
|
86 | + { |
|
87 | + foreach ($this->tasks as $task) |
|
88 | + { |
|
89 | + if (time() < $task->getExpiryTime()) |
|
90 | + continue; |
|
91 | + |
|
92 | + $result = $task->run(); |
|
93 | + |
|
94 | + // It is removed first. |
|
95 | + $this->remove($task); |
|
96 | + |
|
97 | + if ($result instanceof TaskInterface) |
|
98 | + $this->add($result); |
|
99 | + } |
|
100 | + } |
|
101 | 101 | } |
102 | 102 | \ No newline at end of file |
@@ -41,7 +41,7 @@ |
||
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
44 | - * @return null|TaskInterface |
|
44 | + * @return RepeatableTask|null |
|
45 | 45 | */ |
46 | 46 | public function run(): ?TaskInterface |
47 | 47 | { |
@@ -12,79 +12,79 @@ |
||
12 | 12 | |
13 | 13 | class RepeatableTask implements TaskInterface |
14 | 14 | { |
15 | - /** |
|
16 | - * @var int |
|
17 | - */ |
|
18 | - protected $repeatInterval = 0; |
|
15 | + /** |
|
16 | + * @var int |
|
17 | + */ |
|
18 | + protected $repeatInterval = 0; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var int |
|
22 | - */ |
|
23 | - protected $expiryTime = 0; |
|
20 | + /** |
|
21 | + * @var int |
|
22 | + */ |
|
23 | + protected $expiryTime = 0; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var TaskInterface |
|
27 | - */ |
|
28 | - protected $childTask; |
|
25 | + /** |
|
26 | + * @var TaskInterface |
|
27 | + */ |
|
28 | + protected $childTask; |
|
29 | 29 | |
30 | - /** |
|
31 | - * RepeatableTask constructor. |
|
32 | - * |
|
33 | - * @param TaskInterface $childTask |
|
34 | - * @param int $repeatInterval |
|
35 | - */ |
|
36 | - public function __construct(TaskInterface $childTask, int $repeatInterval) |
|
37 | - { |
|
38 | - $this->expiryTime = time() + $repeatInterval; |
|
39 | - $this->repeatInterval = $repeatInterval; |
|
40 | - $this->childTask = $childTask; |
|
41 | - } |
|
30 | + /** |
|
31 | + * RepeatableTask constructor. |
|
32 | + * |
|
33 | + * @param TaskInterface $childTask |
|
34 | + * @param int $repeatInterval |
|
35 | + */ |
|
36 | + public function __construct(TaskInterface $childTask, int $repeatInterval) |
|
37 | + { |
|
38 | + $this->expiryTime = time() + $repeatInterval; |
|
39 | + $this->repeatInterval = $repeatInterval; |
|
40 | + $this->childTask = $childTask; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * @return null|TaskInterface |
|
45 | - */ |
|
46 | - public function run(): ?TaskInterface |
|
47 | - { |
|
48 | - if (time() >= $this->childTask->getExpiryTime()) |
|
49 | - { |
|
50 | - $result = $this->childTask->run(); |
|
43 | + /** |
|
44 | + * @return null|TaskInterface |
|
45 | + */ |
|
46 | + public function run(): ?TaskInterface |
|
47 | + { |
|
48 | + if (time() >= $this->childTask->getExpiryTime()) |
|
49 | + { |
|
50 | + $result = $this->childTask->run(); |
|
51 | 51 | |
52 | - if ($result instanceof TaskInterface) |
|
53 | - $this->childTask = $result; |
|
54 | - } |
|
52 | + if ($result instanceof TaskInterface) |
|
53 | + $this->childTask = $result; |
|
54 | + } |
|
55 | 55 | |
56 | - $this->expiryTime = $this->getExpiryTime() + $this->getRepeatInterval(); |
|
57 | - return $this->expiryTime > 0 ? $this : null; |
|
58 | - } |
|
56 | + $this->expiryTime = $this->getExpiryTime() + $this->getRepeatInterval(); |
|
57 | + return $this->expiryTime > 0 ? $this : null; |
|
58 | + } |
|
59 | 59 | |
60 | - public function cancel(): void |
|
61 | - { |
|
62 | - $this->childTask->cancel(); |
|
63 | - $this->repeatInterval = 0; |
|
64 | - $this->expiryTime = 0; |
|
65 | - } |
|
60 | + public function cancel(): void |
|
61 | + { |
|
62 | + $this->childTask->cancel(); |
|
63 | + $this->repeatInterval = 0; |
|
64 | + $this->expiryTime = 0; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @return int |
|
69 | - */ |
|
70 | - public function getRepeatInterval(): int |
|
71 | - { |
|
72 | - return $this->repeatInterval; |
|
73 | - } |
|
67 | + /** |
|
68 | + * @return int |
|
69 | + */ |
|
70 | + public function getRepeatInterval(): int |
|
71 | + { |
|
72 | + return $this->repeatInterval; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @param int $repeatInterval |
|
77 | - */ |
|
78 | - public function setRepeatInterval(int $repeatInterval) |
|
79 | - { |
|
80 | - $this->repeatInterval = $repeatInterval; |
|
81 | - } |
|
75 | + /** |
|
76 | + * @param int $repeatInterval |
|
77 | + */ |
|
78 | + public function setRepeatInterval(int $repeatInterval) |
|
79 | + { |
|
80 | + $this->repeatInterval = $repeatInterval; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @return int |
|
85 | - */ |
|
86 | - public function getExpiryTime(): int |
|
87 | - { |
|
88 | - return $this->expiryTime; |
|
89 | - } |
|
83 | + /** |
|
84 | + * @return int |
|
85 | + */ |
|
86 | + public function getExpiryTime(): int |
|
87 | + { |
|
88 | + return $this->expiryTime; |
|
89 | + } |
|
90 | 90 | } |
91 | 91 | \ No newline at end of file |
@@ -45,12 +45,12 @@ |
||
45 | 45 | */ |
46 | 46 | public function run(): ?TaskInterface |
47 | 47 | { |
48 | - if (time() >= $this->childTask->getExpiryTime()) |
|
49 | - { |
|
48 | + if (time() >= $this->childTask->getExpiryTime()) { |
|
50 | 49 | $result = $this->childTask->run(); |
51 | 50 | |
52 | - if ($result instanceof TaskInterface) |
|
53 | - $this->childTask = $result; |
|
51 | + if ($result instanceof TaskInterface) { |
|
52 | + $this->childTask = $result; |
|
53 | + } |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | $this->expiryTime = $this->getExpiryTime() + $this->getRepeatInterval(); |
@@ -11,90 +11,90 @@ |
||
11 | 11 | |
12 | 12 | class CallbackTask implements TaskInterface |
13 | 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(0); |
|
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 | - } |
|
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(0); |
|
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 | 98 | |
99 | 99 | |
100 | 100 | } |
101 | 101 | \ No newline at end of file |
@@ -44,15 +44,18 @@ |
||
44 | 44 | { |
45 | 45 | $result = call_user_func_array($this->getCallback(), $this->getStoredArguments()); |
46 | 46 | |
47 | - if (!($result instanceof TaskInterface)) |
|
48 | - return null; |
|
47 | + if (!($result instanceof TaskInterface)) { |
|
48 | + return null; |
|
49 | + } |
|
49 | 50 | |
50 | 51 | return $result; |
51 | 52 | } |
52 | 53 | |
53 | 54 | public function cancel(): void |
54 | 55 | { |
55 | - $this->callback = function () {}; |
|
56 | + $this->callback = function () |
|
57 | + { |
|
58 | +}; |
|
56 | 59 | $this->setExpiryTime(0); |
57 | 60 | } |
58 | 61 |