vendor/craue/formflow-bundle/Storage/SessionProviderTrait.php line 55

Open in your IDE?
  1. <?php
  2. namespace Craue\FormFlowBundle\Storage;
  3. use Craue\FormFlowBundle\Exception\InvalidTypeException;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. /**
  7.  * @internal
  8.  *
  9.  * @author Christian Raue <christian.raue@gmail.com>
  10.  * @copyright 2011-2021 Christian Raue
  11.  * @license http://opensource.org/licenses/mit-license.php MIT License
  12.  */
  13. trait SessionProviderTrait {
  14.     /**
  15.      * @var RequestStack|null
  16.      */
  17.     private $requestStack;
  18.     /**
  19.      * @var SessionInterface|null
  20.      */
  21.     private $session;
  22.     /**
  23.      * @param RequestStack|SessionInterface $requestStackOrSession
  24.      * @throws InvalidTypeException
  25.      */
  26.     private function setRequestStackOrSession($requestStackOrSession) : void {
  27.         // TODO accept only RequestStack as soon as Symfony >= 6.0 is required
  28.         if ($requestStackOrSession instanceof SessionInterface) {
  29.             $this->session $requestStackOrSession;
  30.             return;
  31.         }
  32.         // TODO remove as soon as Symfony >= 5.3 is required
  33.         if (!\method_exists(RequestStack::class, 'getSession')) {
  34.             throw new InvalidTypeException($requestStackOrSessionSessionInterface::class);
  35.         }
  36.         if ($requestStackOrSession instanceof RequestStack) {
  37.             $this->requestStack $requestStackOrSession;
  38.             return;
  39.         }
  40.         throw new InvalidTypeException($requestStackOrSession, [RequestStack::class, SessionInterface::class]);
  41.     }
  42.     private function getSession() : SessionInterface {
  43.         if ($this->requestStack !== null) {
  44.             return $this->requestStack->getSession();
  45.         }
  46.         return $this->session;
  47.     }
  48. }