Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.17% covered (warning)
83.17%
84 / 101
33.33% covered (danger)
33.33%
2 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationCreationService
83.17% covered (warning)
83.17%
84 / 101
33.33% covered (danger)
33.33%
2 / 6
19.54
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 setResourceTransfer
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addPostCreation
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 createByRequest
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 create
94.87% covered (success)
94.87%
74 / 78
0.00% covered (danger)
0.00%
0 / 1
10.01
 getResourceTransfer
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
2.50
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
21declare(strict_types=1);
22
23namespace oat\tao\model\Translation\Service;
24
25use core_kernel_classes_Resource;
26use oat\generis\model\data\Ontology;
27use oat\tao\model\Language\Business\Contract\LanguageRepositoryInterface;
28use oat\tao\model\Language\Language;
29use oat\tao\model\resources\Command\ResourceTransferCommand;
30use oat\tao\model\resources\Contract\ResourceTransferInterface;
31use oat\tao\model\TaoOntology;
32use oat\tao\model\Translation\Command\CreateTranslationCommand;
33use oat\tao\model\Translation\Entity\ResourceTranslatable;
34use oat\tao\model\Translation\Exception\ResourceTranslationException;
35use oat\tao\model\Translation\Query\ResourceTranslatableQuery;
36use oat\tao\model\Translation\Query\ResourceTranslationQuery;
37use oat\tao\model\Translation\Repository\ResourceTranslatableRepository;
38use oat\tao\model\Translation\Repository\ResourceTranslationRepository;
39use Psr\Http\Message\ServerRequestInterface;
40use Psr\Log\LoggerInterface;
41use Throwable;
42
43class TranslationCreationService
44{
45    private Ontology $ontology;
46    private ResourceTranslatableRepository $resourceTranslatableRepository;
47    private ResourceTranslationRepository $resourceTranslationRepository;
48    private LanguageRepositoryInterface $languageRepository;
49    private LoggerInterface $logger;
50    private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer;
51
52    private array $resourceTransferServices;
53    private array $callables;
54
55    public function __construct(
56        Ontology $ontology,
57        ResourceTranslatableRepository $resourceTranslatableRepository,
58        ResourceTranslationRepository $resourceTranslationRepository,
59        LanguageRepositoryInterface $languageRepository,
60        LoggerInterface $logger,
61        TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer
62    ) {
63        $this->ontology = $ontology;
64        $this->resourceTranslatableRepository = $resourceTranslatableRepository;
65        $this->resourceTranslationRepository = $resourceTranslationRepository;
66        $this->languageRepository = $languageRepository;
67        $this->logger = $logger;
68        $this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer;
69    }
70
71    public function setResourceTransfer(string $resourceType, ResourceTransferInterface $resourceTransfer): void
72    {
73        $this->resourceTransferServices[$resourceType] = $resourceTransfer;
74    }
75
76    public function addPostCreation(string $resourceType, callable $callable): void
77    {
78        $this->callables[$resourceType] ??= [];
79        $this->callables[$resourceType][] = $callable;
80    }
81
82    public function createByRequest(ServerRequestInterface $request): core_kernel_classes_Resource
83    {
84        $requestParams = $request->getParsedBody();
85        $id = $requestParams['id'] ?? null;
86        $languageUri = $requestParams['languageUri'] ?? null;
87
88        if (empty($id)) {
89            throw new ResourceTranslationException('Resource id is required');
90        }
91
92        if (empty($languageUri)) {
93            throw new ResourceTranslationException('Parameter languageUri is mandatory');
94        }
95
96        return $this->create(new CreateTranslationCommand($id, $languageUri));
97    }
98
99    public function create(CreateTranslationCommand $command): core_kernel_classes_Resource
100    {
101        try {
102            $resourceUri = $command->getResourceUri();
103            $languageUri = $command->getLanguageUri();
104
105            $translations = $this->resourceTranslationRepository->find(
106                new ResourceTranslationQuery([$resourceUri], $languageUri)
107            );
108
109            if ($translations->count() > 0) {
110                throw new ResourceTranslationException(
111                    sprintf(
112                        'Translation already exists for [id=%s, locale=%s]',
113                        $resourceUri,
114                        $languageUri
115                    )
116                );
117            }
118
119            $resources = $this->resourceTranslatableRepository->find(new ResourceTranslatableQuery($resourceUri));
120
121            if ($resources->count() === 0) {
122                throw new ResourceTranslationException(sprintf('Resource [id=%s] is not translatable', $resourceUri));
123            }
124
125            /** @var ResourceTranslatable $resource */
126            $resource = $resources->current();
127
128            if (!$resource->isReadyForTranslation()) {
129                throw new ResourceTranslationException(
130                    sprintf(
131                        'Resource [id=%s] is not ready for translation',
132                        $resourceUri
133                    )
134                );
135            }
136
137            $existingLanguages = $this->languageRepository->findAvailableLanguagesByUsage();
138            $language = null;
139
140            /** @var Language $language */
141            foreach ($existingLanguages as $existingLanguage) {
142                if ($existingLanguage->getUri() === $languageUri) {
143                    $language = $existingLanguage;
144                }
145            }
146
147            if (!$language) {
148                throw new ResourceTranslationException(sprintf('Language %s does not exist', $languageUri));
149            }
150
151            if ($resource->getLanguageUri() === $language->getUri()) {
152                throw new ResourceTranslationException(
153                    sprintf('Cannot translate to original language %s', $languageUri)
154                );
155            }
156
157            $instance = $this->ontology->getResource($resourceUri);
158            $rootId = $instance->getRootId();
159
160            $clonedInstanceUri = $this->getResourceTransfer($rootId)->transfer(
161                new ResourceTransferCommand(
162                    $resourceUri,
163                    $instance->getParentClassId(),
164                    null,
165                    null
166                )
167            )->getDestination();
168            $clonedInstance = $this->ontology->getResource($clonedInstanceUri);
169            $clonedInstance->setLabel(sprintf('%s (%s)', $instance->getLabel(), $language->getCode()));
170
171            $clonedInstance->editPropertyValues(
172                $this->ontology->getProperty(TaoOntology::PROPERTY_LANGUAGE),
173                $language->getUri()
174            );
175
176            $clonedInstance->editPropertyValues(
177                $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_TYPE),
178                TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_TRANSLATION
179            );
180
181            $clonedInstance->editPropertyValues(
182                $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_PROGRESS),
183                TaoOntology::PROPERTY_VALUE_TRANSLATION_PROGRESS_PENDING
184            );
185
186            $clonedInstance->editPropertyValues(
187                $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI),
188                $resourceUri
189            );
190
191            foreach ($this->callables[$rootId] ?? [] as $callable) {
192                $callable($clonedInstance);
193            }
194
195            $this->translatedIntoLanguagesSynchronizer->sync($instance);
196
197            return $clonedInstance;
198        } catch (Throwable $exception) {
199            $this->logger->error(
200                sprintf(
201                    'Could not translate [id=%s, language=%s] (%s): %s',
202                    $resourceUri,
203                    $languageUri,
204                    get_class($exception),
205                    $exception->getMessage()
206                )
207            );
208
209            throw $exception;
210        }
211    }
212
213    private function getResourceTransfer(string $resourceType): ResourceTransferInterface
214    {
215        $service = $this->resourceTransferServices[$resourceType] ?? null;
216
217        if ($service) {
218            return $service;
219        }
220
221        throw new ResourceTranslationException(
222            sprintf('Missing ResourceTransfer for resource type %s', $resourceType)
223        );
224    }
225}