src/EventListener/AjaxFlashListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Auctioneer\BiddingResult;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Twig\Environment;
  8. class AjaxFlashListener
  9. {
  10.     protected $session;
  11.     public function __construct(SessionInterface $session, private readonly Environment $templating)
  12.     {
  13.         $this->session $session;
  14.     }
  15.     public function onKernelResponse(ResponseEvent $event): void
  16.     {
  17.         $response $event->getResponse();
  18.         $request $event->getRequest();
  19.         if (
  20.             $response instanceof JsonResponse
  21.             && in_array($request->get('_route'), ['frontend_auction_bid''frontend_purchase_instruction_set'])
  22.         ) {
  23.             $flashMessages $this->session->getFlashBag()->all();
  24.             if (!empty($flashMessages)) {
  25.                 $data json_decode($response->getContent(), true);
  26.                 foreach ($flashMessages as $status => $flashes) {
  27.                     foreach ($flashes as $flash) {
  28.                         if (BiddingResult::AMOUNT_BEATEN !== array_search($flashBiddingResult::$messages)) {
  29.                             $data['flash'][] = $this->templating->render('flash/_flash_notification.html.twig', [
  30.                                 'type' => $status,
  31.                                 'message' => $flash,
  32.                                 'duration' => 'short',
  33.                             ]);
  34.                         }
  35.                     }
  36.                 }
  37.                 $response->setData($data);
  38.             }
  39.         }
  40.     }
  41. }