Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ListItemsService | |
100.00% |
29 / 29 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| validateCacheConfig | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getItem | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Ricardo Quintanilha <ricardo.quintanilha@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoQtiTest\model\Service; |
| 26 | |
| 27 | use common_exception_Unauthorized as UnauthorizedException; |
| 28 | use oat\taoQtiTest\models\runner\QtiRunnerServiceContext; |
| 29 | use oat\taoQtiTest\models\runner\RunnerService; |
| 30 | use Psr\Log\LoggerInterface; |
| 31 | use stdClass; |
| 32 | |
| 33 | class ListItemsService |
| 34 | { |
| 35 | /** @var RunnerService */ |
| 36 | private $runnerService; |
| 37 | |
| 38 | /** @var LoggerInterface */ |
| 39 | private $logger; |
| 40 | |
| 41 | public function __construct(RunnerService $runnerService, LoggerInterface $logger) |
| 42 | { |
| 43 | $this->runnerService = $runnerService; |
| 44 | $this->logger = $logger; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @throws UnauthorizedException |
| 49 | */ |
| 50 | public function __invoke(ListItemsQuery $query): ActionResponse |
| 51 | { |
| 52 | $this->validateCacheConfig(); |
| 53 | |
| 54 | if (empty($query->getItemIdentifiers())) { |
| 55 | return ActionResponse::success(); |
| 56 | } |
| 57 | |
| 58 | $items = []; |
| 59 | foreach ($query->getItemIdentifiers() as $itemIdentifier) { |
| 60 | $items[] = $this->getItem($itemIdentifier, $query->getServiceContext()); |
| 61 | } |
| 62 | |
| 63 | return ActionResponse::success() |
| 64 | ->withAttribute('items', $items); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @throws UnauthorizedException |
| 69 | */ |
| 70 | private function validateCacheConfig(): void |
| 71 | { |
| 72 | if ($this->runnerService->getTestConfig()->getConfigValue('itemCaching.enabled')) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $this->logger->warning('Attempt to disclose the next items without the configuration'); |
| 77 | |
| 78 | throw new UnauthorizedException(); |
| 79 | } |
| 80 | |
| 81 | private function getItem(string $itemIdentifier, QtiRunnerServiceContext $serviceContext): array |
| 82 | { |
| 83 | $itemRef = $this->runnerService->getItemHref($serviceContext, $itemIdentifier); |
| 84 | $itemData = $this->runnerService->getItemData($serviceContext, $itemRef); |
| 85 | $baseUrl = $this->runnerService->getItemPublicUrl($serviceContext, $itemRef); |
| 86 | $portableElements = $this->runnerService->getItemPortableElements($serviceContext, $itemRef); |
| 87 | $itemState = $this->runnerService->getItemState($serviceContext, $itemIdentifier); |
| 88 | |
| 89 | if ( |
| 90 | $itemState === null |
| 91 | || count($itemState) < 1 |
| 92 | ) { |
| 93 | $itemState = new stdClass(); |
| 94 | } |
| 95 | |
| 96 | return [ |
| 97 | 'baseUrl' => $baseUrl, |
| 98 | 'itemData' => $itemData, |
| 99 | 'itemState' => $itemState, |
| 100 | 'itemIdentifier' => $itemIdentifier, |
| 101 | 'portableElements' => $portableElements |
| 102 | ]; |
| 103 | } |
| 104 | } |