Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.96% |
40 / 46 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
TranslationItemEventListener | |
86.96% |
40 / 46 |
|
75.00% |
6 / 8 |
14.43 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
populateTranslationProperties | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
4.43 | |||
deleteTranslations | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
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\taoItems\model\Translation\Listener; |
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\oatbox\event\Event; |
30 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
31 | use oat\tao\model\TaoOntology; |
32 | use oat\tao\model\Translation\Service\ResourceLanguageRetriever; |
33 | use oat\tao\model\Translation\Service\TranslationDeletionService; |
34 | use oat\taoItems\model\event\ItemCreatedEvent; |
35 | use oat\taoItems\model\event\ItemRemovedEvent; |
36 | use oat\taoItems\model\event\ItemUpdatedEvent; |
37 | use Psr\Log\LoggerInterface; |
38 | |
39 | class TranslationItemEventListener |
40 | { |
41 | private FeatureFlagCheckerInterface $featureFlagChecker; |
42 | private Ontology $ontology; |
43 | private ResourceLanguageRetriever $resourceLanguageRetriever; |
44 | private LoggerInterface $logger; |
45 | private TranslationDeletionService $translationDeletionService; |
46 | |
47 | public function __construct( |
48 | FeatureFlagCheckerInterface $featureFlagChecker, |
49 | Ontology $ontology, |
50 | ResourceLanguageRetriever $resourceLanguageRetriever, |
51 | LoggerInterface $logger, |
52 | TranslationDeletionService $translationDeletionService |
53 | ) { |
54 | $this->featureFlagChecker = $featureFlagChecker; |
55 | $this->ontology = $ontology; |
56 | $this->resourceLanguageRetriever = $resourceLanguageRetriever; |
57 | $this->logger = $logger; |
58 | $this->translationDeletionService = $translationDeletionService; |
59 | } |
60 | |
61 | public function populateTranslationProperties(Event $event): void |
62 | { |
63 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
64 | return; |
65 | } |
66 | |
67 | if (!$event instanceof ItemCreatedEvent && !$event instanceof ItemUpdatedEvent) { |
68 | throw new InvalidArgumentException( |
69 | sprintf('Event %s is not supported to populate translation properties', get_class($event)) |
70 | ); |
71 | } |
72 | |
73 | $item = $this->ontology->getResource($event->getItemUri()); |
74 | |
75 | $this->setLanguage($item); |
76 | $this->setTranslationType($item); |
77 | $this->setTranslationStatus($item); |
78 | } |
79 | |
80 | public function deleteTranslations(ItemRemovedEvent $event): void |
81 | { |
82 | if ($this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
83 | $this->translationDeletionService |
84 | ->deleteByOriginResourceUri($event->jsonSerialize()[ItemRemovedEvent::PAYLOAD_KEY_ITEM_URI]); |
85 | } |
86 | } |
87 | |
88 | private function setLanguage(core_kernel_classes_Resource $item): void |
89 | { |
90 | $this->setProperty( |
91 | $item, |
92 | TaoOntology::PROPERTY_LANGUAGE, |
93 | TaoOntology::LANGUAGE_PREFIX . $this->resourceLanguageRetriever->retrieve($item) |
94 | ); |
95 | } |
96 | |
97 | private function setTranslationType(core_kernel_classes_Resource $item): void |
98 | { |
99 | $this->setProperty( |
100 | $item, |
101 | TaoOntology::PROPERTY_TRANSLATION_TYPE, |
102 | TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL |
103 | ); |
104 | } |
105 | |
106 | private function setTranslationStatus(core_kernel_classes_Resource $item): void |
107 | { |
108 | $this->setProperty( |
109 | $item, |
110 | TaoOntology::PROPERTY_TRANSLATION_STATUS, |
111 | TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_NOT_READY |
112 | ); |
113 | } |
114 | |
115 | private function setProperty(core_kernel_classes_Resource $item, string $propertyUri, string $value): void |
116 | { |
117 | $property = $this->ontology->getProperty($propertyUri); |
118 | |
119 | if (!$this->isPropertySet($item, $property)) { |
120 | $item->editPropertyValues($property, $value); |
121 | } |
122 | } |
123 | |
124 | private function isPropertySet(core_kernel_classes_Resource $item, core_kernel_classes_Property $property): bool |
125 | { |
126 | if (empty($item->getOnePropertyValue($property))) { |
127 | return false; |
128 | } |
129 | |
130 | $this->logger->info( |
131 | sprintf( |
132 | 'The property "%s" for the item "%s" has already been set.', |
133 | $property->getUri(), |
134 | $item->getUri() |
135 | ) |
136 | ); |
137 | |
138 | return true; |
139 | } |
140 | } |