Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.89% |
23 / 36 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RemoteSourceJsonPathParser | |
63.89% |
23 / 36 |
|
33.33% |
1 / 3 |
20.96 | |
0.00% |
0 / 1 |
| iterate | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| iterateByContext | |
78.57% |
22 / 28 |
|
0.00% |
0 / 1 |
12.19 | |||
| getFeatureFlagChecker | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Lists\Business\Service; |
| 24 | |
| 25 | use RuntimeException; |
| 26 | use Flow\JSONPath\JSONPath; |
| 27 | use Flow\JSONPath\JSONPathException; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\tao\model\Context\ContextInterface; |
| 30 | use oat\tao\model\Lists\Business\Domain\Value; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagChecker; |
| 32 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 33 | use oat\tao\model\Lists\Business\Domain\RemoteSourceContext; |
| 34 | |
| 35 | class RemoteSourceJsonPathParser extends ConfigurableService implements RemoteSourceParserInterface |
| 36 | { |
| 37 | /** |
| 38 | * @deprecated use $this->iterateByContext() |
| 39 | */ |
| 40 | public function iterate(array $json, string $uriRule, string $labelRule): iterable |
| 41 | { |
| 42 | yield from $this->iterateByContext( |
| 43 | new RemoteSourceContext([ |
| 44 | RemoteSourceContext::PARAM_JSON => $json, |
| 45 | RemoteSourceContext::PARAM_URI_PATH => $uriRule, |
| 46 | RemoteSourceContext::PARAM_LABEL_PATH => $labelRule, |
| 47 | ]) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | public function iterateByContext(ContextInterface $context): iterable |
| 52 | { |
| 53 | $jsonPath = new JSONPath($context->getParameter(RemoteSourceContext::PARAM_JSON)); |
| 54 | |
| 55 | try { |
| 56 | $uris = $jsonPath->find($context->getParameter(RemoteSourceContext::PARAM_URI_PATH)); |
| 57 | $labels = $jsonPath->find($context->getParameter(RemoteSourceContext::PARAM_LABEL_PATH)); |
| 58 | } catch (JSONPathException $e) { |
| 59 | throw new RuntimeException($e->getMessage(), $e->getCode(), $e); |
| 60 | } |
| 61 | |
| 62 | $urisCount = $uris->count(); |
| 63 | |
| 64 | if ($urisCount === 0) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if ($urisCount !== $labels->count()) { |
| 69 | throw new RuntimeException('Count of URIs and labels should be equal'); |
| 70 | } |
| 71 | |
| 72 | $dependencyUri = null; |
| 73 | $isListsDependencyEnabled = $this->getFeatureFlagChecker()->isEnabled( |
| 74 | FeatureFlagCheckerInterface::FEATURE_FLAG_LISTS_DEPENDENCY_ENABLED |
| 75 | ); |
| 76 | |
| 77 | if ($isListsDependencyEnabled) { |
| 78 | try { |
| 79 | $dependencyUriRule = $context->getParameter(RemoteSourceContext::PARAM_DEPENDENCY_URI_PATH); |
| 80 | $dependencyUris = !empty($dependencyUriRule) |
| 81 | ? $jsonPath->find($dependencyUriRule) |
| 82 | : null; |
| 83 | } catch (JSONPathException $e) { |
| 84 | $dependencyUris = null; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | do { |
| 89 | if ($isListsDependencyEnabled && $dependencyUris !== null) { |
| 90 | $dependencyUri = $dependencyUris->current() ?: null; |
| 91 | $dependencyUris->next(); |
| 92 | } |
| 93 | |
| 94 | yield new Value(null, $uris->current(), $labels->current(), $dependencyUri); |
| 95 | |
| 96 | $uris->next(); |
| 97 | $labels->next(); |
| 98 | } while ($uris->valid() && $labels->valid()); |
| 99 | } |
| 100 | |
| 101 | private function getFeatureFlagChecker(): FeatureFlagCheckerInterface |
| 102 | { |
| 103 | return $this->getServiceLocator()->get(FeatureFlagChecker::class); |
| 104 | } |
| 105 | } |