Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Redirector | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| redirectTaskToInstance | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
| getTaskInstance | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getTaskLog | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResourceUrlBuilder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 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) 2018-2022 (original work) Open Assessment Technologies SA. |
| 19 | * |
| 20 | * @author Gyula Szucs <gyula@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoBackOffice\controller; |
| 26 | |
| 27 | use Throwable; |
| 28 | use tao_actions_CommonModule; |
| 29 | use InvalidArgumentException; |
| 30 | use core_kernel_classes_Resource; |
| 31 | use common_exception_MissingParameter; |
| 32 | use oat\generis\model\OntologyAwareTrait; |
| 33 | use oat\tao\model\http\HttpJsonResponseTrait; |
| 34 | use oat\tao\model\taskQueue\TaskLogInterface; |
| 35 | use oat\taoBackOffice\model\routing\ResourceUrlBuilder; |
| 36 | |
| 37 | class Redirector extends tao_actions_CommonModule |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | use HttpJsonResponseTrait; |
| 41 | |
| 42 | private const PARAMETER_TASK_ID = 'taskId'; |
| 43 | |
| 44 | /** |
| 45 | * Redirect to a resource generated in a task. |
| 46 | */ |
| 47 | public function redirectTaskToInstance(): void |
| 48 | { |
| 49 | try { |
| 50 | $this->setSuccessJsonResponse( |
| 51 | $this->getResourceUrlBuilder()->buildUrl( |
| 52 | $this->getTaskInstance() |
| 53 | ) |
| 54 | ); |
| 55 | } catch (InvalidArgumentException $exception) { |
| 56 | $this->logError($exception->getMessage()); |
| 57 | $this->setErrorJsonResponse( |
| 58 | __('The requested resource does not exist or has been deleted'), |
| 59 | 202, |
| 60 | [], |
| 61 | 202 |
| 62 | ); |
| 63 | } catch (Throwable $exception) { |
| 64 | $this->logError($exception->getMessage()); |
| 65 | $this->setErrorJsonResponse( |
| 66 | __('There was a problem redirecting to the requested resource'), |
| 67 | 500, |
| 68 | [], |
| 69 | 500 |
| 70 | ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private function getTaskInstance(): core_kernel_classes_Resource |
| 75 | { |
| 76 | $queryParams = $this->getPsrRequest()->getQueryParams(); |
| 77 | |
| 78 | if (!isset($queryParams[self::PARAMETER_TASK_ID])) { |
| 79 | throw new common_exception_MissingParameter(self::PARAMETER_TASK_ID, $this->getRequestURI()); |
| 80 | } |
| 81 | |
| 82 | $entity = $this->getTaskLog()->getByIdAndUser( |
| 83 | $queryParams[self::PARAMETER_TASK_ID], |
| 84 | $this->getSession()->getUserUri(), |
| 85 | true // in Sync mode, task is archived straightaway |
| 86 | ); |
| 87 | |
| 88 | return $this->getResource($entity->getResourceUriFromReport()); |
| 89 | } |
| 90 | |
| 91 | private function getTaskLog(): TaskLogInterface |
| 92 | { |
| 93 | return $this->getPsrContainer()->get(TaskLogInterface::SERVICE_ID); |
| 94 | } |
| 95 | |
| 96 | private function getResourceUrlBuilder(): ResourceUrlBuilder |
| 97 | { |
| 98 | return $this->getPsrContainer()->get(ResourceUrlBuilder::SERVICE_ID); |
| 99 | } |
| 100 | } |