<?php
namespace Aqarmap\Bundle\MainBundle\Listener;
use Aqarmap\Bundle\UserBundle\Constant\UserTypes;
use Aqarmap\Bundle\UserBundle\Entity\PersonalInfo;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Aqarmap\Bundle\UserBundle\Services\UserManager;
use Doctrine\ORM\EntityManagerInterface;
use Ikimea\Browser\Browser;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class RequestEventSubscriber implements EventSubscriberInterface
{
private Environment $engine;
private UserManager $userManager;
private \Twig_Environment $twig_Environment;
private EntityManagerInterface $entityManager;
private Security $security;
public function __construct(
ContainerInterface $container,
Environment $engine,
UserManager $userManager,
AuthorizationCheckerInterface $authorizationChecker,
\Twig_Environment $twig_Environment,
EntityManagerInterface $entityManager,
Security $security
) {
$this->engine = $engine;
$this->userManager = $userManager;
$this->twig_Environment = $twig_Environment;
$this->entityManager = $entityManager;
$this->security = $security;
}
public function onKernelRequest(RequestEvent $event): void
{
$securityToken = $this->security->getToken();
if ($event->getRequest()->query->get('_locale')
&& \in_array($event->getRequest()->get('_route'), ['listing_slug', 'listing_details', 'listing_view_notification', 'listing_view'])) {
$event->getRequest()->query->remove('_locale');
}
try {
$this->userManager->authenticateWithToken(
$event->getRequest()->get('access_token', $event->getRequest()->get('oauth_access_token')),
$event->getResponse()
);
} catch (\Exception $exception) {
}
// Only display the logo alert if user is logged in and he is a broker
$notifyLogo = false;
$notifyPersonalPhoto = false;
$notifyExpiry = false;
$notifyPersonalIsNotCompleted = false;
$routeName = (string) $event->getRequest()->get('_route', '');
if ($securityToken && !strpos($routeName, 'api_') && !strpos($routeName, 'admin_')) {
/** @var User $user */
$user = $this->security->getUser();
if ($user instanceof User && UserTypes::INDIVIDUAL != $user->getUserType()) {
if (!$user->getLogo()) {
$notifyLogo = true;
}
if (!$user->getPersonalPhoto()) {
$notifyPersonalPhoto = true;
}
if (0 === $user->getAbsoluteCreditExpiryDays()) {
$notifyExpiry = true;
}
}
if ($user instanceof User && UserTypes::INDIVIDUAL === $user->getUserType()) {
/** @var PersonalInfo $personalInfo */
$personalInfo = $user->getActivePersonalInfo();
$notifyPersonalIsNotCompleted = $personalInfo ? !$personalInfo->isDataCompleted() : true;
}
}
$this->twig_Environment->addGlobal('notifyLogo', $notifyLogo);
$this->twig_Environment->addGlobal('notifyPersonalPhoto', $notifyPersonalPhoto);
$this->twig_Environment->addGlobal('notifyExpiry', $notifyExpiry);
$this->twig_Environment->addGlobal('notifyPersonalIsNotCompleted', $notifyPersonalIsNotCompleted);
$browser = new Browser();
$event->getRequest()->attributes->set('_browser', [
'browser' => $browser->getBrowser(),
'version' => $browser->getVersion(),
'platform' => $browser->getPlatform(),
]);
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [KernelEvents::REQUEST => 'onKernelRequest'];
}
}