Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslatedIntoLanguagesSynchronizer
87.50% covered (warning)
87.50%
21 / 24
50.00% covered (danger)
50.00%
2 / 4
7.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addCallback
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 sync
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
3.00
 getOriginalResource
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
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) 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\TaoOntology;
28use oat\tao\model\Translation\Entity\AbstractResource;
29use oat\tao\model\Translation\Query\ResourceTranslationQuery;
30use oat\tao\model\Translation\Repository\ResourceTranslationRepository;
31
32class TranslatedIntoLanguagesSynchronizer
33{
34    private Ontology $ontology;
35    private ResourceTranslationRepository $resourceTranslationRepository;
36    private array $callbacks;
37
38    public function __construct(Ontology $ontology, ResourceTranslationRepository $resourceTranslationRepository)
39    {
40        $this->ontology = $ontology;
41        $this->resourceTranslationRepository = $resourceTranslationRepository;
42    }
43
44    public function addCallback(string $type, callable $callback): void
45    {
46        $this->callbacks[$type] = $this->callbacks[$type] ?? [];
47        $this->callbacks[$type][] = $callback;
48    }
49
50    public function sync(core_kernel_classes_Resource $resource): void
51    {
52        $originalResource = $this->getOriginalResource($resource);
53
54        $property = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATED_INTO_LANGUAGES);
55        $originalResource->removePropertyValues($property);
56
57        /** @var AbstractResource[] $translations */
58        $translations = $this->resourceTranslationRepository->find(
59            new ResourceTranslationQuery([$originalResource->getUri()])
60        );
61
62        foreach ($translations as $translation) {
63            $originalResource->setPropertyValue(
64                $property,
65                TaoOntology::LANGUAGE_PREFIX . $translation->getLanguageCode()
66            );
67        }
68
69        foreach (($this->callbacks[$originalResource->getRootId()] ?? []) as $callback) {
70            $callback($originalResource);
71        }
72    }
73
74    private function getOriginalResource(core_kernel_classes_Resource $resource): core_kernel_classes_Resource
75    {
76        $originalResourceUriProperty = $this->ontology->getProperty(
77            TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI
78        );
79        $originalResourceUri = $resource->getOnePropertyValue($originalResourceUriProperty);
80
81        return !empty($originalResourceUri)
82            ? $this->ontology->getResource($originalResourceUri)
83            : $resource;
84    }
85}