src/EventListener/WrongOneSignalUrlListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  7. use Symfony\Component\Routing\RouterInterface;
  8. class WrongOneSignalUrlListener
  9. {
  10.     private $router;
  11.     private $request;
  12.     const ACTIVE_LOCALES = array(
  13.         'ro',
  14.         'tr',
  15.         'bg',
  16.         'hu',
  17.         'it',
  18.         'de',
  19.         'en',
  20.         'ru'
  21.     );
  22.     public function __construct(RouterInterface $routerRequestStack $requestStack)
  23.     {
  24.         $this->router $router;
  25.         $this->request $requestStack->getCurrentRequest();
  26.     }
  27.     public function onKernelRequest(GetResponseEvent $event)
  28.     {
  29.         $this->switchUrl($event);
  30.     }
  31.     public function onKernelException(ExceptionEvent $event)
  32.     {
  33.         $this->switchUrl($event);
  34.     }
  35.     private function switchUrl($event)
  36.     {
  37.         if ($this->request) {
  38.             $route $this->request->get('_route');
  39.             $uri $this->request->getUri();
  40.             $currentLocale $this->request->getLocale();
  41.             if (!$currentLocale || !in_array($currentLocaleself::ACTIVE_LOCALES)) {
  42.                 $currentLocale 'ro';
  43.             }
  44.             if ($uri && strpos($uri'/OneSignalSDKWorker.js')) {
  45.                 if ($route) {
  46.                     $url $this->router->generate($route, [
  47.                         '_locale' => $currentLocale
  48.                     ]);
  49.                 } else {
  50.                     $url str_replace('OneSignalSDKWorker.js'$currentLocale$uri);
  51.                 }
  52.                 $response = new RedirectResponse($url);
  53.                 $event->setResponse($response);
  54.             }
  55.         }
  56.     }
  57. }