custom/plugins/ShopStudioPixelYourShop/src/View/TransientBag/TransientBag.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopStudio\PixelYourShop\View\TransientBag;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. /**
  5.  * @since 1.0.0
  6.  */
  7. class TransientBag implements TransientBagInterface
  8. {
  9.     /**
  10.      * @since 1.0.0
  11.      */
  12.     protected const PREFIX 'shopstudio_pixelyourshop_transient';
  13.     /**
  14.      * @since 1.0.0
  15.      */
  16.     private RequestStack $requestStack;
  17.     /**
  18.      * @since 1.0.0
  19.      */
  20.     public function __construct(RequestStack $requestStack)
  21.     {
  22.         $this->requestStack $requestStack;
  23.     }
  24.     /**
  25.      * @since 1.0.0
  26.      *
  27.      * @inheritDoc
  28.      */
  29.     public function set(Transient $transient): void
  30.     {
  31.         $this->requestStack->getSession()->set(
  32.             sprintf('%s_%s'self::PREFIX$transient->getKey()),
  33.             $transient->getData()
  34.         );
  35.     }
  36.     /**
  37.      * @since 1.0.0
  38.      *
  39.      * @inheritDoc
  40.      */
  41.     public function get(string $key): ?Transient
  42.     {
  43.         if (!$this->requestStack->getSession()->has(sprintf('%s_%s'self::PREFIX$key))) {
  44.             return null;
  45.         }
  46.         $data $this->requestStack->getSession()->remove(sprintf('%s_%s'self::PREFIX$key));
  47.         return new Transient($key, !empty($data) ? $data null);
  48.     }
  49. }