<?php
namespace App\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RouterInterface;
class WrongOneSignalUrlListener
{
private $router;
private $request;
const ACTIVE_LOCALES = array(
'ro',
'tr',
'bg',
'hu',
'it',
'de',
'en',
'ru'
);
public function __construct(RouterInterface $router, RequestStack $requestStack)
{
$this->router = $router;
$this->request = $requestStack->getCurrentRequest();
}
public function onKernelRequest(GetResponseEvent $event)
{
$this->switchUrl($event);
}
public function onKernelException(ExceptionEvent $event)
{
$this->switchUrl($event);
}
private function switchUrl($event)
{
if ($this->request) {
$route = $this->request->get('_route');
$uri = $this->request->getUri();
$currentLocale = $this->request->getLocale();
if (!$currentLocale || !in_array($currentLocale, self::ACTIVE_LOCALES)) {
$currentLocale = 'ro';
}
if ($uri && strpos($uri, '/OneSignalSDKWorker.js')) {
if ($route) {
$url = $this->router->generate($route, [
'_locale' => $currentLocale
]);
} else {
$url = str_replace('OneSignalSDKWorker.js', $currentLocale, $uri);
}
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
}
}