<?php
declare(strict_types=1);
namespace Bluue\StocksBundle\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
*/
protected 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('stocks_bundle__display_all_contexts');
$shared_products = $this->configService->get('products_bundle__shared_products');
if ($all_contexts && $shared_products) {
$routes = [
'stocks_bundle__stock_product_list',
'stocks_bundle__stock_movement_list',
'stocks_bundle__location_list',
'stocks_bundle__sub_location_list',
'stocks_bundle__stock_location_limit_date_list',
'stocks_bundle__stock_preparation_list',
'stocks_bundle__mass_stock_entry_list',
'stocks_bundle__mass_stock_removal_list',
'stocks_bundle__product_restock_list',
'stocks_bundle__product_restock_holding_list',
'stocks_bundle__product_quantity_to_hold_list',
'forecasted_stock_bundle__list'
];
foreach ($routes as $route) {
$currentRoute = $this->requestStack->getMainRequest()->attributes->get('_route');
if ($currentRoute === $route) {
$event->setAllContexts(true);
break;
}
}
}
}
}