<?php
namespace Client\EventListener;
use App\Entity\Central\Client\Client;
use App\Entity\Central\User\User;
use App\Entity\Client\User\UserData;
use App\Service\AppManager;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
class ContextListener
{
private $tokenStorage;
private $appManager;
public function __construct(AppManager $appManager, TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->appManager = $appManager;
}
public function rebuildMetadata(RequestEvent $event)
{
$token = $this->tokenStorage->getToken();
if (!$token instanceof UsernamePasswordToken){
return;
}
$user = $token->getUser();
if (!$user instanceof User){
return;
} else {
$this->appManager->setUser($user);
}
$client = $user->getClient();
if(!$client instanceof Client){
return;
}
$this->appManager->setClient($client);
$userData = $this->appManager->getRepository(UserData::class)->findOneBy(['user' => $user]);
if ($userData instanceof UserData){
$store = null;
foreach ($userData->getStores() as $userStore){
$store = $userStore;
break;
}
$this->appManager->setStore($store);
}
}
}