src/EventListener/LogoutListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Symfony\Component\Security\Core\Security;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Security\Http\Event\LogoutEvent;
  12. use App\Service\DynamicHostService;
  13. #[AsEventListener(eventLogoutEvent::class, method'onLogout')]
  14. class LogoutListener implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private ParameterBagInterface $parameterBag,
  18.         private EntityManagerInterface $em,
  19.         private Security $security,
  20.         private DynamicHostService $dynamicHostService,
  21.     ){}
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [LogoutEvent::class => 'onLogout'];
  25.     }
  26.     public function onLogout(LogoutEvent $event): void
  27.     {
  28.         $user $this->security->getUser();
  29.         $backSIteUrl $this->dynamicHostService->backUrl();
  30.         if($user instanceof UserInterface){
  31.             $user->setIsLogged(false);
  32.             $this->em->persist($user);
  33.             $this->em->flush();
  34.     
  35.         }
  36.         $request $event->getRequest();
  37.         $target $this->parameterBag->get('front_website_url').'/wp-login.php?action=logout';
  38.         $ch curl_init();
  39.         //deconect in wordrpess
  40.         $url $target;
  41.         curl_setopt($chCURLOPT_URL$url);
  42.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  43.         $result curl_exec($ch);
  44.         curl_close($ch);
  45.     }
  46. }