<?php
namespace App\EventSubscriber;
use App\Entity\FormTemplate;
use App\Entity\LinkCreditToVersion;
use App\Entity\LogConnection;
use App\Entity\Notification;
use App\Entity\User;
use App\Entity\UserMindpulseKey;
use App\Service\FormService;
use App\Services\AxonautApiService;
use App\Services\ClinicianCreditService;
use App\Services\NotificationService;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
class LoginSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $em,
private FormService $formService
) {}
public function onLogin(LoginSuccessEvent $event)
{
/** @var User|UserInterface $user */
$user = $event->getUser();
if (
in_array('ROLE_MEMBER', $user->getRoles()) ||
in_array('ROLE_SHOP', $user->getRoles())
) {
$forms = $this->em->getRepository(FormTemplate::class)->findActualityFormForUser($user);
foreach ($forms as $form) {
$this->formService->buildFormForUser($user, $form);
}
}
$logConnection = new LogConnection();
$logConnection
->setUser($user)
->setLoggedAt(new DateTimeImmutable())
;
$this->em->persist($logConnection);
$this->em->flush();
}
public function onLoginFailure(LoginFailureEvent $event)
{
$logConnection = new LogConnection();
$logConnection
->setLoggedAt(new DateTimeImmutable())
->setEmail($event->getRequest()->request->get('email', null))
->setPassword($event->getRequest()->request->get('password', null))
->setMessage($event->getException()->getMessage())
;
$this->em->persist($logConnection);
$this->em->flush();
}
public static function getSubscribedEvents(): array
{
return [
LoginSuccessEvent::class => 'onLogin',
LoginFailureEvent::class => 'onLoginFailure',
];
}
}