|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use App\Entity\Property; |
|
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\Security\Core\Security; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @method Property|null find($id, $lockMode = null, $lockVersion = null) |
|
17
|
|
|
* @method Property|null findOneBy(array $criteria, array $orderBy = null) |
|
18
|
|
|
* @method Property[] findAll() |
|
19
|
|
|
* @method Property[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) |
|
20
|
|
|
*/ |
|
21
|
|
|
class PropertyRepository extends ServiceEntityRepository |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var PaginatorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $paginator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Security |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $security; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(ManagerRegistry $registry, PaginatorInterface $paginator, Security $security) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($registry, Property::class); |
|
36
|
|
|
$this->paginator = $paginator; |
|
37
|
|
|
$this->security = $security; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function findAllPublished(): array |
|
41
|
|
|
{ |
|
42
|
|
|
$qb = $this->createQueryBuilder('p'); |
|
43
|
|
|
$query = $qb->where("p.state = 'published'") |
|
44
|
|
|
->orderBy('p.id', 'DESC') |
|
45
|
|
|
->getQuery(); |
|
46
|
|
|
|
|
47
|
|
|
return $query->execute(); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function countAll(): int |
|
51
|
|
|
{ |
|
52
|
|
|
$count = $this->createQueryBuilder('p') |
|
53
|
|
|
->select('count(p.id)') |
|
54
|
|
|
->getQuery() |
|
55
|
|
|
->getSingleScalarResult(); |
|
56
|
|
|
|
|
57
|
|
|
return (int) $count; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function findLimit(): int |
|
61
|
|
|
{ |
|
62
|
|
|
$repository = $this->getEntityManager()->getRepository('App:Settings'); |
|
63
|
|
|
$limit = $repository->findOneBy(['setting_name' => 'items_per_page']); |
|
64
|
|
|
|
|
65
|
|
|
return (int) $limit->getSettingValue(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function createPaginator(Query $query, int $page): PaginationInterface |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->paginator->paginate($query, $page, $this->findLimit()); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|