<?php
/*
* This file is part of the nellapp-core package.
*
* (c) Benjamin Georgeault
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\EventSubscriber;
use App\Service\Sync\SenderService;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Contracts\EventDispatcher\Event;
/**
* Send entities that have been tracked to apps. Sent it on terminate event,
* only after the request has been sent to the client.
*
* @author Benjamin Georgeault
* @see \App\Service\Sync\SenderService
*/
class SyncSubscriber implements EventSubscriberInterface
{
public function __construct(
private SenderService $senderService,
) {}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::TERMINATE => 'sendTracked',
ConsoleEvents::TERMINATE => 'sendTracked',
];
}
public function sendTracked(Event $event): void
{
// Do it only on main request.
if (
$event instanceof TerminateEvent && (
!$event->isMainRequest()
|| ('security.firewall.map.context.dev' === $event->getRequest()->attributes->get('_firewall_context'))
)
) {
return;
}
$this->senderService->send();
}
}