<?php
namespace App\Controller;
use App\Dto\OpinionFilterDto;
use App\Entity\User;
use App\Privilege\RoleFactory;
use App\Repository\OpinionRepository;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @method User getUser()
* @Route("/opinion", name="opinion")
*/
class OpinionController extends AbstractController
{
const OPINION_PER_PAGE_LATEST = 8;
const OPINION_PER_PAGE_BEST = 20;
/**
* @var OpinionRepository
*/
private OpinionRepository $opinionRepository;
/**
* @var PaginatorInterface
*/
private PaginatorInterface $paginator;
public function __construct(
OpinionRepository $opinionRepository,
PaginatorInterface $paginator
)
{
$this->opinionRepository = $opinionRepository;
$this->paginator = $paginator;
}
/**
* @IsGranted(RoleFactory::ROLE_USER)
* @Route("", name="")
*/
public function latest(Request $request, OpinionFilterDto $opinionFilterDto): Response
{
$pagination = $this->paginator->paginate(
$this->opinionRepository->getLatestOpinion($this->getUser(), $opinionFilterDto),
$request->query->getInt('page', 1),
self::OPINION_PER_PAGE_LATEST
);
return $this->render('opinion/latest.html.twig', [
'pagination' => $pagination,
]);
}
/**
* @IsGranted(RoleFactory::ROLE_USER)
* @Route("/best", name="_best")
*/
public function best(Request $request, OpinionFilterDto $opinionFilterDto): Response
{
$pagination = $this->paginator->paginate(
$this->opinionRepository->getBestOpinion($this->getUser(), $opinionFilterDto),
$request->query->getInt('page', 1),
self::OPINION_PER_PAGE_BEST
);
return $this->render('opinion/best.html.twig', [
'pagination' => $pagination,
]);
}
}