Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.68% |
14 / 19 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| TaskSerializerService | |
73.68% |
14 / 19 |
|
60.00% |
3 / 5 |
14.62 | |
0.00% |
0 / 1 |
| deserialize | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| assertValidJson | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
4.25 | |||
| handleCallbackTask | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
4.68 | |||
| getActionResolver | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\taskQueue\Task; |
| 23 | |
| 24 | use oat\oatbox\action\ActionService; |
| 25 | use oat\oatbox\action\ResolutionException; |
| 26 | use oat\oatbox\log\LoggerAwareTrait; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 29 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 30 | |
| 31 | class TaskSerializerService extends ConfigurableService |
| 32 | { |
| 33 | use LoggerAwareTrait; |
| 34 | use ServiceLocatorAwareTrait; |
| 35 | |
| 36 | public const SERVICE_ID = 'tao/TaskSerializer'; |
| 37 | |
| 38 | /** |
| 39 | * @param string $taskJSON |
| 40 | * @param array $logContext |
| 41 | * @return null|TaskInterface |
| 42 | * @throws \Exception |
| 43 | */ |
| 44 | public function deserialize($taskJSON, array $logContext = []) |
| 45 | { |
| 46 | $basicData = json_decode($taskJSON, true); |
| 47 | $this->assertValidJson($basicData); |
| 48 | |
| 49 | $task = TaskFactory::build($basicData); |
| 50 | |
| 51 | if ($task instanceof CallbackTaskInterface && is_string($task->getCallable())) { |
| 52 | $this->handleCallbackTask($task, $logContext); |
| 53 | } |
| 54 | |
| 55 | return $task; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param TaskInterface $task |
| 60 | * @return false|string |
| 61 | */ |
| 62 | public function serialize(TaskInterface $task) |
| 63 | { |
| 64 | return json_encode($task); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param $basicData |
| 69 | * @throws \Exception |
| 70 | */ |
| 71 | protected function assertValidJson($basicData) |
| 72 | { |
| 73 | if ( |
| 74 | ($basicData !== null |
| 75 | && json_last_error() === JSON_ERROR_NONE |
| 76 | && isset($basicData[TaskInterface::JSON_TASK_CLASS_NAME_KEY])) === false |
| 77 | ) { |
| 78 | throw new \Exception(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param CallbackTaskInterface $task |
| 84 | * @param array $logContext |
| 85 | * @throws \Exception |
| 86 | */ |
| 87 | protected function handleCallbackTask(CallbackTaskInterface $task, array $logContext) |
| 88 | { |
| 89 | try { |
| 90 | $callable = $this->getActionResolver()->resolve($task->getCallable()); |
| 91 | |
| 92 | if ($callable instanceof ServiceLocatorAwareInterface) { |
| 93 | $callable->setServiceLocator($this->getServiceLocator()); |
| 94 | } |
| 95 | |
| 96 | $task->setCallable($callable); |
| 97 | } catch (ResolutionException $e) { |
| 98 | $this->logError('Callable/Action class ' . $task->getCallable() . ' does not exist', $logContext); |
| 99 | |
| 100 | throw new \Exception(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @return ActionService|ConfigurableService|object |
| 106 | */ |
| 107 | protected function getActionResolver() |
| 108 | { |
| 109 | return $this->getServiceLocator()->get(ActionService::SERVICE_ID); |
| 110 | } |
| 111 | } |