Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.02% |
40 / 43 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| TranslationUniqueIdSetter | |
93.02% |
40 / 43 |
|
85.71% |
6 / 7 |
13.06 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addQtiIdentifierSetter | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
2.86 | |||
| __invoke | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| getOriginalResource | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| getUniqueId | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getQtiIdentifierSetter | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 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) 2024-2025 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Translation\Service; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use InvalidArgumentException; |
| 28 | use oat\generis\model\data\Ontology; |
| 29 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 30 | use oat\tao\model\TaoOntology; |
| 31 | |
| 32 | class TranslationUniqueIdSetter |
| 33 | { |
| 34 | private FeatureFlagCheckerInterface $featureFlagChecker; |
| 35 | private Ontology $ontology; |
| 36 | |
| 37 | /** @var AbstractQtiIdentifierSetter[] */ |
| 38 | private array $qtiIdentifierSetters = []; |
| 39 | |
| 40 | /** @var core_kernel_classes_Property[] */ |
| 41 | private array $properties = []; |
| 42 | |
| 43 | public function __construct(FeatureFlagCheckerInterface $featureFlagChecker, Ontology $ontology) |
| 44 | { |
| 45 | $this->featureFlagChecker = $featureFlagChecker; |
| 46 | $this->ontology = $ontology; |
| 47 | } |
| 48 | |
| 49 | public function addQtiIdentifierSetter(AbstractQtiIdentifierSetter $qtiIdentifierSetter, string $resourceType): void |
| 50 | { |
| 51 | if (isset($this->qtiIdentifierSetters[$resourceType])) { |
| 52 | throw new InvalidArgumentException( |
| 53 | 'QTI Identifier setter already exists for resource type ' . $resourceType |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | $this->qtiIdentifierSetters[$resourceType] = $qtiIdentifierSetter; |
| 58 | } |
| 59 | |
| 60 | public function __invoke(core_kernel_classes_Resource $resource): void |
| 61 | { |
| 62 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | $originalResource = $this->getOriginalResource($resource); |
| 67 | $uniqueIdentifier = $this->getUniqueId($originalResource); |
| 68 | |
| 69 | $this->getQtiIdentifierSetter($resource)->set([ |
| 70 | AbstractQtiIdentifierSetter::OPTION_RESOURCE => $resource, |
| 71 | AbstractQtiIdentifierSetter::OPTION_IDENTIFIER => $uniqueIdentifier, |
| 72 | ]); |
| 73 | |
| 74 | $resource->editPropertyValues( |
| 75 | $this->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER), |
| 76 | $uniqueIdentifier |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | private function getOriginalResource(core_kernel_classes_Resource $resource): core_kernel_classes_Resource |
| 81 | { |
| 82 | $property = $this->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI); |
| 83 | $originalResourceUri = $resource->getOnePropertyValue($property); |
| 84 | |
| 85 | if (empty($originalResourceUri)) { |
| 86 | throw new InvalidArgumentException( |
| 87 | sprintf( |
| 88 | 'Resource %s is not a translation - original resource URI is empty', |
| 89 | $resource->getUri() |
| 90 | ) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | return $this->ontology->getResource($originalResourceUri); |
| 95 | } |
| 96 | |
| 97 | private function getUniqueId(core_kernel_classes_Resource $resource): string |
| 98 | { |
| 99 | $property = $this->getProperty(TaoOntology::PROPERTY_UNIQUE_IDENTIFIER); |
| 100 | $uniqueId = $resource->getOnePropertyValue($property); |
| 101 | |
| 102 | if (empty($uniqueId)) { |
| 103 | throw new InvalidArgumentException('Unique ID must exists for resource URI ' . $resource->getUri()); |
| 104 | } |
| 105 | |
| 106 | return $uniqueId->literal; |
| 107 | } |
| 108 | |
| 109 | private function getQtiIdentifierSetter(core_kernel_classes_Resource $resource): AbstractQtiIdentifierSetter |
| 110 | { |
| 111 | $resourceType = $resource->getRootId(); |
| 112 | |
| 113 | if (!isset($this->qtiIdentifierSetters[$resourceType])) { |
| 114 | throw new InvalidArgumentException( |
| 115 | 'QTI Identifier setter does not exist for resource type ' . $resourceType |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | return $this->qtiIdentifierSetters[$resourceType]; |
| 120 | } |
| 121 | |
| 122 | private function getProperty(string $uri): core_kernel_classes_Property |
| 123 | { |
| 124 | if (!isset($this->properties[$uri])) { |
| 125 | $this->properties[$uri] = $this->ontology->getProperty($uri); |
| 126 | } |
| 127 | |
| 128 | return $this->properties[$uri]; |
| 129 | } |
| 130 | } |