src/Service/Entry/ReportService.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Service\Entry;
  3. use App\Entity\Entry\Entry;
  4. use App\Repository\Entry\EntryRepository;
  5. use App\Repository\Entry\Report\PostRepository;
  6. use App\Utils\GetQueryUtilTrait;
  7. use App\Utils\PaginationUtil;
  8. use Doctrine\ORM\QueryBuilder;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class ReportService
  11. {
  12.     use GetQueryUtilTrait;
  13.     public const RESUME_SESSION_NAME "entry_report_index_resume";
  14.     private PostRepository $postRepository;
  15.     private EntryRepository $entryRepository;
  16.     public function __construct(
  17.         PostRepository $postRepository,
  18.         EntryRepository $entryRepository
  19.     ) {
  20.         $this->postRepository $postRepository;
  21.         $this->entryRepository $entryRepository;
  22.     }
  23.     public function getCriteriaFromRequest(Request $request): array {
  24.         $criteria = [];
  25.         if($entryCategory $this->getSingleQuery($request"category")) {
  26.             $criteria['category'] = $entryCategory;
  27.         }
  28.         if($page $this->getSingleQuery($request"page")) {
  29.             $criteria['page'] = $page;
  30.         } else {
  31.             $criteria['page'] = 1;
  32.         }
  33.         $request->getSession()->set(self::RESUME_SESSION_NAME$criteria);
  34.         return $criteria;
  35.     }
  36.     public function getQueryBuilder(array $criteria): QueryBuilder
  37.     {
  38.         $qb $this->postRepository->getListQuery([]);
  39.         $qb
  40.             ->leftJoin(Entry::class, "e""with""a.Entry = e.id")
  41.             ;
  42.         if(isset($criteria['id'])) {
  43.             $qb
  44.                 ->andWhere($qb->expr()->eq('a.id'':id'))
  45.                 ->setParameter("id" $criteria['id'])
  46.                 ;
  47.         }
  48.         if(isset($criteria['category'])) {
  49.             $qb
  50.                 ->andWhere($qb->expr()->eq('e.category'":category"))
  51.                 ->setParameter("category"$criteria['category'])
  52.             ;
  53.         }
  54.         $this->entryRepository->joinOwner($qb);
  55.         $this->entryRepository->addPublishWhere($qb);
  56.         return $qb;
  57.     }
  58.     public function handleList(QueryBuilder $qb, array $criteria): PaginationUtil
  59.     {
  60.         if(!isset($criteria['page'])) {
  61.             $criteria['page'] = 1;
  62.         }
  63.         if(!isset($criteria['liimit'])) {
  64.             $criteria['limit'] = 12;
  65.         }
  66.         return new PaginationUtil($qb$criteria['page'], $criteria['limit']);
  67.     }
  68. }