<?php
namespace App\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;
use App\Service\DynamicHostService;
#[AsEventListener(event: LogoutEvent::class, method: 'onLogout')]
class LogoutListener implements EventSubscriberInterface
{
public function __construct(
private ParameterBagInterface $parameterBag,
private EntityManagerInterface $em,
private Security $security,
private DynamicHostService $dynamicHostService,
){}
public static function getSubscribedEvents(): array
{
return [LogoutEvent::class => 'onLogout'];
}
public function onLogout(LogoutEvent $event): void
{
$user = $this->security->getUser();
$backSIteUrl = $this->dynamicHostService->backUrl();
if($user instanceof UserInterface){
$user->setIsLogged(false);
$this->em->persist($user);
$this->em->flush();
}
$request = $event->getRequest();
$target = $this->parameterBag->get('front_website_url').'/wp-login.php?action=logout';
$ch = curl_init();
//deconect in wordrpess
$url = $target;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
}