src/Subscribers/UserNoProfileRedirectSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Subscribers;
  3. use App\Controller\User\DashboardController;
  4. use App\Controller\User\OrderController;
  5. use App\Controller\User\PaymentController;
  6. use App\Controller\User\ProfileController;
  7. use App\Entity\User;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. class UserNoProfileRedirectSubscriber implements EventSubscriberInterface
  18. {
  19. protected Security $security;
  20. private RouterInterface $routerInterface;
  21. public function __construct( Security $security , RouterInterface $routerInterface )
  22. {
  23. $this->security = $security;
  24. $this->routerInterface = $routerInterface;
  25. }
  26. public function onKernelController(RequestEvent $event)
  27. {
  28. /**
  29. * @var User
  30. */
  31. $user = $this->security->getUser();
  32. $pathInfo = $this->routerInterface->getContext()->getPathInfo();
  33. $newProfilePath = $this->routerInterface->generate('user_settings_new');
  34. preg_match('/^\/user/' , $pathInfo , $matches);
  35. if (!empty($matches)) {
  36. if (is_null($user->getProfile())) {
  37. if ($pathInfo !== $newProfilePath) {
  38. $event->setResponse(
  39. new RedirectResponse(
  40. $newProfilePath
  41. )
  42. );
  43. }
  44. }
  45. }
  46. }
  47. public static function getSubscribedEvents(): array
  48. {
  49. return [
  50. KernelEvents::REQUEST => 'onKernelController',
  51. ];
  52. }
  53. }