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