Skip to content

Commit

Permalink
add functionality for AdminSettingsSubscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig committed Feb 1, 2024
1 parent 9346125 commit d164b91
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/EventSubscriber/AdminSettingsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

Check failure on line 1 in src/EventSubscriber/AdminSettingsSubscriber.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Missing strict types declaration

Strict types declaration is missing

Check failure on line 1 in src/EventSubscriber/AdminSettingsSubscriber.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Missing strict types declaration

Strict types declaration is missing
/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/

namespace Pimcore\Bundle\GenericDataIndexBundle\EventSubscriber;

use Exception;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\IndexUpdateServiceInterface;
use Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex\LanguageServiceInterface;
use Pimcore\Event\SystemEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* @internal
*/
final class AdminSettingsSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly IndexUpdateServiceInterface $indexUpdateService,
private readonly LanguageServiceInterface $languageService
) {
}

public static function getSubscribedEvents(): array
{
return [
SystemEvents::SAVE_ACTION_SYSTEM_SETTINGS => 'updateSearchIndex',
];
}

/**
* @throws Exception
*/
public function updateSearchIndex(GenericEvent $event): void
{
$arguments = $event->getArguments();
$currentLanguages = $arguments['existingValues']['general']['valid_languages'];
$newLanguages = $this->languageService->getNewLanguages(
explode(',', $arguments['values']['general.validLanguages'])
);

if (empty($currentLanguages) && !empty($newLanguages)) {
$this->setLanguagesAndUpdate($newLanguages);

return;
}

sort($currentLanguages);
sort($newLanguages);

if ($currentLanguages !== $newLanguages) {
$this->setLanguagesAndUpdate($newLanguages);
}
}

/**
* @throws Exception
*/
private function setLanguagesAndUpdate(
array $newLanguages
): void
{
$this->languageService->setValidLanguages($newLanguages);
$this->indexUpdateService->updateAll();
}
}
19 changes: 19 additions & 0 deletions src/Service/SearchIndex/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@

namespace Pimcore\Bundle\GenericDataIndexBundle\Service\SearchIndex;

use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Tool;

/**
* @internal
*/
final class LanguageService implements LanguageServiceInterface
{
public function __construct(
private readonly LocaleServiceInterface $localeService,
) {
}

private array $validLanguages = [];

public function setValidLanguages(array $argLanguages): void
Expand All @@ -35,4 +41,17 @@ public function getValidLanguages(): array

return $this->validLanguages;
}

public function getNewLanguages(array $validLanguages): array
{
$newLanguages = [];

foreach ($validLanguages as $language) {
if ($this->localeService->isLocale($language)) {
$newLanguages[] = $language;
}
}

return $newLanguages;
}
}
2 changes: 2 additions & 0 deletions src/Service/SearchIndex/LanguageServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ interface LanguageServiceInterface
public function setValidLanguages(array $argLanguages): void;

public function getValidLanguages(): array;

public function getNewLanguages(array $validLanguages): array;
}

0 comments on commit d164b91

Please sign in to comment.