Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| TranslationMoveService | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| moveTranslations | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| changeInstanceClassReference | |
100.00% |
4 / 4 |
|
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) 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_Class; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\generis\model\data\Ontology; |
| 28 | use oat\tao\model\resources\Command\ResourceTransferCommand; |
| 29 | use oat\tao\model\Translation\Entity\AbstractResource; |
| 30 | use oat\tao\model\Translation\Query\ResourceTranslationQuery; |
| 31 | use oat\tao\model\Translation\Repository\ResourceTranslationRepository; |
| 32 | use Psr\Log\LoggerInterface; |
| 33 | use Throwable; |
| 34 | |
| 35 | class TranslationMoveService |
| 36 | { |
| 37 | private Ontology $ontology; |
| 38 | private ResourceTranslationRepository $resourceTranslationRepository; |
| 39 | private LoggerInterface $logger; |
| 40 | |
| 41 | |
| 42 | public function __construct( |
| 43 | Ontology $ontology, |
| 44 | ResourceTranslationRepository $resourceTranslationRepository, |
| 45 | LoggerInterface $logger, |
| 46 | ) { |
| 47 | $this->ontology = $ontology; |
| 48 | $this->resourceTranslationRepository = $resourceTranslationRepository; |
| 49 | $this->logger = $logger; |
| 50 | } |
| 51 | |
| 52 | public function moveTranslations(ResourceTransferCommand $command): void |
| 53 | { |
| 54 | try { |
| 55 | $destinationClass = $this->ontology->getClass($command->getTo()); |
| 56 | $translations = $this->resourceTranslationRepository |
| 57 | ->find(new ResourceTranslationQuery([$command->getFrom()])); |
| 58 | |
| 59 | /** @var AbstractResource $translation */ |
| 60 | foreach ($translations as $translation) { |
| 61 | $instance = $this->ontology->getResource($translation->getResourceUri()); |
| 62 | $this->changeInstanceClassReference($instance, $destinationClass); |
| 63 | } |
| 64 | } catch (Throwable $exception) { |
| 65 | $this->logger->error( |
| 66 | sprintf( |
| 67 | 'Error moving translations for originResourceUri [%s] (%s): %s', |
| 68 | $command->getTo(), |
| 69 | get_class($exception), |
| 70 | $exception->getMessage() |
| 71 | ) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private function changeInstanceClassReference( |
| 77 | core_kernel_classes_Resource $instance, |
| 78 | core_kernel_classes_Class $destinationClass |
| 79 | ): void { |
| 80 | $fromClasses = $instance->getTypes(); |
| 81 | |
| 82 | foreach ($fromClasses as $fromClass) { |
| 83 | $instance->removeType($fromClass); |
| 84 | } |
| 85 | |
| 86 | $instance->setType($destinationClass); |
| 87 | } |
| 88 | } |