Passed
Push — master ( abdb51...0ee754 )
by Dev
16:27 queued 01:19
created

PageRepository::andHost()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 6
1
<?php
2
3
namespace PiedWeb\CMSBundle\Repository;
4
5
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
6
use Doctrine\ORM\QueryBuilder;
7
use PiedWeb\CMSBundle\Entity\PageInterface as Page;
8
9
/**
10
 * @method Page|null find($id, $lockMode = null, $lockVersion = null)
11
 * @method Page|null findOneBy(array $criteria, array $orderBy = null)
12
 * @method Page[]    findAll()
13
 * @method Page[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
14
 */
15
class PageRepository extends ServiceEntityRepository
16
{
17
    public function getQueryToFindPublished($p): QueryBuilder
18
    {
19
        return $this->createQueryBuilder($p)
20
            ->andWhere($p.'.createdAt <=  :nwo')
21
            ->setParameter('nwo', new \DateTime())
22
            ->orderBy($p.'.createdAt', 'DESC');
23
    }
24
25
    public function getPage($slug, $host, $hostCanBeNull): ?Page
26
    {
27
        $qb = $this->createQueryBuilder('p')
28
            ->andWhere('p.slug =  :slug')->setParameter('slug', $slug);
29
30
        $qb = $this->andHost($qb, $host, $hostCanBeNull);
31
32
        return $qb->getQuery()->getResult()[0] ?? null;
33
    }
34
35
    protected function andNotRedirection(QueryBuilder $qb): QueryBuilder
36
    {
37
        return $qb->andWhere('p.mainContent IS NULL OR p.mainContent NOT LIKE :noi')
38
            ->setParameter('noi', 'Location:%');
39
    }
40
41
    protected function andIndexable(QueryBuilder $qb): QueryBuilder
42
    {
43
        return $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi2')
44
            ->setParameter('noi2', '%noindex%');
45
    }
46
47
    protected function andHost(QueryBuilder $qb, $host, $hostCanBeNull = false): QueryBuilder
48
    {
49
        return $qb->andWhere('(p.host = :h '.($hostCanBeNull ? ' OR p.host IS NULL' : '').')')
50
            ->setParameter('h', $host);
51
    }
52
53
    protected function andLocale(QueryBuilder $qb, $locale, $defaultLocale): QueryBuilder
54
    {
55
        return $qb->andWhere(($defaultLocale == $locale ? 'p.locale IS NULL OR ' : '').'p.locale LIKE :locale')
56
                ->setParameter('locale', $locale);
57
    }
58
59
    /**
60
     * Return page for sitemap
61
     * $qb->getQuery()->getResult();.
62
     */
63
    public function getIndexablePages(
64
        $host,
65
        $hostCanBeNull,
66
        $locale,
67
        $defaultLocale,
68
        ?int $limit = null
69
    ): QueryBuilder {
70
        $qb = $this->getQueryToFindPublished('p');
71
        $qb = $this->andIndexable($qb);
72
        $qb = $this->andNotRedirection($qb);
73
        $qb = $this->andHost($qb, $host, $hostCanBeNull);
74
        $qb = $this->andLocale($qb, $locale, $defaultLocale);
75
76
        if (null !== $limit) {
77
            $qb->setMaxResults($limit);
78
        }
79
80
        return $qb;
81
    }
82
83
    public function getPagesWithoutParent()
84
    {
85
        $q = $this->createQueryBuilder('p')
86
            ->andWhere('p.parentPage is NULL')
87
            ->orderBy('p.slug', 'DESC')
88
            ->getQuery();
89
90
        return $q->getResult();
91
    }
92
93
    public function getPagesUsingMedia($media)
94
    {
95
        $q = $this->createQueryBuilder('p')
96
            ->andWhere('p.mainContent LIKE :val')
97
            ->setParameter('val', '%'.$media.'%')
98
            ->getQuery()
99
        ;
100
101
        return $q->getResult();
102
    }
103
}
104