vendor/terminal42/contao-mp_forms/src/Storage/SessionStorage.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Terminal42\MultipageFormsBundle\Storage;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Terminal42\MultipageFormsBundle\Step\StepDataCollection;
  7. class SessionStorage implements StorageInterface
  8. {
  9.     public const SESSION_KEY 'contao.mp_forms';
  10.     public function __construct(private Request $request)
  11.     {
  12.     }
  13.     public function storeData(string $storageIdentifierStepDataCollection $stepDataCollection): void
  14.     {
  15.         $this->writeToSession($storageIdentifier$stepDataCollection);
  16.     }
  17.     public function getData(string $storageIdentifier): StepDataCollection
  18.     {
  19.         return $this->readFromSession($storageIdentifier);
  20.     }
  21.     private function writeToSession(string $storageIdentifierStepDataCollection $collection): void
  22.     {
  23.         if (null === ($session $this->getSession())) {
  24.             return;
  25.         }
  26.         $session->set($this->getSessionKey($storageIdentifier), $collection);
  27.     }
  28.     private function readFromSession(string $storageIdentifierbool $checkPrevious false): StepDataCollection
  29.     {
  30.         $empty = new StepDataCollection();
  31.         if (null === ($session $this->getSession($checkPrevious))) {
  32.             return $empty;
  33.         }
  34.         return $session->get($this->getSessionKey($storageIdentifier), $empty);
  35.     }
  36.     private function getSessionKey(string $storageIdentifier): string
  37.     {
  38.         return self::SESSION_KEY.'.'.$storageIdentifier;
  39.     }
  40.     private function getSession(bool $checkPrevious false): SessionInterface|null
  41.     {
  42.         if ($checkPrevious && !$this->request->hasPreviousSession()) {
  43.             return null;
  44.         }
  45.         if (!$this->request->hasSession()) {
  46.             return null;
  47.         }
  48.         return $this->request->getSession();
  49.     }
  50. }