<?php
namespace Lnb\Shopware6\LnbThemeDefault\Subscriber;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigSubscriber implements EventSubscriberInterface
{
private const RESOLVE_LANGUAGE_CONFIG = [
//key in frontend extension => key in system config
'showNotificationBar' => 'notificationBarLanguageIds'
];
public function __construct(
private SystemConfigService $systemConfigService
)
{
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
];
}
public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
{
$languageId = $event->getSalesChannelContext()->getContext()->getLanguageId();
$config = [];
foreach (self::RESOLVE_LANGUAGE_CONFIG as $key => $configKey) {
$availableLanguages = $this->loadArrayConfig($configKey);
if ($availableLanguages === null) {
$config[$key] = true;
continue;
}
if (!in_array($languageId, $availableLanguages)) {
$config[$key] = false;
continue;
}
$config[$key] = true;
}
$event->getPagelet()->addExtension('lnb_theme_default_language_configs', new ArrayStruct($config));
}
private function loadArrayConfig(string $key): ?array
{
$config = $this->systemConfigService->get($this->buildConfigKey($key));
return is_array($config) ? $config : null;
}
private function buildConfigKey(string $key): string
{
return sprintf('LnbThemeDefault.config.%s', $key);
}
}