Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryExecutionState | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
156 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| put | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getExecutionService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStateService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| extractState | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoDelivery\controller; |
| 26 | |
| 27 | use common_exception_MissingParameter; |
| 28 | use common_exception_NotFound as NotFoundException; |
| 29 | use common_exception_NotImplemented as NotImplementedException; |
| 30 | use common_exception_RestApi as ApiException; |
| 31 | use oat\taoDelivery\model\execution\ServiceProxy; |
| 32 | use oat\taoDelivery\model\execution\StateServiceInterface; |
| 33 | use tao_actions_RestController as RestController; |
| 34 | use oat\taoDelivery\model\execution\DeliveryExecutionService; |
| 35 | |
| 36 | /** Kindly use `funcAcl` in order to assign the roles, having access to the controller */ |
| 37 | class DeliveryExecutionState extends RestController |
| 38 | { |
| 39 | /** |
| 40 | * @throws NotFoundException |
| 41 | * |
| 42 | * @throws NotImplementedException |
| 43 | * @throws NotFoundException |
| 44 | */ |
| 45 | public function index(): void |
| 46 | { |
| 47 | $queryParams = $this->getPsrRequest()->getQueryParams(); |
| 48 | |
| 49 | switch ($this->getPsrRequest()->getMethod()) { |
| 50 | case 'PUT': |
| 51 | $this->put($queryParams['uri'] ?? ''); |
| 52 | break; |
| 53 | default: |
| 54 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 55 | $this->returnFailure(new ApiException('Not implemented')); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private function put(string $uri): void |
| 60 | { |
| 61 | if (empty($uri)) { |
| 62 | $this->returnFailure(new common_exception_MissingParameter('uri')); |
| 63 | } |
| 64 | |
| 65 | $state = $this->extractState(); |
| 66 | |
| 67 | $deliveryExecution = $this->getExecutionService()->getDeliveryExecution($uri); |
| 68 | |
| 69 | try { |
| 70 | if ($deliveryExecution->getState()->getUri() !== $state) { |
| 71 | $deliveryExecution->setState($state); |
| 72 | } |
| 73 | } catch (NotFoundException $exception) { |
| 74 | $this->returnFailure(new NotFoundException('Delivery execution not found', 404, $exception)); |
| 75 | } |
| 76 | |
| 77 | $this->returnSuccess([], false); |
| 78 | } |
| 79 | |
| 80 | protected function getExecutionService(): DeliveryExecutionService |
| 81 | { |
| 82 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 83 | return $this->getServiceLocator()->get(ServiceProxy::SERVICE_ID); |
| 84 | } |
| 85 | |
| 86 | protected function getStateService(): StateServiceInterface |
| 87 | { |
| 88 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 89 | return $this->getServiceLocator()->get(StateServiceInterface::SERVICE_ID); |
| 90 | } |
| 91 | |
| 92 | private function extractState(): string |
| 93 | { |
| 94 | $data = json_decode((string)$this->getPsrRequest()->getBody(), true); |
| 95 | |
| 96 | if (empty($data['value']) || !in_array($data['value'], $this->getStateService()->getDeliveriesStates(), true)) { |
| 97 | /** @noinspection PhpUnhandledExceptionInspection */ |
| 98 | $this->returnFailure(new common_exception_MissingParameter('value')); |
| 99 | } |
| 100 | |
| 101 | return $data['value']; |
| 102 | } |
| 103 | } |