src/EventSubscriber/ActivitySubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User\User;
  4. use Carbon\Carbon;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  8. use Symfony\Component\Security\Core\Security;
  9. class ActivitySubscriber implements EventSubscriberInterface
  10. {
  11.     private $em;
  12.     private $security;
  13.     public function __construct(EntityManagerInterface $emSecurity $security) {
  14.         $this->em $em;
  15.         $this->security $security;
  16.     }
  17.     // public static function getSubscribedEvents() {
  18.     //     return [
  19.     //         // must be registered before (i.e. with a higher priority than) the default Locale listener
  20.     //         KernelEvents::TERMINATE => [['onTerminate', 20]],
  21.     //     ];
  22.     // }
  23.     
  24.     public function onKernelTerminate(TerminateEvent $event)
  25.     {
  26.         /** @var User $user */
  27.         $user $this->security->getUser();
  28.         
  29.         // $user = $this->em->getRepository(User::class)->findOneBy(['salt'=>$token]);
  30.         // dump($user);
  31.         // // if (!$user->isActive()) {
  32.             if($user){
  33.                 $fortuneTeller $user->getFortuneTeller();
  34.                 if($fortuneTeller){
  35.                     $user->changeLastOnlineAt(Carbon::now());
  36.                     if (!$user->isActive()) {
  37.                         $fortuneTeller->changeIsOnline(false);
  38.                         $fortuneTeller->setChatAvailable(false);
  39.                     } else {
  40.                         $fortuneTeller->changeIsOnline(true);
  41.                         // $fortuneTeller->setChatAvailable(true);
  42.                     }
  43.     
  44.                     $this->em->persist($user);
  45.                     $this->em->persist($fortuneTeller);
  46.                     $this->em->flush();
  47.                 }
  48.             }
  49.         // // }
  50.     }
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             'kernel.terminate' => ['onKernelTerminate'20],
  55.         ];
  56.     }
  57. }