|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\Page; |
|
8
|
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
9
|
|
|
use Doctrine\ORM\Query; |
|
10
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
11
|
|
|
use Knp\Component\Pager\Pagination\PaginationInterface; |
|
12
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @method Page|null find($id, $lockMode = null, $lockVersion = null) |
|
17
|
|
|
* @method Page|null findOneBy(array $criteria, array $orderBy = null) |
|
18
|
|
|
* @method Page[] findAll() |
|
19
|
|
|
* @method Page[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
20
|
|
|
*/ |
|
21
|
|
|
final class PageRepository extends ServiceEntityRepository |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PaginatorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $paginator; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(ManagerRegistry $registry, PaginatorInterface $paginator) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($registry, Page::class); |
|
31
|
|
|
$this->paginator = $paginator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function countAll(): int |
|
35
|
|
|
{ |
|
36
|
|
|
$count = $this->createQueryBuilder('p') |
|
37
|
|
|
->select('count(p.id)') |
|
38
|
|
|
->getQuery() |
|
39
|
|
|
->getSingleScalarResult(); |
|
40
|
|
|
|
|
41
|
|
|
return (int) $count; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function findLimit(): int |
|
45
|
|
|
{ |
|
46
|
|
|
$repository = $this->getEntityManager()->getRepository('App:Settings'); |
|
47
|
|
|
$limit = $repository->findOneBy(['setting_name' => 'items_per_page']); |
|
48
|
|
|
|
|
49
|
|
|
return (int) $limit->getSettingValue(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function findLatest(Request $request): PaginationInterface |
|
53
|
|
|
{ |
|
54
|
|
|
$qb = $this->createQueryBuilder('p') |
|
55
|
|
|
->orderBy('p.id', 'DESC'); |
|
56
|
|
|
|
|
57
|
|
|
return $this->createPaginator($qb->getQuery(), $request); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function createPaginator(Query $query, Request $request): PaginationInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$page = $request->query->getInt('page', 1); |
|
63
|
|
|
|
|
64
|
|
|
return $this->paginator->paginate($query, $page, $this->findLimit()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|