<?php
namespace App\EventListener;
use App\Entity\Cart;
use App\Entity\Language;
use App\Entity\WebGroup;
use App\Tools\LocaleHelper;
use App\Tools\WebGroupHelper;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Twig\Environment as Twig;
class RequestListener
{
/**
* @var Twig
*/
private $twig;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var WebGroupHelper
*/
private $webGroupHelper;
/**
* @var LocaleHelper
*/
private $localeHelper;
/**
* @var string
*/
private $fallbackLocale;
/**
* RequestListener constructor.
*
* @param Twig $twig
* @param EntityManagerInterface $entityManager
* @param WebGroupHelper $webGroupHelper
* @param LocaleHelper $localeHelper
* @param ContainerInterface $container
*/
public function __construct(
Twig $twig,
EntityManagerInterface $entityManager,
WebGroupHelper $webGroupHelper,
LocaleHelper $localeHelper,
ContainerInterface $container
)
{
$this->twig = $twig;
$this->entityManager = $entityManager;
$this->webGroupHelper = $webGroupHelper;
$this->localeHelper = $localeHelper;
$this->fallbackLocale = $container->getParameter('default_locale');
}
/**
* @param GetResponseEvent $event
* @throws Exception
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$exception = $request->attributes->get('exception');
// Skip sub requests but keep error pages
if (!$event->isMasterRequest() && empty($exception)) {
return;
}
$attributes = $request->attributes;
// Skip ajax requests
if (
$request->isXmlHttpRequest() &&
!in_array($attributes->get('_route'), ['app_navigation_mega_menu_ajax', 'app_navigation_mobile_ajax', 'app_search']) &&
!in_array($attributes->get('_route'), ['admin_groups_load', 'admin_groups_edit'])
) {
return;
}
$locales = $this->localeHelper->getLocales();
foreach ($languageMapping = $this->localeHelper->getLanguageMapping() as $locale => $language) {
if (!in_array($locale, $locales)) {
unset($languageMapping[$locale]);
}
}
$this->twig->addGlobal('languages', $languageMapping);
$locale = $event->getRequest()->getLocale();
$language = $this->entityManager->getRepository(Language::class)->findOneByLocale($locale);
if (empty($language)) {
$language = $this->entityManager->getRepository(Language::class)->findOneByLocale($this->fallbackLocale);
//throw new RuntimeException('Cannot find matching language');
}
$this->twig->addGlobal('language', $language);
$fallbackLanguage = $this->entityManager->getRepository(Language::class)->findOneByLocale($this->fallbackLocale);
if (empty($fallbackLanguage)) {
throw new RuntimeException('Cannot find matching language');
}
$this->twig->addGlobal('fallbackLanguage', $fallbackLanguage);
// $translations = $this->entityManager->getRepository(WebGroupTranslation::class)->findByLanguageIndexByGroupId($language);
$webGroups = $this->entityManager->getRepository(WebGroup::class)->findByParent(null);
// if ($attributes->get('_route') === 'app_article_listing') {
// $webGroups = $this->webGroupHelper->extendArticleParameters($webGroups);
// }
$this->webGroupHelper->translateGroups($webGroups, $language, $fallbackLanguage);
// add webGroups as global variable for the navigation
$this->twig->addGlobal('webGroups', $webGroups);
$sessionCart = $event->getRequest()->getSession()->get('cart');
if (!empty($sessionCart)) {
$sessionCart = $this->entityManager->getRepository(Cart::class)->findOneById($sessionCart->getId());
$event->getRequest()->getSession()->set('cart', $sessionCart);
}
}
}