<?php
namespace App\EventListener;
use App\Entity\Gos\User;
use App\Event\DiaboloResponseEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class DiaboloResponseListener implements EventSubscriberInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
DiaboloResponseEvent::DIABOLO_RESPONSE => 'onDiaboloResponse',
];
}
public function onDiaboloResponse(DiaboloResponseEvent $event): void
{
$responseHttpCode = $event->getResponseHttpCode();
$user = $event->getUser();
if ($user instanceof User) {
$failureAt = $responseHttpCode !== 200 ? new \DateTime() : null;
$user->setLastFailureDiaboloResponseAt($failureAt);
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
}