Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.96% |
40 / 46 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| TranslationItemEventListener | |
86.96% |
40 / 46 |
|
75.00% |
6 / 8 |
14.43 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| populateTranslationProperties | |
70.00% |
7 / 10 |
|
0.00% |
0 / 1 |
4.43 | |||
| deleteTranslations | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setLanguage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setTranslationType | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setTranslationStatus | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| isPropertySet | |
100.00% |
10 / 10 |
|
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 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoItems\model\Translation\Listener; |
| 24 | |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use InvalidArgumentException; |
| 28 | use oat\generis\model\data\Ontology; |
| 29 | use oat\oatbox\event\Event; |
| 30 | use oat\oatbox\service\ServiceManager; |
| 31 | use oat\tao\model\featureFlag\FeatureFlagCheckerInterface; |
| 32 | use oat\tao\model\TaoOntology; |
| 33 | use oat\tao\model\Translation\Service\ResourceLanguageRetriever; |
| 34 | use oat\tao\model\Translation\Service\TranslationDeletionService; |
| 35 | use oat\taoItems\model\event\ItemCreatedEvent; |
| 36 | use oat\taoItems\model\event\ItemRemovedEvent; |
| 37 | use oat\taoItems\model\event\ItemUpdatedEvent; |
| 38 | use oat\taoQtiItem\model\Translation\Service\QtiLanguageSetter; |
| 39 | use Psr\Log\LoggerInterface; |
| 40 | |
| 41 | class TranslationItemEventListener |
| 42 | { |
| 43 | private FeatureFlagCheckerInterface $featureFlagChecker; |
| 44 | private Ontology $ontology; |
| 45 | private ResourceLanguageRetriever $resourceLanguageRetriever; |
| 46 | private LoggerInterface $logger; |
| 47 | private TranslationDeletionService $translationDeletionService; |
| 48 | |
| 49 | public function __construct( |
| 50 | FeatureFlagCheckerInterface $featureFlagChecker, |
| 51 | Ontology $ontology, |
| 52 | ResourceLanguageRetriever $resourceLanguageRetriever, |
| 53 | LoggerInterface $logger, |
| 54 | TranslationDeletionService $translationDeletionService |
| 55 | ) { |
| 56 | $this->featureFlagChecker = $featureFlagChecker; |
| 57 | $this->ontology = $ontology; |
| 58 | $this->resourceLanguageRetriever = $resourceLanguageRetriever; |
| 59 | $this->logger = $logger; |
| 60 | $this->translationDeletionService = $translationDeletionService; |
| 61 | } |
| 62 | |
| 63 | public function populateTranslationProperties(Event $event): void |
| 64 | { |
| 65 | if (!$this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | if (!$event instanceof ItemCreatedEvent && !$event instanceof ItemUpdatedEvent) { |
| 70 | throw new InvalidArgumentException( |
| 71 | sprintf('Event %s is not supported to populate translation properties', get_class($event)) |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | $item = $this->ontology->getResource($event->getItemUri()); |
| 76 | |
| 77 | $this->setLanguage($item); |
| 78 | $this->setTranslationType($item); |
| 79 | $this->setTranslationStatus($item); |
| 80 | } |
| 81 | |
| 82 | public function deleteTranslations(ItemRemovedEvent $event): void |
| 83 | { |
| 84 | if ($this->featureFlagChecker->isEnabled('FEATURE_FLAG_TRANSLATION_ENABLED')) { |
| 85 | $this->translationDeletionService |
| 86 | ->deleteByOriginResourceUri($event->jsonSerialize()[ItemRemovedEvent::PAYLOAD_KEY_ITEM_URI]); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private function setLanguage(core_kernel_classes_Resource $item): void |
| 91 | { |
| 92 | $this->setProperty( |
| 93 | $item, |
| 94 | TaoOntology::PROPERTY_LANGUAGE, |
| 95 | TaoOntology::LANGUAGE_PREFIX . $this->resourceLanguageRetriever->retrieve($item) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | private function setTranslationType(core_kernel_classes_Resource $item): void |
| 100 | { |
| 101 | $this->setProperty( |
| 102 | $item, |
| 103 | TaoOntology::PROPERTY_TRANSLATION_TYPE, |
| 104 | TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | private function setTranslationStatus(core_kernel_classes_Resource $item): void |
| 109 | { |
| 110 | $this->setProperty( |
| 111 | $item, |
| 112 | TaoOntology::PROPERTY_TRANSLATION_STATUS, |
| 113 | TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_NOT_READY |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | private function setProperty(core_kernel_classes_Resource $item, string $propertyUri, string $value): void |
| 118 | { |
| 119 | $property = $this->ontology->getProperty($propertyUri); |
| 120 | |
| 121 | if (!$this->isPropertySet($item, $property)) { |
| 122 | $item->editPropertyValues($property, $value); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private function isPropertySet(core_kernel_classes_Resource $item, core_kernel_classes_Property $property): bool |
| 127 | { |
| 128 | if (empty($item->getOnePropertyValue($property))) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | $this->logger->info( |
| 133 | sprintf( |
| 134 | 'The property "%s" for the item "%s" has already been set.', |
| 135 | $property->getUri(), |
| 136 | $item->getUri() |
| 137 | ) |
| 138 | ); |
| 139 | |
| 140 | return true; |
| 141 | } |
| 142 | } |