1 | <?php |
||
29 | class TaskList |
||
30 | { |
||
31 | /** |
||
32 | * The config to read from. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $dataDir; |
||
37 | |||
38 | /** |
||
39 | * The task factory to use. |
||
40 | * |
||
41 | * @var TaskFactoryInterface |
||
42 | */ |
||
43 | private $factory; |
||
44 | |||
45 | /** |
||
46 | * Create a new instance. |
||
47 | * |
||
48 | * @param string $dataDir The directory to keep the database in. |
||
49 | * |
||
50 | * @param TaskFactoryInterface $factory The task factory to use. |
||
51 | */ |
||
52 | public function __construct($dataDir, TaskFactoryInterface $factory) |
||
53 | { |
||
54 | $this->dataDir = $dataDir; |
||
55 | $this->factory = $factory; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Add the task to the list. |
||
60 | * |
||
61 | * @param string $type The type name. |
||
62 | * |
||
63 | * @param JsonArray|null $metaData The (optional) meta data. |
||
64 | * |
||
65 | * @return string |
||
66 | * |
||
67 | * @throws \InvalidArgumentException When no task instance can be created from the meta data. |
||
68 | */ |
||
69 | public function queue($type, JsonArray $metaData = null) |
||
70 | { |
||
71 | $taskId = $this->generateId(); |
||
72 | |||
73 | if (null === $metaData) { |
||
74 | $metaData = new JsonArray(); |
||
75 | } |
||
76 | |||
77 | $metaData |
||
78 | ->set(Task::SETTING_ID, $taskId) |
||
79 | ->set(Task::SETTING_TYPE, $type) |
||
80 | ->set('status', Task::STATE_PENDING) |
||
81 | ->set(Task::SETTING_CREATED_AT, date('c')); |
||
82 | |||
83 | $taskFile = new JsonFile($this->taskIdToFileName($taskId), null); |
||
84 | $taskFile->setData($metaData->getData()); |
||
85 | $taskFile->save(); |
||
86 | |||
87 | if (!$this->createTaskFromMetaData($taskFile)) { |
||
88 | unlink($taskFile->getFilename()); |
||
89 | throw new \InvalidArgumentException('Could not create task of type "' . $metaData->get('type') . '"'); |
||
90 | } |
||
91 | |||
92 | $this->getConfig()->set($taskId, $metaData->getData()); |
||
93 | |||
94 | return $taskId; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Remove a task from the queue (or the first one if no id given). |
||
99 | * |
||
100 | * @param null|string $taskId The id of the task to dequeue, if null the first queued task will be returned. |
||
101 | * |
||
102 | * @return Task|null |
||
103 | */ |
||
104 | public function dequeue($taskId = null) |
||
120 | |||
121 | /** |
||
122 | * Retrieve the first task from the queue without removing it. |
||
123 | * |
||
124 | * @return Task|null |
||
125 | */ |
||
126 | public function getNext() |
||
135 | |||
136 | /** |
||
137 | * Remove a task from the list, including it's task file. |
||
138 | * |
||
139 | * @param string $taskId The task to remove. |
||
140 | * |
||
141 | * @return TaskList |
||
142 | */ |
||
143 | public function remove($taskId) |
||
154 | |||
155 | /** |
||
156 | * Retrieve the ids of all registered tasks. |
||
157 | * |
||
158 | * @return string[] |
||
159 | */ |
||
160 | public function getIds() |
||
164 | |||
165 | /** |
||
166 | * Retrieve a task. |
||
167 | * |
||
168 | * @param string $taskId The id of the task to retrieve. |
||
169 | * |
||
170 | * @return Task|null |
||
171 | */ |
||
172 | public function getTask($taskId) |
||
181 | |||
182 | /** |
||
183 | * Retrieve the correct filename for the given task id. |
||
184 | * |
||
185 | * @param string $taskId The task id to generate the file name for. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | private function taskIdToFileName($taskId) |
||
193 | |||
194 | /** |
||
195 | * Create a task instance from the given MetaData. |
||
196 | * |
||
197 | * @param JsonArray $config The configuration of the task. |
||
198 | * |
||
199 | * @return Task|null |
||
200 | */ |
||
201 | private function createTaskFromMetaData(JsonArray $config) |
||
202 | { |
||
203 | $typeName = $config->get(Task::SETTING_TYPE); |
||
204 | if ($this->factory->isTypeSupported($typeName)) { |
||
205 | return $this->factory->createInstance($typeName, $config); |
||
206 | } |
||
207 | |||
208 | return null; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Generate a task id. |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | private function generateId() |
||
220 | |||
221 | /** |
||
222 | * Get the config file instance. |
||
223 | * |
||
224 | * @return JsonFile |
||
225 | */ |
||
226 | private function getConfig() |
||
230 | } |
||
231 |