src/Twig/TwigExtension.php line 109

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use Twig\Extension\AbstractExtension;
  4. use Twig\TwigFunction;
  5. use App\Service\SessionManagerLMDV;
  6. use Symfony\Component\Asset\Packages;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Validator\Constraints\DateTime;
  9. /**
  10.  * Service class that declares some twig helpers in order to retrieve information in an easy way.
  11.  *
  12.  * @author jra
  13.  *
  14.  */
  15. class TwigExtension extends AbstractExtension
  16. {
  17.   /**
  18.    * @var \Symfony\Component\HttpFoundation\Request
  19.    */
  20.   protected $request;
  21.   /**
  22.    * @var \Symfony\Component\Asset\Packages
  23.    */
  24.   protected $assets;
  25.   protected $projectDir;
  26.   public function __construct(RequestStack $request_stackPackages $assets,  string $projectDir) {
  27.     $this->request $request_stack->getCurrentRequest();
  28.     $this->assets $assets;
  29.     $this->projectDir $projectDir;
  30.   }
  31.   public function getFunctions()
  32.   {
  33.       return [
  34.           new TwigFunction('LMDV_getTitle', [$this'getTitle']),
  35.           new TwigFunction('LMDV_getStartDate', [$this'getDateDepart']),
  36.           new TwigFunction('LMDV_getEndDate', [$this'getEndDate']),
  37.           new TwigFunction('LMDV_getDepartureCity', [$this'getDepartureCity']),
  38.           new TwigFunction('LMDV_getDestinationCity', [$this'getDestinationCity']),
  39.           new TwigFunction('LMDV_getTotalPrice', [$this'getTotalPrice']),
  40.           new TwigFunction('LMDV_getTelephone', [$this'getTelephone']),
  41.           new TwigFunction('LMDV_getImage', [$this'getImage']),
  42.           new TwigFunction('LMDV_getTotalDay', [$this'getTotalDay']),
  43.           new TwigFunction('LMDV_getTotalNight', [$this'getTotalNight']),
  44.           new TwigFunction('LMDV_getProcess', [$this'getProcess']),
  45.           new TwigFunction('LMDV_hasPDFVoyage', [$this'hasPDFVoyage'])
  46.       ];
  47.   }
  48.   /**
  49.    * Gets a random image from an static list. Width an height parameters are calculated
  50.    * because it allows browsers to calculate an aspect ratio and make place for the image
  51.    * in the layout *before* actually downloading the image
  52.    *
  53.    * @return array $output
  54.    *    An array with the url, width & height
  55.    */
  56.   public function getImage() {
  57.     // Static images array
  58.     $images = [
  59.       'build/images/femalehiker.jpg',
  60.     ];
  61.     $key array_rand($images1);
  62.     $filename $this->assets->getUrl($images[$key]);
  63.     list($width$height) = getimagesize($this->projectDir '/public/' $filename);
  64.     $output = [
  65.       'url' => $filename,
  66.       'width' => $width,
  67.       'height' => $height
  68.     ];
  69.     return $output;
  70.   }
  71.   public function getTitle()
  72.   {
  73.     if ($this->request->getSession()->get('TWIG_title') != '') {
  74.         return $this->request->getSession()->get('TWIG_title');
  75.     }
  76.     else {
  77.         return 'N/A';
  78.     }
  79.   }
  80.   public function getDateDepart()
  81.   {
  82.       setlocale(LC_TIME'fr_FR.UTF8''fr.UTF8');
  83.       return $this->request->getSession()->get('departureDate') ? strftime("%d %B %Y"strtotime($this->request->getSession()->get('departureDate'))) : 'N/A';
  84.   }
  85.   public function getEndDate()
  86.   {
  87.       setlocale(LC_TIME'fr_FR.UTF8''fr.UTF8');
  88.       return $this->request->getSession()->get('endDate') ? strftime("%d %B %Y"strtotime($this->request->getSession()->get('endDate'))) : 'N/A';
  89.   }
  90.   public function getDepartureCity()
  91.   {
  92.       return $this->request->getSession()->get('GETINFO_departureCity') ? $this->request->getSession()->get('GETINFO_departureCity') : ($this->request->getSession()->get('departureCity') ? $this->request->getSession()->get('departureCity') : 'N/A');
  93.   }
  94.   public function getTelephone() {
  95.     if($this->request->getSession()->get('GETINFO_supportPhone') != '') {
  96.       return $this->request->getSession()->get('GETINFO_supportPhone');
  97.     }
  98.     else {
  99.       return '';
  100.     }
  101.   }
  102.   public function getTotalPrice()
  103.   {
  104.     if ($this->request->getSession()->get('TWIG_totalPrice') != '') {
  105.       $money $this->request->getSession()->get('TWIG_totalPrice');
  106.       $str $money->getDisplayValue(TRUE);
  107.       $str str_replace('EUR''€'$str);
  108.       return $str;
  109.     }
  110.     else {
  111.       return 'N/A';
  112.     }
  113.   }
  114.   public function getTotalNight() {
  115.     $start $this->request->getSession()->get('departureDate');
  116.     $end $this->request->getSession()->get('endDate');
  117.     if($start && $end) {
  118.       $date1 = new \DateTime($start);
  119.       $date2 = new \DateTime($end);
  120.       $diff $date2->diff($date1)->format("%a");
  121.       return $diff;
  122.     }
  123.     else {
  124.       return 'N/A';
  125.     }
  126.   }
  127.   public function getTotalDay() {
  128.     $start $this->request->getSession()->get('departureDate');
  129.     $end $this->request->getSession()->get('endDate');
  130.     if($start && $end) {
  131.       $date1 = new \DateTime($start);
  132.       $date2 = new \DateTime($end);
  133.       $diff $date2->diff($date1)->format("%a");
  134.       return $diff 1;
  135.     }
  136.     else {
  137.       return 'N/A';
  138.     }
  139.   }
  140.   /**
  141.    * NOTE : This value is extracted from the URL so we introduce a dependency between
  142.    * a className and the URL structure. This should be avoided but as the PDF controller
  143.    * is shared between all user-cases-paths, this is currently the fastest way to identify
  144.    * what tunnel we are on. If URL structure changes, a mapping between the URL and the needed
  145.    * Process class name could be implemented here.
  146.    *
  147.    * After an HiPay paiement, we can not look at the URL so we trust the previously stored session value.
  148.    *
  149.    * @return string
  150.    *    The string that represents the last namespace part of the null
  151.    */
  152.   public function getProcess() {
  153.     $aux $this->request->getPathInfo();
  154.     $aux explode('/'$aux);
  155.     $process $aux[1];
  156.     if($process == 'payment') {
  157.       return $this->request->getSession()->get('TWIG_process');
  158.     }
  159.     else {
  160.       $this->request->getSession()->set('TWIG_process'$process);
  161.       return $process;
  162.     }
  163.   }
  164.   public function hasPDFVoyage() {
  165.     return $this->request->getSession()->get('hasPDFVoyage');
  166.   }
  167. }