<?php
namespace App\Subscribers;
use App\Controller\User\DashboardController;
use App\Controller\User\OrderController;
use App\Controller\User\PaymentController;
use App\Controller\User\ProfileController;
use App\Entity\User;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
class UserNoProfileRedirectSubscriber implements EventSubscriberInterface
{
protected Security $security;
private RouterInterface $routerInterface;
public function __construct( Security $security , RouterInterface $routerInterface )
{
$this->security = $security;
$this->routerInterface = $routerInterface;
}
public function onKernelController(RequestEvent $event)
{
/**
* @var User
*/
$user = $this->security->getUser();
$pathInfo = $this->routerInterface->getContext()->getPathInfo();
$newProfilePath = $this->routerInterface->generate('user_settings_new');
preg_match('/^\/user/' , $pathInfo , $matches);
if (!empty($matches)) {
if (is_null($user->getProfile())) {
if ($pathInfo !== $newProfilePath) {
$event->setResponse(
new RedirectResponse(
$newProfilePath
)
);
}
}
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelController',
];
}
}