Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.33% |
53 / 60 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
TranslationUpdateService | |
88.33% |
53 / 60 |
|
75.00% |
3 / 4 |
8.10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
update | |
85.42% |
41 / 48 |
|
0.00% |
0 / 1 |
5.08 | |||
getOriginalResourceUri | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getLocaleCode | |
100.00% |
4 / 4 |
|
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) 2024 (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_Resource; |
26 | use oat\generis\model\data\Ontology; |
27 | use oat\oatbox\event\EventManager; |
28 | use oat\tao\model\TaoOntology; |
29 | use oat\tao\model\Translation\Command\UpdateTranslationCommand; |
30 | use oat\tao\model\Translation\Event\TranslationActionEvent; |
31 | use oat\tao\model\Translation\Exception\ResourceTranslationException; |
32 | use Psr\Log\LoggerInterface; |
33 | use Throwable; |
34 | |
35 | class TranslationUpdateService |
36 | { |
37 | private Ontology $ontology; |
38 | private LoggerInterface $logger; |
39 | private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer; |
40 | private EventManager $eventManager; |
41 | |
42 | public function __construct( |
43 | Ontology $ontology, |
44 | LoggerInterface $logger, |
45 | TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer, |
46 | EventManager $eventManager |
47 | ) { |
48 | $this->ontology = $ontology; |
49 | $this->logger = $logger; |
50 | $this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer; |
51 | $this->eventManager = $eventManager; |
52 | } |
53 | |
54 | public function update(UpdateTranslationCommand $command): core_kernel_classes_Resource |
55 | { |
56 | try { |
57 | $instance = $this->ontology->getResource($command->getResourceUri()); |
58 | |
59 | if (!$instance->exists()) { |
60 | throw new ResourceTranslationException( |
61 | sprintf( |
62 | 'Resource %s does not exist', |
63 | $command->getResourceUri() |
64 | ) |
65 | ); |
66 | } |
67 | |
68 | $translationType = $instance->getOnePropertyValue( |
69 | $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_TYPE) |
70 | ); |
71 | |
72 | if ($translationType->getUri() !== TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_TRANSLATION) { |
73 | throw new ResourceTranslationException( |
74 | sprintf( |
75 | 'Resource %s is not a translation', |
76 | $command->getResourceUri() |
77 | ) |
78 | ); |
79 | } |
80 | |
81 | $translationProgressProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_PROGRESS); |
82 | $oldProgressUri = $instance->getOnePropertyValue($translationProgressProperty)->getUri(); |
83 | |
84 | if ($oldProgressUri === $command->getProgressUri()) { |
85 | return $instance; |
86 | } |
87 | |
88 | $instance->editPropertyValues($translationProgressProperty, $command->getProgressUri()); |
89 | |
90 | $this->translatedIntoLanguagesSynchronizer->sync($instance); |
91 | |
92 | $this->eventManager->trigger(new TranslationActionEvent( |
93 | TranslationActionEvent::ACTION_UPDATED, |
94 | $instance->getRootId(), |
95 | $this->getOriginalResourceUri($instance), |
96 | $instance->getUri(), |
97 | $this->getLocaleCode($instance), |
98 | [ |
99 | $translationProgressProperty->getUri() => [ |
100 | 'old' => $oldProgressUri, |
101 | 'new' => $command->getProgressUri(), |
102 | ], |
103 | ] |
104 | )); |
105 | |
106 | return $instance; |
107 | } catch (Throwable $exception) { |
108 | $this->logger->error( |
109 | sprintf( |
110 | 'Could not update translation status of [resourceUri=%s] (%s): %s', |
111 | $command->getResourceUri(), |
112 | get_class($exception), |
113 | $exception->getMessage() |
114 | ) |
115 | ); |
116 | |
117 | throw $exception; |
118 | } |
119 | } |
120 | |
121 | private function getOriginalResourceUri(core_kernel_classes_Resource $translation): string |
122 | { |
123 | $originalResourceUri = $translation->getOnePropertyValue( |
124 | $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI) |
125 | ); |
126 | |
127 | return $originalResourceUri->getUri(); |
128 | } |
129 | |
130 | private function getLocaleCode(core_kernel_classes_Resource $translation): string |
131 | { |
132 | $language = $translation->getOnePropertyValue( |
133 | $this->ontology->getProperty(TaoOntology::PROPERTY_LANGUAGE) |
134 | ); |
135 | |
136 | return str_replace(TaoOntology::LANGUAGE_PREFIX, '', $language->getUri()); |
137 | } |
138 | } |