<?php
declare(strict_types=1);
namespace Bluue\SalesBundle\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('sales_bundle__display_all_contexts');
$shared_products = $this->configService->get('sales_bundle__shared_sales');
if ($all_contexts && $shared_products) {
$routes = [
'sales_bundle__quotation_list',
'sales_bundle__order_list',
'sales_bundle__delivery_note_list',
'sales_bundle__invoice_list',
'sales_bundle__credit_note_list',
'sales_bundle__payment_list',
'recurring_invoices_bundle__recurring_invoice_list',
];
foreach ($routes as $route) {
$currentRoute = $this->requestStack->getMainRequest()->attributes->get('_route');
if ($currentRoute === $route) {
$event->setAllContexts(true);
break;
}
}
}
}
}