<?php
namespace App\Service;
use App\Entity\VoyageInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Service class that encapsultes the $_SESSION management. We use a namespace in order to separate the information store
* for each LMDV form.
*
* @author jra
*
*/
class SessionManagerLMDV implements SessionManagerLMDVInterface {
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* @var \Symfony\Component\Serializer\SerializerInterface
*/
protected $serializer;
/**
* @var \Symfony\Contracts\Translation\TranslatorInterface
*/
protected $translator;
/**
* @var string
*/
protected $namespace;
public function __construct(RequestStack $request_stack, SerializerInterface $serializer, TranslatorInterface $translator) {
$this->request = $request_stack->getCurrentRequest();
$this->serializer = $serializer;
$this->translator = $translator;
}
public function setEntityNamespace(string $namespace) {
$this->namespace = $namespace;
}
public function getEntityNamespace() {
return $this->namespace;
}
protected function checkNamespace() {
if(empty($this->namespace)) {
throw new \Exception($this->translator->trans("error.session_manager.namespace"));
}
}
protected function getKey(string $etape = NULL) {
$this->checkNamespace();
$aux = str_replace('\\', '/', $this->namespace);
$prefix = \basename($aux);
$key = $prefix;
if($etape) {
$key = $prefix . '_' . $etape;
}
return $key;
}
public function setDataToSession(string $etape_name, $object) {
$this->checkNamespace();
$key = $this->getKey($etape_name);
$jsonContent = $this->serializer->serialize($object, 'json');
$this->request->getSession()->set($key, $jsonContent);
}
/**
* Gets the data saved into the $_SESSION and builds up the Voyage() object.
*
* @param string $namespace
* @return \App\Entity\VoyageInterface
*/
public function getDataFromSession() : VoyageInterface {
$this->checkNamespace();
$session = $this->request->getSession();
// Etape 0 session recover
$key = $this->getKey('etape0');
$jsonContent = $session->get($key) ? $session->get($key) : '';
$etape0 = NULL;
if($jsonContent) {
$etape0 = $this->serializer->deserialize($jsonContent, $this->namespace .'\Etape0', 'json');
}
// Etape 1 session recover
$key = $this->getKey('etape1');
$jsonContent = $session->get($key) ? $session->get($key) : '';
$etape1 = NULL;
if($jsonContent) {
$etape1 = $this->serializer->deserialize($jsonContent, $this->namespace .'\Etape1', 'json');
}
// Etape 2 session recover
$key = $this->getKey('etape2');
$jsonContent = $session->get($key) ? $session->get($key) : '';
$etape2 = NULL;
if($jsonContent) {
$etape2 = $this->serializer->deserialize($jsonContent, $this->namespace .'\Etape2', 'json');
}
// Etape 3 session recover
$key = $this->getKey('etape3');
$jsonContent = $session->get($key) ? $session->get($key) : '';
$etape3 = NULL;
if($jsonContent) {
$etape3 = $this->serializer->deserialize($jsonContent, $this->namespace .'\Etape3', 'json');
}
$class = $this->namespace .'\Voyage';
/** @var \App\Entity\VoyageInterface $voyage */
$voyage = new $class();
$voyage->setEtape0($etape0);
$voyage->setEtape1($etape1);
$voyage->setEtape2($etape2);
$voyage->setEtape3($etape3);
// Add other information saved into the session */
$voyage->setTitle($session->get('GETINFO_title'));
$voyage->setCountry($session->get('GETINFO_country'));
$voyage->setEntityNamespace($this->getEntityNamespace());
if($etape0 && !empty($session->get('GETINFO_departureCity'))) {
$etape0->setVilleDepart($session->get('GETINFO_departureCity'));
}
$voyage->setSupportPhone($session->get('GETINFO_supportPhone'));
// We add externalParameters to the $voyage too, as they are used by the WebService class
$voyage->setIdOppSF($session->get('idOppSF'));
$voyage->setIdOppSFParent($session->get('idOppSFParent'));
$voyage->setBusinessCode($session->get('businessCode'));
$voyage->setAgentCode($session->get('agentCode'));
$voyage->setProductCode($session->get('productCode'));
$voyage->setDepartureDate($session->get('departureDate'));
$voyage->setEndDate($session->get('endDate'));
$voyage->setDepartureCity($session->get('departureCity'));
$voyage->setAdults($session->get('adults'));
$voyage->setChildren($session->get('children'));
$voyage->setSessionId($session->get('sessionId'));
$voyage->setReferencePDF($session->get('reference_pdf'));
$processClass = '\App\Service\Process\\' . $this->getKey() . 'Process';
$voyage->setProcess(new $processClass());
return $voyage;
}
/**
* Saves $_GET parameters into the session. This is called only once in FrontController class
*
* @param Request $request
*/
public function setExternalParametersIntoSession(string $timestamp) {
$idOppSF = $this->request->query->get('idOppSF');
$businessCode = $this->request->query->get('businessCode');
$agentCode = $this->request->query->get('agentCode');
$productCode = $this->request->query->get('productCode');
$departureDate = $this->request->query->get('departureDate');
$endDate = $this->request->query->get('endDate');
$departureCity = $this->request->query->get('departureCity');
$adults = $this->request->query->get('adults');
$children = $this->request->query->get('children');
$session = $this->request->getSession();
$session->set('idOppSF', $idOppSF);
$session->set('businessCode', $businessCode);
$session->set('agentCode', $agentCode);
$session->set('productCode', $productCode);
$session->set('departureDate', $departureDate);
$session->set('endDate', $endDate);
$session->set('departureCity', $departureCity);
$session->set('adults', $adults);
$session->set('children', $children);
// Create a custom identifier with the sessionId + all $_GET parameters
// which will be set in FrontController and which will be validated in 'step0'.
// This allows copy/paste URLs ending in 'step0', if the custom sessionId is not set,
// we will get back to the FrontController.
$all_params = $this->request->query->all();
unset($all_params['_ts']);
$aux = [
'params' => $all_params,
'_ts' => $timestamp,
'session_id' => $session->getId()
];
$sessionId = md5(json_encode($aux));
$session->set('sessionId', $sessionId);
}
/**
* Verifies that the custom sessionId is valid. Only used in step0
*
* @param string $sessionId
* The custom sessionId already set in the Voyage object
*/
public function isValidCustomSessionId(?string $sessionId) {
$all_params = $this->request->query->all();
$ts = @$all_params['_ts'];
unset($all_params['_ts']);
$aux = [
'params' => $all_params,
'_ts' => $ts,
'session_id' => $this->request->getSession()->getId()
];
$computed = md5(json_encode($aux));
if($computed != $sessionId) {
return FALSE;
}
else {
return TRUE;
}
}
}