<?php
namespace App\EventSubscriber;
use App\Entity\User\User;
use Carbon\Carbon;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\Security\Core\Security;
class ActivitySubscriber implements EventSubscriberInterface
{
private $em;
private $security;
public function __construct(EntityManagerInterface $em, Security $security) {
$this->em = $em;
$this->security = $security;
}
// public static function getSubscribedEvents() {
// return [
// // must be registered before (i.e. with a higher priority than) the default Locale listener
// KernelEvents::TERMINATE => [['onTerminate', 20]],
// ];
// }
public function onKernelTerminate(TerminateEvent $event)
{
/** @var User $user */
$user = $this->security->getUser();
// $user = $this->em->getRepository(User::class)->findOneBy(['salt'=>$token]);
// dump($user);
// // if (!$user->isActive()) {
if($user){
$fortuneTeller = $user->getFortuneTeller();
if($fortuneTeller){
$user->changeLastOnlineAt(Carbon::now());
if (!$user->isActive()) {
$fortuneTeller->changeIsOnline(false);
$fortuneTeller->setChatAvailable(false);
} else {
$fortuneTeller->changeIsOnline(true);
// $fortuneTeller->setChatAvailable(true);
}
$this->em->persist($user);
$this->em->persist($fortuneTeller);
$this->em->flush();
}
}
// // }
}
public static function getSubscribedEvents()
{
return [
'kernel.terminate' => ['onKernelTerminate', 20],
];
}
}