src/Service/ToastService.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Utils\Toast;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. class ToastService {
  6.     /**
  7.      * @var RequestStack
  8.      */
  9.     private $requestStack;
  10.     public function __construct(RequestStack $requestStack)
  11.     {
  12.         $this->requestStack $requestStack;
  13.     }
  14.     
  15.     /**
  16.      * Add toast to session
  17.      */
  18.     public function toast($type$title$message '') {
  19.         //Toast creation
  20.         $toast = new Toast($type$title$message);
  21.         //Add toast in session
  22.         $toastSession $this->requestStack->getSession()->get('core_toast', []);
  23.         if(!is_array($toastSession)) $toastSession = Array();
  24.         array_push($toastSession$toast);
  25.         $this->requestStack->getSession()->set('core_toast'$toastSession);
  26.     }
  27.     /**
  28.      * Return toasts and clean session
  29.      */
  30.     public function getToats() {
  31.         $toasts $this->requestStack->getSession()->get('core_toast', []);
  32.         //Clean session
  33.         $this->requestStack->getSession()->set('core_toast', []);
  34.         return $toasts;
  35.     }
  36. }