vendor/php-flasher/flasher-symfony/Storage/SessionBag.php line 37

  1. <?php
  2. /*
  3.  * This file is part of the PHPFlasher package.
  4.  * (c) Younes KHOUBZA <younes.khoubza@gmail.com>
  5.  */
  6. namespace Flasher\Symfony\Storage;
  7. use Flasher\Prime\Storage\Bag\BagInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Session as LegacySession;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. final class SessionBag implements BagInterface
  12. {
  13.     const ENVELOPES_NAMESPACE 'flasher::envelopes';
  14.     /**
  15.      * @var RequestStack|SessionInterface
  16.      */
  17.     private $session;
  18.     /**
  19.      * @param RequestStack|SessionInterface $session
  20.      */
  21.     public function __construct($session)
  22.     {
  23.         $this->session $session;
  24.     }
  25.     /**
  26.      * {@inheritdoc}
  27.      */
  28.     public function get()
  29.     {
  30.         return $this->session()->get(self::ENVELOPES_NAMESPACE, array()); // @phpstan-ignore-line
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public function set(array $envelopes)
  36.     {
  37.         $this->session()->set(self::ENVELOPES_NAMESPACE$envelopes);
  38.     }
  39.     /**
  40.      * @return SessionInterface
  41.      */
  42.     private function session()
  43.     {
  44.         if ($this->session instanceof SessionInterface || $this->session instanceof LegacySession) { // @phpstan-ignore-line
  45.             return $this->session// @phpstan-ignore-line
  46.         }
  47.         if (method_exists($this->session'getSession')) {
  48.             return $this->session $this->session->getSession();
  49.         }
  50.         return $this->session $this->session->getCurrentRequest()->getSession(); // @phpstan-ignore-line
  51.     }
  52. }