<?php
namespace Harmonizely\Service\CustomTranslation\EventListener;
use Exception;
use Harmonizely\Repository\Company\Contract\IOrganizationRepository;
use Harmonizely\Repository\Company\Contract\IUserRepository;
use Harmonizely\Service\CustomTranslation\Contract\IGetCustomTranslationCatalogService;
use Harmonizely\Service\CustomTranslation\Event\SetCustomTranslationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Contracts\Translation\TranslatorInterface;
class SetCustomTranslationListener implements EventSubscriberInterface
{
/**
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* @var IGetCustomTranslationCatalogService
*/
private IGetCustomTranslationCatalogService $getCustomTranslationCatalogService;
/**
* @var array
*/
private array $messageCatalogueList = [];
/**
* @var IUserRepository
*/
private IUserRepository $userRepository;
/**
* @var IOrganizationRepository
*/
private IOrganizationRepository $organizationRepository;
/**
* @param TranslatorInterface $translator
* @param IGetCustomTranslationCatalogService $getCustomTranslationCatalogService
* @param IUserRepository $userRepository
* @param IOrganizationRepository $organizationRepository
*/
public function __construct(
TranslatorInterface $translator,
IGetCustomTranslationCatalogService $getCustomTranslationCatalogService,
IUserRepository $userRepository,
IOrganizationRepository $organizationRepository
)
{
$this->translator = $translator;
$this->getCustomTranslationCatalogService = $getCustomTranslationCatalogService;
$this->userRepository = $userRepository;
$this->organizationRepository = $organizationRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
SetCustomTranslationEvent::NAME => 'onSetCustomTranslation',
];
}
/**
* @param SetCustomTranslationEvent $event
* @throws Exception
*/
public function onSetCustomTranslation(SetCustomTranslationEvent $event): void
{
$customTranslationCatalog = $this->getCustomTranslationCatalogService->perform(
$event->getUserId(),
$event->getOrganizationId()
);
if ($event->getUserId()) {
$user = $this->userRepository->find($event->getUserId());
$accountLocale = $user->getLocale();
} elseif ($event->getOrganizationId()) {
$organization = $this->organizationRepository->find($event->getOrganizationId());
$accountLocale = $organization->getLocale();
} else {
$accountLocale = $this->translator->getLocale();
}
/** @var MessageCatalogue $catalogue */
$catalogue = $this->translator->getCatalogue();
$autoCatalogue = $this->translator->getCatalogue($accountLocale);
$domains = $catalogue->getDomains();
$this->setDefaultCatalog($catalogue, $this->translator->getLocale());
$this->setDefaultCatalog($autoCatalogue, $accountLocale);
foreach ($customTranslationCatalog->getEntries() as $entry) {
foreach ($domains as $domain) {
$catalogue->set($entry->getMsgId(), $entry->getMsgStr(), $domain);
$autoCatalogue->set($entry->getMsgId(), $entry->getMsgStr(), $domain);
}
}
}
/**
* @param MessageCatalogue $catalogue
* @param string $locale
* @return void
*/
public function setDefaultCatalog(MessageCatalogue $catalogue, string $locale): void
{
if (!isset($this->messageCatalogueList[$locale])) {
$this->messageCatalogueList[$locale] = new MessageCatalogue($locale, $catalogue->all());
} else {
$defaultCatalogue = $this->messageCatalogueList[$locale];
foreach ($defaultCatalogue->all() as $domain => $messages) {
$catalogue->replace($messages, $domain);
}
}
}
}