Skip to content

Commit

Permalink
chore: use translation language retriever instead of user language se…
Browse files Browse the repository at this point in the history
…rvice, beautify the code
  • Loading branch information
shpran committed Sep 30, 2024
1 parent 72f0a8a commit f19f24f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
50 changes: 26 additions & 24 deletions models/classes/Translation/Listener/TestCreatedEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
use core_kernel_classes_Property;
use core_kernel_classes_Resource;
use oat\generis\model\data\Ontology;
use oat\oatbox\user\UserLanguageServiceInterface;
use oat\tao\model\featureFlag\FeatureFlagCheckerInterface;
use oat\tao\model\TaoOntology;
use oat\tao\model\Translation\Service\ResourceLanguageRetriever;
use oat\taoTests\models\event\TestCreatedEvent;
use Psr\Log\LoggerInterface;

class TestCreatedEventListener
{
private FeatureFlagCheckerInterface $featureFlagChecker;
private Ontology $ontology;
private UserLanguageServiceInterface $userLanguageService;
private ResourceLanguageRetriever $resourceLanguageRetriever;
private LoggerInterface $logger;

public function __construct(
FeatureFlagCheckerInterface $featureFlagChecker,
Ontology $ontology,
UserLanguageServiceInterface $userLanguageService,
ResourceLanguageRetriever $resourceLanguageRetriever,
LoggerInterface $logger
) {
$this->featureFlagChecker = $featureFlagChecker;
$this->ontology = $ontology;
$this->userLanguageService = $userLanguageService;
$this->resourceLanguageRetriever = $resourceLanguageRetriever;
$this->logger = $logger;
}

Expand All @@ -65,36 +65,38 @@ public function populateTranslationProperties(TestCreatedEvent $event): void

private function setLanguage(core_kernel_classes_Resource $test): void
{
$translationLanguageProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_LANGUAGE);

if ($this->isPropertySet($test, $translationLanguageProperty)) {
return;
}

$defaultLanguage = $this->userLanguageService->getDefaultLanguage();
$test->editPropertyValues($translationLanguageProperty, TaoOntology::LANGUAGE_PREFIX . $defaultLanguage);
$this->setProperty(
$test,
TaoOntology::PROPERTY_LANGUAGE,
TaoOntology::LANGUAGE_PREFIX . $this->resourceLanguageRetriever->retrieve($test)
);
}

private function setTranslationType(core_kernel_classes_Resource $test): void
{
$translationTypeProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_TYPE);

if ($this->isPropertySet($test, $translationTypeProperty)) {
return;
}

$test->editPropertyValues($translationTypeProperty, TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL);
$this->setProperty(
$test,
TaoOntology::PROPERTY_TRANSLATION_TYPE,
TaoOntology::PROPERTY_VALUE_TRANSLATION_TYPE_ORIGINAL
);
}

private function setTranslationStatus(core_kernel_classes_Resource $test): void
{
$translationStatusProperty = $this->ontology->getProperty(TaoOntology::PROPERTY_TRANSLATION_STATUS);
$this->setProperty(
$test,
TaoOntology::PROPERTY_TRANSLATION_STATUS,
TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_NOT_READY
);
}

private function setProperty(core_kernel_classes_Resource $test, string $propertyUri, string $value): void
{
$property = $this->ontology->getProperty($propertyUri);

if ($this->isPropertySet($test, $translationStatusProperty)) {
return;
if (!$this->isPropertySet($test, $property)) {
$test->editPropertyValues($property, $value);
}

$test->editPropertyValues($translationStatusProperty, TaoOntology::PROPERTY_VALUE_TRANSLATION_STATUS_NOT_READY);
}

private function isPropertySet(core_kernel_classes_Resource $test, core_kernel_classes_Property $property): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
use oat\generis\model\data\Ontology;
use oat\generis\model\DependencyInjection\ContainerServiceProviderInterface;
use oat\oatbox\log\LoggerService;
use oat\oatbox\user\UserLanguageService;
use oat\tao\model\TaoOntology;
use oat\tao\model\featureFlag\FeatureFlagChecker;
use oat\tao\model\Translation\Form\Modifier\TranslationFormModifier as TaoTranslationFormModifier;
use oat\tao\model\Translation\Service\ResourceLanguageRetriever;
use oat\tao\model\Translation\Service\ResourceMetadataPopulateService;
use oat\tao\model\Translation\Service\TranslationCreationService;
use oat\taoTests\models\TaoTestOntology;
Expand Down Expand Up @@ -87,7 +87,7 @@ public function __invoke(ContainerConfigurator $configurator): void
->args([
service(FeatureFlagChecker::class),
service(Ontology::SERVICE_ID),
service(UserLanguageService::SERVICE_ID),
service(ResourceLanguageRetriever::class),
service(LoggerService::SERVICE_ID),
]);

Expand Down
2 changes: 1 addition & 1 deletion scripts/install/SetupEventListeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use oat\oatbox\extension\InstallAction;
use oat\taoItems\model\event\ItemCreatedEvent;
use oat\taoItems\model\Translation\Listener\ItemCreatedEventListener;
use oat\taoItems\model\Translation\Listener\TranslationItemEventListener;
use oat\taoTests\models\event\TestCreatedEvent;
use oat\taoTests\models\Translation\Listener\TestCreatedEventListener;

Expand Down

0 comments on commit f19f24f

Please sign in to comment.