Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| RemoteListService | |
0.00% |
0 / 40 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| sync | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| createRemoteSourceContext | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| isListsDependencyEnabled | |
0.00% |
0 / 3 |
|
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) 2025 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoBackOffice\model\lists; |
| 24 | |
| 25 | use core_kernel_classes_Class; |
| 26 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 27 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 28 | use oat\tao\model\Lists\Business\Domain\RemoteSourceContext; |
| 29 | use oat\tao\model\Lists\Business\Domain\ValueCollection; |
| 30 | use oat\tao\model\Lists\Business\Service\RemoteSource; |
| 31 | use oat\tao\model\Lists\Business\Service\RemoteSourcedListOntology; |
| 32 | use oat\tao\model\Lists\Business\Service\ValueCollectionService; |
| 33 | use RuntimeException; |
| 34 | |
| 35 | class RemoteListService |
| 36 | { |
| 37 | private ValueCollectionService $valueCollectionService; |
| 38 | private RemoteSource $remoteSource; |
| 39 | private FeatureFlagChecker $featureFlagChecker; |
| 40 | |
| 41 | public function __construct( |
| 42 | ValueCollectionService $valueCollectionService, |
| 43 | RemoteSource $remoteSource, |
| 44 | FeatureFlagChecker $featureFlagChecker |
| 45 | ) { |
| 46 | $this->valueCollectionService = $valueCollectionService; |
| 47 | $this->remoteSource = $remoteSource; |
| 48 | $this->featureFlagChecker = $featureFlagChecker; |
| 49 | } |
| 50 | |
| 51 | public function sync(core_kernel_classes_Class $collectionClass, bool $isReloading = false): void |
| 52 | { |
| 53 | $context = $this->createRemoteSourceContext($collectionClass); |
| 54 | $collection = new ValueCollection( |
| 55 | $collectionClass->getUri(), |
| 56 | ...iterator_to_array($this->remoteSource->fetchByContext($context)) |
| 57 | ); |
| 58 | |
| 59 | $result = $this->valueCollectionService->persist($collection); |
| 60 | |
| 61 | if (!$result) { |
| 62 | throw new RuntimeException( |
| 63 | sprintf( |
| 64 | 'Attempt for %s of remote list was not successful', |
| 65 | $isReloading ? 'reloading' : 'loading' |
| 66 | ) |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private function createRemoteSourceContext(core_kernel_classes_Class $collectionClass): RemoteSourceContext |
| 72 | { |
| 73 | $sourceUrl = (string) $collectionClass->getOnePropertyValue( |
| 74 | $collectionClass->getProperty(RemoteSourcedListOntology::PROPERTY_SOURCE_URI) |
| 75 | ); |
| 76 | $uriPath = (string) $collectionClass->getOnePropertyValue( |
| 77 | $collectionClass->getProperty(RemoteSourcedListOntology::PROPERTY_ITEM_URI_PATH) |
| 78 | ); |
| 79 | $labelPath = (string) $collectionClass->getOnePropertyValue( |
| 80 | $collectionClass->getProperty(RemoteSourcedListOntology::PROPERTY_ITEM_LABEL_PATH) |
| 81 | ); |
| 82 | |
| 83 | $parameters = [ |
| 84 | RemoteSourceContext::PARAM_SOURCE_URL => $sourceUrl, |
| 85 | RemoteSourceContext::PARAM_URI_PATH => $uriPath, |
| 86 | RemoteSourceContext::PARAM_LABEL_PATH => $labelPath, |
| 87 | RemoteSourceContext::PARAM_PARSER => 'jsonpath', |
| 88 | ]; |
| 89 | |
| 90 | if ($this->isListsDependencyEnabled()) { |
| 91 | $dependencyUriPath = (string) $collectionClass->getOnePropertyValue( |
| 92 | $collectionClass->getProperty(RemoteSourcedListOntology::PROPERTY_DEPENDENCY_ITEM_URI_PATH) |
| 93 | ); |
| 94 | $parameters[RemoteSourceContext::PARAM_DEPENDENCY_URI_PATH] = $dependencyUriPath; |
| 95 | } |
| 96 | |
| 97 | return new RemoteSourceContext($parameters); |
| 98 | } |
| 99 | |
| 100 | private function isListsDependencyEnabled(): bool |
| 101 | { |
| 102 | return $this->featureFlagChecker->isEnabled( |
| 103 | FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED |
| 104 | ); |
| 105 | } |
| 106 | } |