Completed
Pull Request — master (#323)
by Leny
12:57 queued 06:24
created

ViewRepository::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Repository;
4
5
use Doctrine\ORM\Query;
6
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
7
use Victoire\Bundle\PageBundle\Entity\PageStatus;
8
9
/**
10
 * The View repository.
11
 */
12
class ViewRepository extends NestedTreeRepository
13
{
14
    private $queryBuilder;
15
16
    /**
17
     * Get query builder instance.
18
     */
19
    public function getInstance()
20
    {
21
        return $this->queryBuilder ? $this->queryBuilder : $this->createQueryBuilder('page');
22
    }
23
24
    /**
25
     * Get the query builder for a view  by url.
26
     *
27
     * @param string $url The url
28
     *
29
     * @return \Doctrine\ORM\QueryBuilder The query builder
30
     */
31
    public function getOneByUrl($url)
32
    {
33
        return $this->createQueryBuilder('page')
34
            ->where('page.url = (:url)')
35
            ->setMaxResults(1)
36
            ->setParameter('url', $url);
37
    }
38
39
    /**
40
     * Filter the query by the sitemap index (=visibility).
41
     *
42
     * @param bool $indexed
43
     *
44
     * @return ViewRepository
45
     */
46
    public function filterBySitemapIndexed($indexed = true)
47
    {
48
        $qb = $this->getInstance();
49
        $qb->innerJoin('page.seo', 'seo')->addSelect('seo')
50
            ->andWhere('seo.sitemapIndexed = :sitemapIndexed')
51
            ->setParameter('sitemapIndexed', $indexed);
52
53
        return $this;
54
    }
55
56
    /**
57
     * Get all rentals in the repository.
58
     *
59
     * @param bool $excludeUnpublished Should we get only the published Views ?
60
     *
61
     * @return ViewRepository
62
     */
63
    public function getAll($excludeUnpublished = false)
64
    {
65
        $this->queryBuilder = $this->getInstance();
66
67
        //If $excludeUnpublished === true, we exclude the non published results
68
        if ($excludeUnpublished) {
69
            $this->queryBuilder
70
                ->andWhere('page.status = :status')
71
                ->orWhere('page.status = :scheduled_status AND page.publishedAt > :publicationDate')
72
                ->setParameter('status', PageStatus::PUBLISHED)
73
                ->setParameter('scheduled_status', PageStatus::SCHEDULED)
74
                ->setParameter('publicationDate', new \DateTime());
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * Run instance.
82
     *
83
     * @param string $method
84
     * @param string $hydrationMode
85
     *
86
     * @return array
87
     */
88
    public function run($method = 'getResult', $hydrationMode = Query::HYDRATE_OBJECT)
89
    {
90
        return $this->getInstance()->getQuery()->$method($hydrationMode);
91
    }
92
93
    /**
94
     * Get the the view that is a homepage and a published one.
95
     *
96
     * @param string $locale
97
     *
98
     * @return Page
99
     */
100
    public function findOneByHomepage($locale = 'fr')
101
    {
102
        //the query builder
103
        $queryBuilder = $this->createQueryBuilder('page');
104
105
        $queryBuilder
106
            ->where('page.homepage = true')
107
            ->andWhere('page.status = :status')
108
            ->andWhere('page.locale = :locale')
109
            ->setMaxResults(1)
110
            ->setParameter('locale', $locale)
111
            ->setParameter('status', PageStatus::PUBLISHED);
112
113
        $query = $queryBuilder->getQuery();
114
        $view = $query->getOneOrNullResult();
115
116
        return $view;
117
    }
118
119
    /**
120
     * Get PageSeo.
121
     *
122
     * @param string $method leftJoin|innerJoin
123
     *
124
     * @return ViewRepository
125
     */
126
    public function joinSeo($method = 'leftJoin')
127
    {
128
        $this->getInstance()->$method('page.seo', 'seo')->addSelect('seo');
129
130
        return $this;
131
    }
132
133
    /**
134
     * Filter the query by the sitemap index (=visibility).
135
     *
136
     * @param array $ids
137
     *
138
     * @return ViewRepository
139
     */
140
    public function filterByIds($ids)
141
    {
142
        $this->getInstance()->andWhere('page.id IN (:ids)')->setParameter('ids', $ids);
143
144
        return $this;
145
    }
146
}
147