src/Service/MaintenanceListener.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Yaml\Yaml;
  8. class MaintenanceListener
  9. {
  10.     // todo: make configurable
  11.     const PORTAL_MAINTENANCE_MODE false;
  12.     /** @var \Symfony\Component\DependencyInjection\ContainerInterface */
  13.     protected $container;
  14.     /** @var \Twig_Environment */
  15.     protected $twig;
  16.     /** @var bool */
  17.     protected $isActive false;
  18.     /** @var string */
  19.     protected $title;
  20.     /** @var string */
  21.     protected $message;
  22.     /** @var TokenStorageInterface */
  23.     protected $tokenStorage;
  24.     const PROTECTED_ROUTES = [
  25.         'anfrage',
  26.         'client_details',
  27.         'tan_verification',
  28.         'payment_details',
  29.         'execute_payment_paypal',
  30.         'execute_payment_sofort',
  31.         'cancel_payment',
  32.         'payment_success',
  33.     ];
  34.     public function __construct(ContainerInterface $container, \Twig_Environment $twigTokenStorageInterface $tokenStorage)
  35.     {
  36.         $this->container $container;
  37.         $this->twig      $twig;
  38.         $appSettings Yaml::parseFile(__DIR__ '/../../config/app_settings.yaml');
  39.         if ( ! empty($appSettings) && isset($appSettings['maintenance'])) {
  40.             $maintenanceSettings $appSettings['maintenance'];
  41.             $this->isActive      $maintenanceSettings['is_active'] ?: false;
  42.             $this->title         $maintenanceSettings['title'];
  43.             $this->message       $maintenanceSettings['message']['custom'] ?: $maintenanceSettings['message']['default'];
  44.         }
  45.         $this->tokenStorage $tokenStorage;
  46.     }
  47.     public function onKernelRequest(GetResponseEvent $event)
  48.     {
  49.         // If maintenance is active
  50.         if ($this->isActive || static::PORTAL_MAINTENANCE_MODE) {
  51.             $token $this->tokenStorage->getToken();
  52.             if ($token !== null) {
  53.                 /** @var \Symfony\Component\Security\Core\User\UserInterface $currentUser */
  54.                 $currentUser $this->tokenStorage->getToken()->getUser();
  55.                 if ( ! is_string($currentUser) && ! in_array('ROLE_ADMIN'$currentUser->getRoles())) {
  56.                     // logout user if not admin
  57.                     $this->container->get('security.token_storage')->setToken(null);
  58.                     $this->container->get('session')->invalidate();
  59.                 }
  60.             }
  61.             $route $event->getRequest()->get('_route');
  62.             if (($this->isActive && in_array($route, static::PROTECTED_ROUTES))
  63.                 || strpos($route'portal') === 0
  64.                 || strpos($route'customer') === 0
  65.             ) {
  66.                 // We load our maintenance template
  67.                 $template $this->twig->render('maintenance.html.twig', [
  68.                     'title'   => $this->title,
  69.                     'message' => $this->message,
  70.                 ]);
  71.                 // We send our response with a 503 response code (service unavailable)
  72.                 $event->setResponse(new Response($template503));
  73.                 $event->stopPropagation();
  74.             }
  75.         }
  76.     }
  77. }