<?php
namespace App\Component\CompanyMatch\EventSubscriber;
use App\Component\CompanyMatch\Client\CompanyMatchClient;
use App\Component\CompanyMatch\Event\CompanyMatchUserViewedEvent;
use App\Component\CompanyMatch\Form\Setting\CompanyMatchSettingType;
use App\Component\CompanyMatch\Util\CompanyMatchUserHistorian;
use App\Component\CompanyMatch\Util\CompanyMatchUtil;
use App\Entity\SiteUserData;
use App\Event\SettingsSavedEvent;
use App\EventListener\FeatureFlagListener;
use Doctrine\ORM\EntityManagerInterface;
use Flagception\Manager\FeatureManagerInterface;
use FOS\UserBundle\Controller\ResettingController;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
class CompanyMatchEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly CompanyMatchClient $client,
private readonly EntityManagerInterface $entityManager,
private readonly FeatureManagerInterface $featureManager,
private readonly ResettingController $resettingController,
private readonly RouterInterface $router,
protected readonly CompanyMatchUserHistorian $companyMatchUserHistorian,
) {
}
public static function getSubscribedEvents(): array
{
return [
SettingsSavedEvent::class => 'settingsSaved',
FOSUserEvents::REGISTRATION_SUCCESS => ['userRegistered', -50],
FOSUserEvents::REGISTRATION_COMPLETED => ['userRegisteredCompleted', -50],
LoginSuccessEvent::class => 'onLoginSuccess',
CompanyMatchUserViewedEvent::class => 'userViewed',
];
}
public function settingsSaved(SettingsSavedEvent $event): void
{
if (!$this->featureCheck()) {
return;
}
if (CompanyMatchSettingType::class !== \get_class($event->getType())) {
return;
}
$this->client->updateUrlToken();
$event->stopPropagation();
}
public function userRegistered(FormEvent $event): void
{
if (!$this->featureCheck()) {
return;
}
if (
!empty(CompanyMatchUtil::getCookie($event->getRequest()))
|| 'pages_user_register' === $event->getRequest()->attributes->get('_route')
) {
$event->setResponse(new RedirectResponse($this->router->generate('pages_user_dashboard')));
return;
}
$event->setResponse(new RedirectResponse($this->router->generate('company_match_init', [
'email' => $event->getForm()->getData()->getEmail(),
'user_dashboard' => true,
])));
}
public function userRegisteredCompleted(FilterUserResponseEvent $event): void
{
if (!$this->featureCheck()) {
return;
}
if (($request = $event->getRequest())
&& ($user = $event->getUser())->getPasswordRequestedAt() instanceof \DateTime
) {
$request->attributes->set('username', $user->getUsername());
$this->resettingController->sendEmailAction($request);
}
}
public function userViewed(CompanyMatchUserViewedEvent $event): void
{
$this->companyMatchUserHistorian->increaseViewCount($event->getCompanySiteUser());
}
public function onLoginSuccess(LoginSuccessEvent $event): void
{
if (!$this->featureCheck()) {
return;
}
if ('front_secured_area' !== $event->getFirewallName()) {
return;
}
if (($user = $this->entityManager->getRepository(SiteUserData::class)
->findOneBy(['user' => $event->getUser()])) instanceof SiteUserData
&& null !== ($companyMatchToken = $user->getCompanyMatchToken())
) {
CompanyMatchUtil::setCookie($event->getResponse(), $companyMatchToken);
}
}
private function featureCheck(): bool
{
return $this->featureManager->isActive(FeatureFlagListener::FEATURE_COMPANY_MATCH);
}
}