<?php
declare(strict_types=1);
namespace Bluue\ProductsBundle\EventSubscriber;
use App\Event\SharedContextEvent;
use App\Services\Configuration;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SharedContextSubscriber implements EventSubscriberInterface
{
/**
* @var Configuration
*/
private Configuration $configService;
/**
* @var RequestStack
*/
private RequestStack $requestStack;
/**
* @param Configuration $configService
* @param RequestStack $requestStack
*/
public function __construct(Configuration $configService, RequestStack $requestStack)
{
$this->configService = $configService;
$this->requestStack = $requestStack;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
SharedContextEvent::NAME => 'onLoad'
];
}
/**
* @param SharedContextEvent $event
* @return void
*/
public function onLoad(SharedContextEvent $event): void
{
$all_contexts = $this->configService->get('products_bundle__display_all_contexts');
$shared_products = $this->configService->get('products_bundle__shared_products');
if ($all_contexts && $shared_products) {
$routes = [
'products_bundle__product_list',
'products_bundle__brand_list',
'products_bundle__attribute_group_list',
'products_bundle__attribute_list',
'products_bundle__feature_list',
'products_bundle__feature_value_list'
];
foreach ($routes as $route) {
$currentRoute = $this->requestStack->getMainRequest()->attributes->get('_route');
if ($currentRoute === $route) {
$event->setAllContexts(true);
break;
}
}
}
}
}