<?php
namespace App\EntityManager;
use App\Entity\Forum;
use App\Entity\ForumAccess;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
class ForumAccessManager extends AbstractEntityManager
{
const SESSION_FORUM_ACCESS_KEY = 'forumAccess';
protected $session;
public function __construct(
EntityManagerInterface $entityManager,
TokenStorageInterface $tokenStorage,
CacheInterface $cache,
RequestStack $requestStack,
PaginatorInterface $paginator,
SessionInterface $session,
Security $security
) {
parent::__construct($entityManager, $tokenStorage, $cache, $requestStack, $paginator, $security);
$this->session = $session;
}
public function getForumAccess(Forum $forum): ?ForumAccess
{
return $this->getRepository()->getByForum($forum);
}
public function getForumAccessForCurrentUser(Forum $forum): ?ForumAccess
{
/** @var ForumAccess[] $forumAccess */
$forumAccess = $this->session->get(self::SESSION_FORUM_ACCESS_KEY);
return $forumAccess[$forum->getId()] ?? null;
}
public function setAccessInUserSession(User $user)
{
$access = $this->getRepository()->getByUser($user);
$this->session->set(self::SESSION_FORUM_ACCESS_KEY, $access);
}
public function getForumWithReadAccessForCurrentUser()
{
return $this->getRepository()->getForumWithReadAccessForUser($this->getUser());
}
protected function getRepository()
{
return $this->entityManager->getRepository(ForumAccess::class);
}
}