Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
20 / 22 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SynchronisationService | |
90.91% |
20 / 22 |
|
50.00% |
2 / 4 |
8.05 | |
0.00% |
0 / 1 |
| process | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 | |||
| getAvailableActions | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| setAvailableActions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateSynchronisationData | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 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 ; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiTest\models\runner\synchronisation; |
| 24 | |
| 25 | use common_exception_InconsistentData; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\taoQtiTest\models\runner\QtiRunnerServiceContext; |
| 28 | use oat\taoQtiTest\models\runner\synchronisation\synchronisationService\ResponseGenerator; |
| 29 | |
| 30 | class SynchronisationService extends ConfigurableService |
| 31 | { |
| 32 | public const SERVICE_ID = 'taoQtiTest/synchronisationService'; |
| 33 | public const ACTIONS_OPTION = 'actions'; |
| 34 | |
| 35 | /** |
| 36 | * Wrap the process to appropriate action and aggregate results |
| 37 | * |
| 38 | * @param $data |
| 39 | * @param $serviceContext QtiRunnerServiceContext |
| 40 | * @return array |
| 41 | * @throws common_exception_InconsistentData |
| 42 | */ |
| 43 | public function process($data, QtiRunnerServiceContext $serviceContext): array |
| 44 | { |
| 45 | $this->validateSynchronisationData($data); |
| 46 | |
| 47 | /** @var ResponseGenerator $responseGenerator */ |
| 48 | $responseGenerator = $this->getServiceLocator()->get(ResponseGenerator::class); |
| 49 | |
| 50 | // extract the actions and build usable instances |
| 51 | $actions = $responseGenerator->prepareActions($data, $this->getAvailableActions()); |
| 52 | |
| 53 | $timeNow = microtime(true); |
| 54 | $lastActionTimestamp = $responseGenerator->getLastActionTimestamp($actions, $serviceContext, $timeNow); |
| 55 | |
| 56 | $response = []; |
| 57 | foreach ($actions as $action) { |
| 58 | if ($action instanceof TestRunnerAction) { |
| 59 | $response[] = $responseGenerator->getActionResponse( |
| 60 | $action, |
| 61 | $timeNow, |
| 62 | $lastActionTimestamp, |
| 63 | $serviceContext |
| 64 | ); |
| 65 | } else { |
| 66 | $response[] = $action; // if error happened |
| 67 | } |
| 68 | // no need to break on the first error as all actions expected to be with a response by the fe part |
| 69 | } |
| 70 | |
| 71 | return $response; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get available actions from config |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | public function getAvailableActions(): array |
| 80 | { |
| 81 | return is_array($this->getOption(self::ACTIONS_OPTION)) |
| 82 | ? $this->getOption(self::ACTIONS_OPTION) |
| 83 | : []; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set available actions to config |
| 88 | * |
| 89 | * @param array $actions |
| 90 | */ |
| 91 | public function setAvailableActions(array $actions = []): void |
| 92 | { |
| 93 | $this->setOption(self::ACTIONS_OPTION, $actions); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param $data |
| 98 | * @throws common_exception_InconsistentData |
| 99 | */ |
| 100 | protected function validateSynchronisationData($data): void |
| 101 | { |
| 102 | if (empty($data)) { |
| 103 | throw new common_exception_InconsistentData('No action to check. Processing action requires data.'); |
| 104 | } |
| 105 | } |
| 106 | } |