src/Services/FixedNotificationManager.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. class FixedNotificationManager
  7. {
  8.     const
  9.         FIXED_NOTIF_KEY "site_fixed_notifications"
  10.     ;
  11.     private SessionInterface $session;
  12.     private ?Request $request;
  13.     public function __construct(
  14.         private RequestStack $requestStack
  15.     ) {
  16.         $this->request $this->requestStack->getCurrentRequest();
  17.         if ($this->request instanceof Request) {
  18.             $this->session $this->request->getSession();
  19.         }
  20.     }
  21.     /**
  22.      * @return array|mixed
  23.      */
  24.     public function getNotificationsToDisplay()
  25.     {
  26.         return $this->getNotifsArray();
  27.     }
  28.     /**
  29.      * @param $text
  30.      */
  31.     public function addNotification($text)
  32.     {
  33.         $notifs $this->getNotifsArray();
  34.         $notifs[] = $text;
  35.         $this->session->set(self::FIXED_NOTIF_KEYjson_encode($notifs));
  36.     }
  37.     /**
  38.      * @return array|mixed
  39.      */
  40.     private function getNotifsArray()
  41.     {
  42.         if ($this->session->has(self::FIXED_NOTIF_KEY)) {
  43.             $notifs json_decode($this->session->get(self::FIXED_NOTIF_KEY), true);
  44.         } else {
  45.             $notifs = [];
  46.         }
  47.         return $notifs;
  48.     }
  49. }