<?php
namespace App\Service;
use App\Utils\Toast;
use Symfony\Component\HttpFoundation\RequestStack;
class ToastService {
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* Add toast to session
*/
public function toast($type, $title, $message = '') {
//Toast creation
$toast = new Toast($type, $title, $message);
//Add toast in session
$toastSession = $this->requestStack->getSession()->get('core_toast', []);
if(!is_array($toastSession)) $toastSession = Array();
array_push($toastSession, $toast);
$this->requestStack->getSession()->set('core_toast', $toastSession);
}
/**
* Return toasts and clean session
*/
public function getToats() {
$toasts = $this->requestStack->getSession()->get('core_toast', []);
//Clean session
$this->requestStack->getSession()->set('core_toast', []);
return $toasts;
}
}