src/EventSubscriber/SyncSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the nellapp-core package.
  4.  *
  5.  * (c) Benjamin Georgeault
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\EventSubscriber;
  11. use App\Service\Sync\SenderService;
  12. use Symfony\Component\Console\ConsoleEvents;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Contracts\EventDispatcher\Event;
  17. /**
  18.  * Send entities that have been tracked to apps. Sent it on terminate event,
  19.  * only after the request has been sent to the client.
  20.  *
  21.  * @author Benjamin Georgeault
  22.  * @see \App\Service\Sync\SenderService
  23.  */
  24. class SyncSubscriber implements EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         private SenderService $senderService,
  28.     ) {}
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             KernelEvents::TERMINATE => 'sendTracked',
  33.             ConsoleEvents::TERMINATE => 'sendTracked',
  34.         ];
  35.     }
  36.     public function sendTracked(Event $event): void
  37.     {
  38.         // Do it only on main request.
  39.         if (
  40.             $event instanceof TerminateEvent && (
  41.                 !$event->isMainRequest()
  42.                 || ('security.firewall.map.context.dev' === $event->getRequest()->attributes->get('_firewall_context'))
  43.             )
  44.         ) {
  45.             return;
  46.         }
  47.         $this->senderService->send();
  48.     }
  49. }