<?php declare(strict_types=1);
namespace Samson\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\Framework\Struct\StructCollection;
use Shopware\Core\System\SalesChannel\Aggregate\SalesChannelDomain\SalesChannelDomainEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouterInterface;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2022
*
***/
class PageSubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private EntityRepositoryInterface $salesChannelDomainRepository;
private RouterInterface $router;
public function __construct(SystemConfigService $systemConfigService,
EntityRepositoryInterface $salesChannelDomainRepository,
RouterInterface $router
)
{
$this->systemConfigService = $systemConfigService;
$this->salesChannelDomainRepository = $salesChannelDomainRepository;
$this->router = $router;
}
/** {@inheritDoc} */
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onPageLoaded'
];
}
public function onPageLoaded(GenericPageLoadedEvent $event)
{
$keys = ['first', 'second', 'third'];
$result = [];
foreach ($keys as $key) {
$result[] = $this->loadConfig($key, $event->getSalesChannelContext());
}
$event->getPage()->addExtension('salesChannels', new StructCollection(array_filter($result)));
}
private function loadConfig(string $key, SalesChannelContext $salesChannelContext): ?ArrayStruct
{
$context = $salesChannelContext->getContext();
$salesChannelId = $salesChannelContext->getSalesChannelId();
$id = $this->systemConfigService->get("SamsonCustomer.config.{$key}SalesChannel", $salesChannelId);
$route = $this->systemConfigService->get("SamsonCustomer.config.{$key}SalesChannelRouteName", $salesChannelId);
if (!$id) {
return null;
}
$url = $this->generateUrl($id, $route, $context);
$color = $this->systemConfigService->get('SamsonCustomer.config.samsonPrimaryColor', $id);
$contrastColor = $this->systemConfigService->get('SamsonCustomer.config.samsonPrimaryContrastColor', $id);
if (!$url || !$color) {
return null;
}
return new ArrayStruct([
'id' => $id,
'color' => $color,
'contrastColor' => $contrastColor,
'label' => "samson.sales-channel.{$key}-sales-channel",
'url' => $url,
]);
}
private function generateUrl(string $salesChannelId, ?string $route, Context $context): ?string
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('salesChannelId', $salesChannelId))
->addFilter(new EqualsFilter('languageId', $context->getLanguageId()))
;
/* @var SalesChannelDomainEntity $salesChannelDomain */
$salesChannelDomain = $this->salesChannelDomainRepository->search($criteria, $context)->first();
if (!$salesChannelDomain) {
return null;
}
$context = $this->router->getContext();
$oldBaseUrl = $context->getBaseUrl();
$newBaseUrl = $salesChannelDomain->getUrl();
$route = $route ?? 'frontend.home.page';
$context->setBaseUrl($newBaseUrl);
$url = $this->router->generate($route);
$context->setBaseUrl($oldBaseUrl);
return $url;
}
}