Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
38 / 38 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
TestCreatedEventListener | |
100.00% |
38 / 38 |
|
100.00% |
7 / 7 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
populateTranslationProperties | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
setLanguage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setTranslationType | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setTranslationStatus | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
isPropertySet | |
100.00% |
10 / 10 |
|
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 (original work) Open Assessment Technologies SA. |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoTests\models\Translation\Listener; |
24 | |
25 | use core_kernel_classes_Property; |
26 | use core_kernel_classes_Resource; |
27 | use oat\generis\model\data\Ontology; |
28 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
29 | use oat\tao\model\TaoOntology; |
30 | use oat\tao\model\Translation\Service\ResourceLanguageRetriever; |
31 | use oat\taoTests\models\event\TestCreatedEvent; |
32 | use Psr\Log\LoggerInterface; |
33 | |
34 | class TestCreatedEventListener |
35 | { |
36 | private FeatureFlagCheckerInterface $featureFlagChecker; |
37 | private Ontology $ontology; |
38 | private ResourceLanguageRetriever $resourceLanguageRetriever; |
39 | private LoggerInterface $logger; |
40 | |
41 | public function __construct( |
42 | FeatureFlagCheckerInterface $featureFlagChecker, |
43 | Ontology $ontology, |
44 | ResourceLanguageRetriever $resourceLanguageRetriever, |
45 | LoggerInterface $logger |
46 | ) { |
47 | $this->featureFlagChecker = $featureFlagChecker; |
48 | $this->ontology = $ontology; |
49 | $this->resourceLanguageRetriever = $resourceLanguageRetriever; |
50 | $this->logger = $logger; |
51 | } |
52 | |
53 | public function populateTranslationProperties(TestCreatedEvent $event): void |
54 | { |
55 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
56 | return; |
57 | } |
58 | |
59 | $test = $this->ontology->getResource($event->getTestUri()); |
60 | |
61 | $this->setLanguage($test); |
62 | $this->setTranslationType($test); |
63 | $this->setTranslationStatus($test); |
64 | } |
65 | |
66 | private function setLanguage(core_kernel_classes_Resource $test): void |
67 | { |
68 | $this->setProperty( |
69 | $test, |
70 | TaoOntology::PROPERTY_LANGUAGE, |
71 | TaoOntology::LANGUAGE_PREFIX . $this->resourceLanguageRetriever->retrieve($test) |
72 | ); |
73 | } |
74 | |
75 | private function setTranslationType(core_kernel_classes_Resource $test): void |
76 | { |
77 | $this->setProperty( |
78 | $test, |
79 | TaoOntology::PROPERTY_TRANSLATION_TYPE, |
80 | TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL |
81 | ); |
82 | } |
83 | |
84 | private function setTranslationStatus(core_kernel_classes_Resource $test): void |
85 | { |
86 | $this->setProperty( |
87 | $test, |
88 | TaoOntology::PROPERTY_TRANSLATION_STATUS, |
89 | TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_NOT_READY |
90 | ); |
91 | } |
92 | |
93 | private function setProperty(core_kernel_classes_Resource $test, string $propertyUri, string $value): void |
94 | { |
95 | $property = $this->ontology->getProperty($propertyUri); |
96 | |
97 | if (!$this->isPropertySet($test, $property)) { |
98 | $test->editPropertyValues($property, $value); |
99 | } |
100 | } |
101 | |
102 | private function isPropertySet(core_kernel_classes_Resource $test, core_kernel_classes_Property $property): bool |
103 | { |
104 | if (empty($test->getOnePropertyValue($property))) { |
105 | return false; |
106 | } |
107 | |
108 | $this->logger->info( |
109 | sprintf( |
110 | 'The property "%s" for the test "%s" has already been set.', |
111 | $property->getUri(), |
112 | $test->getUri() |
113 | ) |
114 | ); |
115 | |
116 | return true; |
117 | } |
118 | } |