<?php
namespace App\EventListener\BaseSite;
use App\Entity\Generic\Customer\Customer;
use App\Entity\Generic\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
class OtpCheckSubscriber implements EventSubscriberInterface
{
public function __construct(
private Security $security,
private RouterInterface $router,
)
{
}
public function onKernelController(ControllerEvent $event): void
{
//
$request = $event->getRequest();
$route = $request->attributes->get('_route');
// فقط مسیرهای مربوط به پنل مشتری
if (!str_starts_with($request->getPathInfo(), '/shop-owner')) {
return;
}
$user = $this->security->getUser();
if (!$user instanceof User) {
return;
}
// مسیرهایی که باید مجاز باشن حتی اگه phone یا otp هنوز تأیید نشده
$safeRoutes = [
'app_base_site_auth_profile',
'app_register',
'app_verify_email',
'shop_owner_login',
'shop_owner_logout', // اگه logout هم داری
'base_site_enter_phone',
'base_site_otp_verify',
'app_logout', // اگه logout هم داری
'shop_owner_logout', // اگه logout هم داری
];
if (in_array((string)$route, $safeRoutes, true)) {
return;
}
if ((string)$route !== 'base_site_enter_phone') {
if (!$user->getMobile()) {
$url = $this->router->generate('base_site_enter_phone');
$event->setController(fn() => new RedirectResponse($url));
}
}
if ((string)$route !== 'base_site_otp_verify') {
if (!$user->isVerified()) {
$url = $this->router->generate('base_site_enter_phone');
$event->setController(fn() => new RedirectResponse($url));
}
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => [['onKernelController', 0]], // دقیقاً قبل اجرای کنترلر
];
}
}