vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/LanguageService.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\LanguageService as LanguageServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\Language;
  10. use eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct;
  11. use eZ\Publish\API\Repository\Events\Language\BeforeCreateLanguageEvent;
  12. use eZ\Publish\API\Repository\Events\Language\BeforeDeleteLanguageEvent;
  13. use eZ\Publish\API\Repository\Events\Language\BeforeDisableLanguageEvent;
  14. use eZ\Publish\API\Repository\Events\Language\BeforeEnableLanguageEvent;
  15. use eZ\Publish\API\Repository\Events\Language\BeforeUpdateLanguageNameEvent;
  16. use eZ\Publish\API\Repository\Events\Language\CreateLanguageEvent;
  17. use eZ\Publish\API\Repository\Events\Language\DeleteLanguageEvent;
  18. use eZ\Publish\API\Repository\Events\Language\DisableLanguageEvent;
  19. use eZ\Publish\API\Repository\Events\Language\EnableLanguageEvent;
  20. use eZ\Publish\API\Repository\Events\Language\UpdateLanguageNameEvent;
  21. use eZ\Publish\SPI\Repository\Decorator\LanguageServiceDecorator;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. class LanguageService extends LanguageServiceDecorator
  24. {
  25.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  26.     protected $eventDispatcher;
  27.     public function __construct(
  28.         LanguageServiceInterface $innerService,
  29.         EventDispatcherInterface $eventDispatcher
  30.     ) {
  31.         parent::__construct($innerService);
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     public function createLanguage(LanguageCreateStruct $languageCreateStruct): Language
  35.     {
  36.         $eventData = [$languageCreateStruct];
  37.         $beforeEvent = new BeforeCreateLanguageEvent(...$eventData);
  38.         $this->eventDispatcher->dispatch($beforeEvent);
  39.         if ($beforeEvent->isPropagationStopped()) {
  40.             return $beforeEvent->getLanguage();
  41.         }
  42.         $language $beforeEvent->hasLanguage()
  43.             ? $beforeEvent->getLanguage()
  44.             : $this->innerService->createLanguage($languageCreateStruct);
  45.         $this->eventDispatcher->dispatch(
  46.             new CreateLanguageEvent($language, ...$eventData)
  47.         );
  48.         return $language;
  49.     }
  50.     public function updateLanguageName(
  51.         Language $language,
  52.         string $newName
  53.     ): Language {
  54.         $eventData = [
  55.             $language,
  56.             $newName,
  57.         ];
  58.         $beforeEvent = new BeforeUpdateLanguageNameEvent(...$eventData);
  59.         $this->eventDispatcher->dispatch($beforeEvent);
  60.         if ($beforeEvent->isPropagationStopped()) {
  61.             return $beforeEvent->getUpdatedLanguage();
  62.         }
  63.         $updatedLanguage $beforeEvent->hasUpdatedLanguage()
  64.             ? $beforeEvent->getUpdatedLanguage()
  65.             : $this->innerService->updateLanguageName($language$newName);
  66.         $this->eventDispatcher->dispatch(
  67.             new UpdateLanguageNameEvent($updatedLanguage, ...$eventData)
  68.         );
  69.         return $updatedLanguage;
  70.     }
  71.     public function enableLanguage(Language $language): Language
  72.     {
  73.         $eventData = [$language];
  74.         $beforeEvent = new BeforeEnableLanguageEvent(...$eventData);
  75.         $this->eventDispatcher->dispatch($beforeEvent);
  76.         if ($beforeEvent->isPropagationStopped()) {
  77.             return $beforeEvent->getEnabledLanguage();
  78.         }
  79.         $enabledLanguage $beforeEvent->hasEnabledLanguage()
  80.             ? $beforeEvent->getEnabledLanguage()
  81.             : $this->innerService->enableLanguage($language);
  82.         $this->eventDispatcher->dispatch(
  83.             new EnableLanguageEvent($enabledLanguage, ...$eventData)
  84.         );
  85.         return $enabledLanguage;
  86.     }
  87.     public function disableLanguage(Language $language): Language
  88.     {
  89.         $eventData = [$language];
  90.         $beforeEvent = new BeforeDisableLanguageEvent(...$eventData);
  91.         $this->eventDispatcher->dispatch($beforeEvent);
  92.         if ($beforeEvent->isPropagationStopped()) {
  93.             return $beforeEvent->getDisabledLanguage();
  94.         }
  95.         $disabledLanguage $beforeEvent->hasDisabledLanguage()
  96.             ? $beforeEvent->getDisabledLanguage()
  97.             : $this->innerService->disableLanguage($language);
  98.         $this->eventDispatcher->dispatch(
  99.             new DisableLanguageEvent($disabledLanguage, ...$eventData)
  100.         );
  101.         return $disabledLanguage;
  102.     }
  103.     public function deleteLanguage(Language $language): void
  104.     {
  105.         $eventData = [$language];
  106.         $beforeEvent = new BeforeDeleteLanguageEvent(...$eventData);
  107.         $this->eventDispatcher->dispatch($beforeEvent);
  108.         if ($beforeEvent->isPropagationStopped()) {
  109.             return;
  110.         }
  111.         $this->innerService->deleteLanguage($language);
  112.         $this->eventDispatcher->dispatch(
  113.             new DeleteLanguageEvent(...$eventData)
  114.         );
  115.     }
  116. }