Test Failed
Branch master (354693)
by Valery
12:29
created

PropertyRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 69
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findLimit() 0 7 1
A __construct() 0 3 1
A countAll() 0 8 1
A findByFilter() 0 24 4
A createPaginator() 0 7 1
A findLatest() 0 8 1
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 Pagerfanta\Adapter\DoctrineORMAdapter;
11
use Pagerfanta\Pagerfanta;
12
use Symfony\Bridge\Doctrine\RegistryInterface;
13
14
/**
15
 * @method Property|null find($id, $lockMode = null, $lockVersion = null)
16
 * @method Property|null findOneBy(array $criteria, array $orderBy = null)
17
 * @method Property[]    findAll()
18
 * @method Property[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
19
 */
20
final class PropertyRepository extends ServiceEntityRepository
21
{
22
    public function __construct(RegistryInterface $registry)
23
    {
24
        parent::__construct($registry, Property::class);
25
    }
26
27
    public function countAll(): int
28
    {
29
        $count = $this->createQueryBuilder('p')
30
            ->select('count(p.id)')
31
            ->getQuery()
32
            ->getSingleScalarResult();
33
34
        return (int) $count;
35
    }
36
37
    public function findLatest(int $page = 1): Pagerfanta
38
    {
39
        $qb = $this->createQueryBuilder('p')
40
            ->addSelect('f')
41
            ->leftJoin('p.photos', 'f')
42
            ->orderBy('p.id', 'DESC');
43
44
        return $this->createPaginator($qb->getQuery(), $page);
45
    }
46
47
    public function findByFilter(int $locality, int $operation, int $category, int $page = 1): Pagerfanta
48
    {
49
        $qb = $this->createQueryBuilder('p');
50
51
        $qb->Where('p.published = 1');
52
53
        // Locality
54
        if ($locality > 0) {
55
            $qb->andWhere('p.locality = '.(int) $locality);
56
        }
57
58
        // Operation
59
        if ($operation > 0) {
60
            $qb->andWhere('p.operation = '.(int) $operation);
61
        }
62
63
        // Category
64
        if ($category > 0) {
65
            $qb->andWhere('p.category = '.(int) $category);
66
        }
67
68
        $qb->orderBy('p.id', 'DESC');
69
70
        return $this->createPaginator($qb->getQuery(), $page);
71
    }
72
73
    private function findLimit(): int
74
    {
75
        $em = $this->getEntityManager();
76
77
        $query = $em->createQuery("SELECT s.items_per_page FROM App\Entity\Setting s");
78
79
        return (int) $query->getSingleScalarResult();
80
    }
81
82
    private function createPaginator(Query $query, int $page): Pagerfanta
83
    {
84
        $paginator = new Pagerfanta(new DoctrineORMAdapter($query));
85
        $paginator->setMaxPerPage($this->findLimit());
86
        $paginator->setCurrentPage($page);
87
88
        return $paginator;
89
    }
90
}
91