src/Service/SessionManagerLMDV.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\VoyageInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\Serializer\SerializerInterface;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. /**
  9.  * Service class that encapsultes the $_SESSION management. We use a namespace in order to separate the information store
  10.  * for each LMDV form.
  11.  *
  12.  * @author jra
  13.  *
  14.  */
  15. class SessionManagerLMDV implements SessionManagerLMDVInterface {
  16.   /**
  17.    * @var \Symfony\Component\HttpFoundation\Request
  18.    */
  19.   protected $request;
  20.   /**
  21.    * @var \Symfony\Component\Serializer\SerializerInterface
  22.    */
  23.   protected $serializer;
  24.   /**
  25.    * @var \Symfony\Contracts\Translation\TranslatorInterface
  26.    */
  27.   protected $translator;
  28.   /**
  29.    * @var string
  30.    */
  31.   protected $namespace;
  32.   public function __construct(RequestStack $request_stackSerializerInterface $serializerTranslatorInterface $translator) {
  33.     $this->request =  $request_stack->getCurrentRequest();
  34.     $this->serializer $serializer;
  35.     $this->translator $translator;
  36.   }
  37.   public function setEntityNamespace(string $namespace) {
  38.     $this->namespace $namespace;
  39.   }
  40.   public function getEntityNamespace() {
  41.     return $this->namespace;
  42.   }
  43.   protected function checkNamespace() {
  44.     if(empty($this->namespace)) {
  45.       throw new \Exception($this->translator->trans("error.session_manager.namespace"));
  46.     }
  47.   }
  48.   protected function getKey(string $etape NULL) {
  49.     $this->checkNamespace();
  50.     $aux str_replace('\\''/'$this->namespace);
  51.     $prefix = \basename($aux);
  52.     $key $prefix;
  53.     if($etape) {
  54.       $key $prefix '_' $etape;
  55.     }
  56.     return $key;
  57.   }
  58.   public function setDataToSession(string $etape_name$object) {
  59.     $this->checkNamespace();
  60.     $key $this->getKey($etape_name);
  61.     $jsonContent $this->serializer->serialize($object'json');
  62.     $this->request->getSession()->set($key$jsonContent);
  63.   }
  64.  /**
  65.    * Gets the data saved into the $_SESSION and builds up the Voyage() object.
  66.    *
  67.    * @param string $namespace
  68.    * @return \App\Entity\VoyageInterface
  69.    */
  70.   public function getDataFromSession() : VoyageInterface {
  71.     $this->checkNamespace();
  72.     $session $this->request->getSession();
  73.     // Etape 0 session recover
  74.     $key $this->getKey('etape0');
  75.     $jsonContent $session->get($key) ? $session->get($key) : '';
  76.     $etape0 NULL;
  77.     if($jsonContent) {
  78.       $etape0 $this->serializer->deserialize($jsonContent$this->namespace .'\Etape0''json');
  79.     }
  80.     // Etape 1 session recover
  81.     $key $this->getKey('etape1');
  82.     $jsonContent $session->get($key) ? $session->get($key) : '';
  83.     $etape1 NULL;
  84.     if($jsonContent) {
  85.       $etape1 $this->serializer->deserialize($jsonContent$this->namespace .'\Etape1''json');
  86.     }
  87.     // Etape 2 session recover
  88.     $key $this->getKey('etape2');
  89.     $jsonContent $session->get($key) ? $session->get($key) : '';
  90.     $etape2 NULL;
  91.     if($jsonContent) {
  92.       $etape2 $this->serializer->deserialize($jsonContent$this->namespace .'\Etape2''json');
  93.     }
  94.     // Etape 3 session recover
  95.     $key $this->getKey('etape3');
  96.     $jsonContent $session->get($key) ? $session->get($key) : '';
  97.     $etape3 NULL;
  98.     if($jsonContent) {
  99.       $etape3 $this->serializer->deserialize($jsonContent$this->namespace .'\Etape3''json');
  100.     }
  101.     $class $this->namespace .'\Voyage';
  102.     /** @var \App\Entity\VoyageInterface $voyage */
  103.     $voyage = new $class();
  104.     $voyage->setEtape0($etape0);
  105.     $voyage->setEtape1($etape1);
  106.     $voyage->setEtape2($etape2);
  107.     $voyage->setEtape3($etape3);
  108.     // Add other information saved into the session */
  109.     $voyage->setTitle($session->get('GETINFO_title'));
  110.     $voyage->setCountry($session->get('GETINFO_country'));
  111.     $voyage->setEntityNamespace($this->getEntityNamespace());
  112.     if($etape0 && !empty($session->get('GETINFO_departureCity'))) {
  113.       $etape0->setVilleDepart($session->get('GETINFO_departureCity'));
  114.     }
  115.     $voyage->setSupportPhone($session->get('GETINFO_supportPhone'));
  116.     // We add externalParameters to the $voyage too, as they are used by the WebService class
  117.     $voyage->setIdOppSF($session->get('idOppSF'));
  118.     $voyage->setIdOppSFParent($session->get('idOppSFParent'));
  119.     $voyage->setBusinessCode($session->get('businessCode'));
  120.     $voyage->setAgentCode($session->get('agentCode'));
  121.     $voyage->setProductCode($session->get('productCode'));
  122.     $voyage->setDepartureDate($session->get('departureDate'));
  123.     $voyage->setEndDate($session->get('endDate'));
  124.     $voyage->setDepartureCity($session->get('departureCity'));
  125.     $voyage->setAdults($session->get('adults'));
  126.     $voyage->setChildren($session->get('children'));
  127.     $voyage->setSessionId($session->get('sessionId'));
  128.     $voyage->setReferencePDF($session->get('reference_pdf'));
  129.     $processClass '\App\Service\Process\\' $this->getKey() . 'Process';
  130.     $voyage->setProcess(new $processClass());
  131.     return $voyage;
  132.   }
  133.   /**
  134.    * Saves $_GET parameters into the session. This is called only once in FrontController class
  135.    *
  136.    * @param Request $request
  137.    */
  138.   public function setExternalParametersIntoSession(string $timestamp) {
  139.     $idOppSF $this->request->query->get('idOppSF');
  140.     $businessCode $this->request->query->get('businessCode');
  141.     $agentCode $this->request->query->get('agentCode');
  142.     $productCode $this->request->query->get('productCode');
  143.     $departureDate $this->request->query->get('departureDate');
  144.     $endDate $this->request->query->get('endDate');
  145.     $departureCity $this->request->query->get('departureCity');
  146.     $adults $this->request->query->get('adults');
  147.     $children $this->request->query->get('children');
  148.     $session $this->request->getSession();
  149.     $session->set('idOppSF'$idOppSF);
  150.     $session->set('businessCode'$businessCode);
  151.     $session->set('agentCode'$agentCode);
  152.     $session->set('productCode'$productCode);
  153.     $session->set('departureDate'$departureDate);
  154.     $session->set('endDate'$endDate);
  155.     $session->set('departureCity'$departureCity);
  156.     $session->set('adults'$adults);
  157.     $session->set('children'$children);
  158.     // Create a custom identifier with the sessionId + all $_GET parameters
  159.     // which will be set in FrontController and which will be validated in 'step0'.
  160.     // This allows copy/paste URLs ending in 'step0', if the custom sessionId is not set,
  161.     // we will get back to the FrontController.
  162.     $all_params $this->request->query->all();
  163.     unset($all_params['_ts']);
  164.     $aux = [
  165.       'params' => $all_params,
  166.       '_ts' => $timestamp,
  167.       'session_id' => $session->getId()
  168.     ];
  169.     $sessionId md5(json_encode($aux));
  170.     $session->set('sessionId'$sessionId);
  171.   }
  172.   /**
  173.    * Verifies that the custom sessionId is valid. Only used in step0
  174.    *
  175.    * @param string $sessionId
  176.    *    The custom sessionId already set in the Voyage object
  177.    */
  178.   public function isValidCustomSessionId(?string $sessionId) {
  179.     $all_params $this->request->query->all();
  180.     $ts = @$all_params['_ts'];
  181.     unset($all_params['_ts']);
  182.     $aux = [
  183.       'params' => $all_params,
  184.       '_ts' => $ts,
  185.       'session_id' => $this->request->getSession()->getId()
  186.     ];
  187.     $computed md5(json_encode($aux));
  188.     if($computed != $sessionId) {
  189.       return FALSE;
  190.     }
  191.     else {
  192.       return TRUE;
  193.     }
  194.   }
  195. }