1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Asylamba\Classes\Task; |
4
|
|
|
|
5
|
|
|
use Asylamba\Classes\DependencyInjection\Container; |
6
|
|
|
|
7
|
|
|
use Asylamba\Classes\Process\Process; |
8
|
|
|
|
9
|
|
|
class TaskManager |
10
|
|
|
{ |
11
|
|
|
/** @var Container **/ |
12
|
|
|
protected $container; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param Container $container |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Container $container) |
18
|
|
|
{ |
19
|
|
|
$this->container = $container; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $data |
24
|
|
|
* @return Task |
25
|
|
|
*/ |
26
|
|
|
public function createTaskFromData($data) |
27
|
|
|
{ |
28
|
|
|
switch ($data['type']) { |
29
|
|
|
case Task::TYPE_TECHNICAL: |
30
|
|
|
$task = $this->createTechnicalTask($data['manager'], $data['method'], $data['id']); |
31
|
|
|
break; |
32
|
|
|
case Task::TYPE_REALTIME: |
33
|
|
|
$task = $this->createRealTimeTask($data['manager'], $data['method'], $data['object_id'], $data['date'], $data['id'], ($data['context'] ?? null)); |
34
|
|
|
break; |
35
|
|
|
case Task::TYPE_CYCLIC: |
|
|
|
|
36
|
|
|
$task = $this->createCyclicTask($data['manager'], $data['method'], $data['id']); |
37
|
|
|
break; |
38
|
|
|
} |
39
|
|
|
if (isset($data['estimated_time'])) { |
40
|
|
|
$task->setEstimatedTime($data['estimated_time']); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
return $task; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $manager |
47
|
|
|
* @param string $method |
48
|
|
|
* @param int $objectId |
49
|
|
|
* @param string $date |
50
|
|
|
* @param int $id |
51
|
|
|
* @param array $context |
52
|
|
|
* @return Task |
53
|
|
|
*/ |
54
|
|
|
public function createRealTimeTask($manager, $method, $objectId, $date, $id = null, $context = null) |
55
|
|
|
{ |
56
|
|
|
return |
57
|
|
|
(new RealTimeTask()) |
58
|
|
|
->setId((($id !== null) ? $id : $this->generateId())) |
59
|
|
|
->setManager($manager) |
60
|
|
|
->setMethod($method) |
61
|
|
|
->setObjectId($objectId) |
62
|
|
|
->setDate($date) |
63
|
|
|
->setContext($context) |
64
|
|
|
; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $manager |
69
|
|
|
* @param string $method |
70
|
|
|
* @return Task |
71
|
|
|
*/ |
72
|
|
|
public function createCyclicTask($manager, $method, $id = null) |
73
|
|
|
{ |
74
|
|
|
return |
75
|
|
|
(new CyclicTask()) |
76
|
|
|
->setId((($id !== null) ? $id : $this->generateId())) |
77
|
|
|
->setManager($manager) |
78
|
|
|
->setMethod($method) |
79
|
|
|
; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $manager |
84
|
|
|
* @param string $method |
85
|
|
|
* @return Task |
86
|
|
|
*/ |
87
|
|
|
public function createTechnicalTask($manager, $method, $id = null) |
88
|
|
|
{ |
89
|
|
|
return |
90
|
|
|
(new TechnicalTask()) |
91
|
|
|
->setId((($id !== null) ? $id : $this->generateId())) |
92
|
|
|
->setManager($manager) |
93
|
|
|
->setMethod($method) |
94
|
|
|
; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \Asylamba\Classes\Task\Task $task |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
public function perform(Task $task) |
102
|
|
|
{ |
103
|
|
|
// Get the manager from the container and then execute the given method with its arguments |
104
|
|
|
call_user_func_array( |
105
|
|
|
[$this->container->get($task->getManager()), $task->getMethod()], |
106
|
|
|
($task instanceof RealTimeTask) ? [$task->getObjectId()] : [] |
107
|
|
|
); |
108
|
|
|
return ['success' => true, 'task' => $task]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Process $process |
113
|
|
|
* @param array $data |
114
|
|
|
*/ |
115
|
|
|
public function validateTask(Process $process, $data) |
116
|
|
|
{ |
117
|
|
|
$task = $this->createTaskFromData($data['task']); |
118
|
|
|
|
119
|
|
|
if (!isset($data['time'])) { |
120
|
|
|
$data['time'] = 0.0; |
121
|
|
|
} |
122
|
|
|
$task->setTime((float) $data['time']); |
123
|
|
|
|
124
|
|
|
$process->removeTask($task); |
125
|
|
|
if ($task instanceof RealTimeTask && $task->getContext() !== null) { |
126
|
|
|
$process->removeContext($task->getContext()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($data['success'] === true) { |
130
|
|
|
$this->container->get('load_balancer')->storeStats($task); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function generateId() |
135
|
|
|
{ |
136
|
|
|
return uniqid('process_'); |
137
|
|
|
} |
138
|
|
|
} |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.