SQL and performance improvements (#2017)

This commit is contained in:
Kevin Papst
2020-10-23 00:31:53 +02:00
committed by GitHub
parent 6ed7ee76e7
commit 4533c994a3
14 changed files with 216 additions and 206 deletions

View File

@@ -10,48 +10,37 @@
namespace App\Security;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
/**
* @deprecated will be removed with 2.0
*/
final class CurrentUser
{
/**
* @var TokenStorageInterface
*/
private $storage;
/**
* @var UserRepository
*/
private $repository;
/**
* @var User|null
*/
private $user;
/**
* @param TokenStorageInterface $storage
* @param UserRepository $repository
*/
public function __construct(TokenStorageInterface $storage, UserRepository $repository)
public function __construct(TokenStorageInterface $storage)
{
$this->storage = $storage;
$this->repository = $repository;
}
/**
* @return User|null
*/
public function getUser()
public function getUser(): ?User
{
if (null === $this->storage->getToken()) {
return null;
}
// some inline caching to prevent multiple DB lookups
if (null !== $this->user) {
return $this->user;
}
if (null === $this->storage->getToken()) {
return null;
}
/** @var User $user */
$user = $this->storage->getToken()->getUser();
@@ -59,7 +48,7 @@ final class CurrentUser
return null;
}
$this->user = $this->repository->getUserById($user->getId());
$this->user = $user;
return $this->user;
}