Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.17% |
38 / 48 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ResourceTranslationRepository | |
79.17% |
38 / 48 |
|
50.00% |
1 / 2 |
5.23 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
77.27% |
34 / 44 |
|
0.00% |
0 / 1 |
4.19 | |||
| 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\Repository; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\data\Ontology; |
| 27 | use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService; |
| 28 | use oat\search\helper\SupportedOperatorHelper; |
| 29 | use oat\tao\model\TaoOntology; |
| 30 | use oat\tao\model\Translation\Entity\ResourceCollection; |
| 31 | use oat\tao\model\Translation\Exception\ResourceTranslationException; |
| 32 | use oat\tao\model\Translation\Factory\ResourceTranslationFactory; |
| 33 | use oat\tao\model\Translation\Query\ResourceTranslationQuery; |
| 34 | use Psr\Log\LoggerInterface; |
| 35 | use Throwable; |
| 36 | |
| 37 | class ResourceTranslationRepository |
| 38 | { |
| 39 | private Ontology $ontology; |
| 40 | private ComplexSearchService $complexSearch; |
| 41 | private LoggerInterface $logger; |
| 42 | private ResourceTranslationFactory $factory; |
| 43 | |
| 44 | public function __construct( |
| 45 | Ontology $ontology, |
| 46 | ComplexSearchService $complexSearch, |
| 47 | ResourceTranslationFactory $resourceTranslationFactory, |
| 48 | LoggerInterface $logger |
| 49 | ) { |
| 50 | $this->ontology = $ontology; |
| 51 | $this->complexSearch = $complexSearch; |
| 52 | $this->factory = $resourceTranslationFactory; |
| 53 | $this->logger = $logger; |
| 54 | } |
| 55 | |
| 56 | public function find(ResourceTranslationQuery $query): ResourceCollection |
| 57 | { |
| 58 | $output = []; |
| 59 | $resourceUris = $query->getResourceUris(); |
| 60 | $languageUri = $query->getLanguageUri(); |
| 61 | |
| 62 | $queryBuilder = $this->complexSearch->query(); |
| 63 | $searchQuery = $this->complexSearch->searchType( |
| 64 | $queryBuilder, |
| 65 | 'http://www.tao.lu/Ontologies/TAO.rdf#AssessmentContentObject', |
| 66 | true |
| 67 | ); |
| 68 | $searchQuery->addCriterion( |
| 69 | TaoOntology::PROPERTY_TRANSLATION_TYPE, |
| 70 | SupportedOperatorHelper::EQUAL, |
| 71 | TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_TRANSLATION |
| 72 | ); |
| 73 | $searchQuery->addCriterion( |
| 74 | TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI, |
| 75 | SupportedOperatorHelper::IN, |
| 76 | $resourceUris |
| 77 | ); |
| 78 | |
| 79 | if ($languageUri) { |
| 80 | $searchQuery->addCriterion( |
| 81 | TaoOntology::PROPERTY_LANGUAGE, |
| 82 | SupportedOperatorHelper::EQUAL, |
| 83 | $languageUri |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | $queryBuilder->setCriteria($searchQuery); |
| 88 | |
| 89 | $result = $this->complexSearch->getGateway()->search($queryBuilder); |
| 90 | $originResources = []; |
| 91 | $originResourceProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_ORIGINAL_RESOURCE_URI); |
| 92 | |
| 93 | /** @var core_kernel_classes_Resource $translationResource */ |
| 94 | foreach ($result as $translationResource) { |
| 95 | try { |
| 96 | $originResourceUri = $translationResource->getOnePropertyValue($originResourceProperty)->getUri(); |
| 97 | $originResources[$originResourceUri] ??= $this->ontology->getResource($originResourceUri); |
| 98 | |
| 99 | $output[] = $this->factory->create($originResources[$originResourceUri], $translationResource); |
| 100 | } catch (Throwable $exception) { |
| 101 | $this->logger->warning( |
| 102 | sprintf( |
| 103 | 'Cannot read translation status for [ids=%s, translation=%s]: %s - %s', |
| 104 | implode(',', $resourceUris), |
| 105 | $translationResource->getUri(), |
| 106 | $exception->getMessage(), |
| 107 | $exception->getTraceAsString() |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return new ResourceCollection(...$output); |
| 114 | } |
| 115 | } |