Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
47.62% |
30 / 63 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TranslationDeletionService | |
47.62% |
30 / 63 |
|
33.33% |
1 / 3 |
24.37 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| deleteByRequest | |
57.14% |
24 / 42 |
|
0.00% |
0 / 1 |
8.83 | |||
| deleteByOriginResourceUri | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| 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\generis\model\resource\Contract\ResourceDeleterInterface; |
| 28 | use oat\oatbox\event\EventManager; |
| 29 | use oat\tao\model\Translation\Entity\AbstractResource; |
| 30 | use oat\tao\model\Translation\Event\TranslationActionEvent; |
| 31 | use oat\tao\model\Translation\Exception\ResourceTranslationException; |
| 32 | use oat\tao\model\Translation\Query\ResourceTranslationQuery; |
| 33 | use oat\tao\model\Translation\Repository\ResourceTranslationRepository; |
| 34 | use Psr\Http\Message\ServerRequestInterface; |
| 35 | use Psr\Log\LoggerInterface; |
| 36 | use Throwable; |
| 37 | |
| 38 | class TranslationDeletionService |
| 39 | { |
| 40 | private Ontology $ontology; |
| 41 | private ResourceDeleterInterface $resourceDeleter; |
| 42 | private ResourceTranslationRepository $resourceTranslationRepository; |
| 43 | private LoggerInterface $logger; |
| 44 | private TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer; |
| 45 | private EventManager $eventManager; |
| 46 | |
| 47 | public function __construct( |
| 48 | Ontology $ontology, |
| 49 | ResourceDeleterInterface $resourceDeleter, |
| 50 | ResourceTranslationRepository $resourceTranslationRepository, |
| 51 | LoggerInterface $logger, |
| 52 | TranslatedIntoLanguagesSynchronizer $translatedIntoLanguagesSynchronizer, |
| 53 | EventManager $eventManager |
| 54 | ) { |
| 55 | $this->ontology = $ontology; |
| 56 | $this->resourceDeleter = $resourceDeleter; |
| 57 | $this->resourceTranslationRepository = $resourceTranslationRepository; |
| 58 | $this->logger = $logger; |
| 59 | $this->translatedIntoLanguagesSynchronizer = $translatedIntoLanguagesSynchronizer; |
| 60 | $this->eventManager = $eventManager; |
| 61 | } |
| 62 | |
| 63 | public function deleteByRequest(ServerRequestInterface $request): core_kernel_classes_Resource |
| 64 | { |
| 65 | $requestParams = $request->getParsedBody(); |
| 66 | $resourceUri = $requestParams['id'] ?? null; |
| 67 | $languageUri = $requestParams['languageUri'] ?? null; |
| 68 | |
| 69 | if (empty($resourceUri)) { |
| 70 | throw new ResourceTranslationException('Resource id is required'); |
| 71 | } |
| 72 | |
| 73 | if (empty($languageUri)) { |
| 74 | throw new ResourceTranslationException('Parameter languageUri is mandatory'); |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | $resource = $this->ontology->getResource($resourceUri); |
| 79 | $rootId = $resource->getRootId(); |
| 80 | |
| 81 | $translations = $this->resourceTranslationRepository |
| 82 | ->find(new ResourceTranslationQuery([$resourceUri], $languageUri)); |
| 83 | |
| 84 | if ($translations->count() === 0) { |
| 85 | throw new ResourceTranslationException( |
| 86 | sprintf( |
| 87 | 'Translation does not exist for [id=%s, locale=%s]', |
| 88 | $resourceUri, |
| 89 | $languageUri |
| 90 | ) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** @var AbstractResource $translation */ |
| 95 | foreach ($translations as $translation) { |
| 96 | $translationResource = $this->ontology->getResource($translation->getResourceUri()); |
| 97 | |
| 98 | $this->resourceDeleter->delete($translationResource); |
| 99 | |
| 100 | $this->eventManager->trigger(new TranslationActionEvent( |
| 101 | TranslationActionEvent::ACTION_DELETED, |
| 102 | $rootId, |
| 103 | $resourceUri, |
| 104 | $translation->getResourceUri(), |
| 105 | $translation->getLanguageCode() |
| 106 | )); |
| 107 | } |
| 108 | |
| 109 | $this->translatedIntoLanguagesSynchronizer->sync($resource); |
| 110 | |
| 111 | return $resource; |
| 112 | } catch (Throwable $exception) { |
| 113 | $this->logger->error( |
| 114 | sprintf( |
| 115 | 'Could not delete translation [id=%s, language=%s] (%s): %s', |
| 116 | $resourceUri, |
| 117 | $languageUri, |
| 118 | get_class($exception), |
| 119 | $exception->getMessage() |
| 120 | ) |
| 121 | ); |
| 122 | |
| 123 | throw $exception; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public function deleteByOriginResourceUri(string $originResourceUri): void |
| 128 | { |
| 129 | try { |
| 130 | $translations = $this->resourceTranslationRepository |
| 131 | ->find(new ResourceTranslationQuery([$originResourceUri])); |
| 132 | |
| 133 | /** @var AbstractResource $translation */ |
| 134 | foreach ($translations as $translation) { |
| 135 | $instance = $this->ontology->getResource($translation->getResourceUri()); |
| 136 | |
| 137 | $this->resourceDeleter->delete($instance); |
| 138 | } |
| 139 | } catch (Throwable $exception) { |
| 140 | $this->logger->error( |
| 141 | sprintf( |
| 142 | 'Error deleting translations by originResourceUri [%s] (%s): %s', |
| 143 | $originResourceUri, |
| 144 | get_class($exception), |
| 145 | $exception->getMessage() |
| 146 | ) |
| 147 | ); |
| 148 | |
| 149 | throw $exception; |
| 150 | } |
| 151 | } |
| 152 | } |