vendor/php-translation/symfony-bundle/EditInPlace/Activator.php line 89

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the PHP Translation package.
  4.  *
  5.  * (c) PHP Translation team <tobias.nyholm@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Translation\Bundle\EditInPlace;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. /**
  15.  * Default Activator implementation.
  16.  *
  17.  * @author Damien Alexandre <dalexandre@jolicode.com>
  18.  */
  19. final class Activator implements ActivatorInterface
  20. {
  21.     public const KEY 'translation_bundle.edit_in_place.enabled';
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     private $requestStack;
  26.     /**
  27.      * @var Session|null
  28.      */
  29.     private $session null;
  30.     public function __construct(RequestStack $requestStack)
  31.     {
  32.         $this->requestStack $requestStack;
  33.     }
  34.     /**
  35.      * Set session if available.
  36.      */
  37.     public function setSession(Session $session): void
  38.     {
  39.         $this->session $session;
  40.     }
  41.     /**
  42.      * Get session based on availability.
  43.      */
  44.     private function getSession(): ?Session
  45.     {
  46.         $session $this->session;
  47.         $request $this->requestStack->getCurrentRequest();
  48.         if (null === $session && $request && $request->hasSession()) {
  49.             $session $this->requestStack->getSession();
  50.         }
  51.         return $session;
  52.     }
  53.     /**
  54.      * Enable the Edit In Place mode.
  55.      */
  56.     public function activate(): void
  57.     {
  58.         if (null !== $this->getSession()) {
  59.             $this->getSession()->set(self::KEYtrue);
  60.         }
  61.     }
  62.     /**
  63.      * Disable the Edit In Place mode.
  64.      */
  65.     public function deactivate(): void
  66.     {
  67.         if (null !== $this->getSession()) {
  68.             $this->getSession()->remove(self::KEY);
  69.         }
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function checkRequest(Request $request null): bool
  75.     {
  76.         if (null === $this->getSession() || !$this->getSession()->has(self::KEY)) {
  77.             return false;
  78.         }
  79.         return $this->getSession()->get(self::KEYfalse);
  80.     }
  81. }