PageRepository::getQueryToFindPublished()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 list<T>   findAll()
13
 * @method list<T>   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
        if ((int) $slug > 0) {
31
            $qb->orWhere('p.id =  :slug')->setParameter('slug', $slug);
32
        }
33
34
        $qb = $this->andHost($qb, $host, $hostCanBeNull);
35
36
        return $qb->getQuery()->getResult()[0] ?? null;
37
    }
38
39
    protected function andNotRedirection(QueryBuilder $qb): QueryBuilder
40
    {
41
        return $qb->andWhere('p.mainContent IS NULL OR p.mainContent NOT LIKE :noi')
42
            ->setParameter('noi', 'Location:%');
43
    }
44
45
    protected function andIndexable(QueryBuilder $qb): QueryBuilder
46
    {
47
        return $qb->andWhere('p.metaRobots IS NULL OR p.metaRobots NOT LIKE :noi2')
48
            ->setParameter('noi2', '%noindex%');
49
    }
50
51
    public function andHost(QueryBuilder $qb, $host, $hostCanBeNull = false): QueryBuilder
52
    {
53
        return $qb->andWhere('(p.host = :h '.($hostCanBeNull ? ' OR p.host IS NULL' : '').')')
54
            ->setParameter('h', $host);
55
    }
56
57
    protected function andLocale(QueryBuilder $qb, $locale, $defaultLocale): QueryBuilder
58
    {
59
        return $qb->andWhere(($defaultLocale == $locale ? 'p.locale IS NULL OR ' : '').'p.locale LIKE :locale')
60
                ->setParameter('locale', $locale);
61
    }
62
63
    /**
64
     * Return page for sitemap
65
     * $qb->getQuery()->getResult();.
66
     */
67
    public function getIndexablePages(
68
        $host,
69
        $hostCanBeNull,
70
        $locale,
71
        $defaultLocale,
72
        ?int $limit = null
73
    ): QueryBuilder {
74
        $qb = $this->getQueryToFindPublished('p');
75
        $qb = $this->andIndexable($qb);
76
        $qb = $this->andNotRedirection($qb);
77
        $qb = $this->andHost($qb, $host, $hostCanBeNull);
78
        $qb = $this->andLocale($qb, $locale, $defaultLocale);
79
80
        if (null !== $limit) {
81
            $qb->setMaxResults($limit);
82
        }
83
84
        return $qb;
85
    }
86
87
    public function getPagesWithoutParent()
88
    {
89
        $q = $this->createQueryBuilder('p')
90
            ->andWhere('p.parentPage is NULL')
91
            ->orderBy('p.slug', 'DESC')
92
            ->getQuery();
93
94
        return $q->getResult();
95
    }
96
97
    public function getPagesUsingMedia($media)
98
    {
99
        $q = $this->createQueryBuilder('p')
100
            ->andWhere('p.mainContent LIKE :val')
101
            ->setParameter('val', '%'.$media.'%')
102
            ->getQuery()
103
        ;
104
105
        return $q->getResult();
106
    }
107
}
108