<?php
namespace App\Services;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class FixedNotificationManager
{
const
FIXED_NOTIF_KEY = "site_fixed_notifications"
;
private SessionInterface $session;
private ?Request $request;
public function __construct(
private RequestStack $requestStack
) {
$this->request = $this->requestStack->getCurrentRequest();
if ($this->request instanceof Request) {
$this->session = $this->request->getSession();
}
}
/**
* @return array|mixed
*/
public function getNotificationsToDisplay()
{
return $this->getNotifsArray();
}
/**
* @param $text
*/
public function addNotification($text)
{
$notifs = $this->getNotifsArray();
$notifs[] = $text;
$this->session->set(self::FIXED_NOTIF_KEY, json_encode($notifs));
}
/**
* @return array|mixed
*/
private function getNotifsArray()
{
if ($this->session->has(self::FIXED_NOTIF_KEY)) {
$notifs = json_decode($this->session->get(self::FIXED_NOTIF_KEY), true);
} else {
$notifs = [];
}
return $notifs;
}
}