Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskFactory
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
0.00% covered (danger)
0.00%
0 / 1
 build
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\tao\model\taskQueue\Task;
23
24class TaskFactory
25{
26    /**
27     * @param array $basicData
28     *
29     * @return TaskInterface
30     * @throws InvalidTaskException
31     */
32    public static function build($basicData)
33    {
34        $className = $basicData[TaskInterface::JSON_TASK_CLASS_NAME_KEY];
35
36        if (class_exists($className) && is_subclass_of($className, TaskInterface::class)) {
37            $metaData = $basicData[TaskInterface::JSON_METADATA_KEY];
38
39            /** @var TaskInterface $task */
40            $task = new $className(
41                $metaData[TaskInterface::JSON_METADATA_ID_KEY],
42                $metaData[TaskInterface::JSON_METADATA_OWNER_KEY]
43            );
44            $task->setMetadata($metaData);
45            $task->setParameter($basicData[TaskInterface::JSON_PARAMETERS_KEY]);
46
47            if (isset($metaData[TaskInterface::JSON_METADATA_CREATED_AT_KEY])) {
48                $task->setCreatedAt(new \DateTime($metaData[TaskInterface::JSON_METADATA_CREATED_AT_KEY]));
49            }
50
51            return $task;
52        }
53
54        throw new InvalidTaskException();
55    }
56}