<?php
namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Constant\TimelineDataConstant;
use App\Entity\Theservice;
use App\Service\Api\Timeline\OtherMonitoring;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Psr\Log\LoggerInterface;
class TheServiceSubscriber implements EventSubscriberInterface
{
/**
* @var OtherMonitoring
*/
private $otherMonitoring;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param OtherMonitoring $otherMonitoring
* @param LoggerInterface $logger
*/
public function __construct(OtherMonitoring $otherMonitoring, LoggerInterface $logger)
{
$this->otherMonitoring = $otherMonitoring;
$this->logger = $logger;
}
/**
*
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['onPostCreate', EventPriorities::POST_WRITE],
];
}
/**
*
* @param ViewEvent $event
*/
public function onPostCreate(ViewEvent $event): void
{
$entity = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($entity instanceof Theservice && $method === 'POST') {
try {
$this->otherMonitoring->eventOnTheServiceCreation(
$entity,
TimelineDataConstant::SERVICE_PROXIMITY_CREATION,
TimelineDataConstant::COLOR_THEMES['w-local']['variants']['service_proximity']
);
} catch (\Exception $e) {
$this->logger->error('Error during timeline creation for The service: ' . $e->getMessage());
}
}
}
}