vendor/sylius/resource-bundle/src/Bundle/Storage/SessionStorage.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Storage;
  12. use Sylius\Component\Resource\Storage\StorageInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. final class SessionStorage implements StorageInterface
  16. {
  17.     /** @var RequestStack|SessionInterface */
  18.     private $requestStack;
  19.     /**
  20.      * @param RequestStack|SessionInterface $requestStack
  21.      */
  22.     public function __construct(/* RequestStack */ $requestStack)
  23.     {
  24.         /** @phpstan-ignore-next-line */
  25.         if (!$requestStack instanceof SessionInterface && !$requestStack instanceof RequestStack) {
  26.             throw new \InvalidArgumentException(sprintf('The first argument of "%s" should be instance of "%s" or "%s"'__METHOD__SessionInterface::class, RequestStack::class));
  27.         }
  28.         if ($requestStack instanceof SessionInterface) {
  29.             trigger_deprecation(
  30.                 'sylius/resource-bundle',
  31.                 '1.10',
  32.                 'Passing an instance of "%s" as the constructor argument for "%s" is deprecated and will not be supported in 2.0. Pass an instance of "%s" instead.',
  33.                 SessionInterface::class,
  34.                 self::class,
  35.                 RequestStack::class,
  36.             );
  37.         }
  38.         $this->requestStack $requestStack;
  39.     }
  40.     public function has(string $name): bool
  41.     {
  42.         return $this->getSession()->has($name);
  43.     }
  44.     public function get(string $name$default null)
  45.     {
  46.         return $this->getSession()->get($name$default);
  47.     }
  48.     public function set(string $name$value): void
  49.     {
  50.         $this->getSession()->set($name$value);
  51.     }
  52.     public function remove(string $name): void
  53.     {
  54.         $this->getSession()->remove($name);
  55.     }
  56.     public function all(): array
  57.     {
  58.         return $this->getSession()->all();
  59.     }
  60.     private function getSession(): SessionInterface
  61.     {
  62.         if ($this->requestStack instanceof SessionInterface) {
  63.             return $this->requestStack;
  64.         }
  65.         /** @phpstan-ignore-next-line */
  66.         return $this->requestStack->getSession();
  67.     }
  68. }