Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

ViewRepository::joinTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Repository;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\Expr;
7
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
8
use Victoire\Bundle\CoreBundle\Entity\View;
9
use Victoire\Bundle\PageBundle\Entity\Page;
10
use Victoire\Bundle\PageBundle\Entity\PageStatus;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
12
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
13
14
/**
15
 * The View repository.
16
 */
17
class ViewRepository extends NestedTreeRepository
18
{
19
    use StateFullRepositoryTrait;
20
21
    protected $mainAlias = 'view';
22
23
    /**
24
     * Get the query builder for a view  by url.
25
     *
26
     * @param string $url The url
27
     *
28
     * @return \Doctrine\ORM\QueryBuilder The query builder
29
     */
30
    public function getOneByUrl($url)
31
    {
32
        return $this->createQueryBuilder($this->mainAlias)
33
            ->where($this->mainAlias.'.url = (:url)')
34
            ->setMaxResults(1)
35
            ->setParameter('url', $url);
36
    }
37
38
    /**
39
     * Filter the query by the sitemap index (=visibility).
40
     *
41
     * @param bool $indexed
42
     *
43
     * @return ViewRepository
44
     */
45
    public function filterBySitemapIndexed($indexed = true)
46
    {
47
        $qb = $this->getInstance();
48
        $qb->innerJoin($this->mainAlias.'.seo', 'seo')->addSelect('seo')
49
            ->andWhere('seo.sitemapIndexed = :sitemapIndexed')
50
            ->setParameter('sitemapIndexed', $indexed);
51
52
        return $this;
53
    }
54
55
    /**
56
     * Get all rentals in the repository.
57
     *
58
     * @param bool $excludeUnpublished Should we get only the published Views ?
59
     *
60
     * @return ViewRepository
61
     */
62
    public function getAll($excludeUnpublished = false)
63
    {
64
        $this->qb = $this->getInstance();
65
66
        //If $excludeUnpublished === true, we exclude the non published results
67
        if ($excludeUnpublished) {
68
            $this->qb
69
                ->andWhere($this->mainAlias.'.status = :status')
70
                ->orWhere($this->mainAlias.'.status = :scheduled_status AND page.publishedAt > :publicationDate')
71
                ->setParameter('status', PageStatus::PUBLISHED)
72
                ->setParameter('scheduled_status', PageStatus::SCHEDULED)
73
                ->setParameter('publicationDate', new \DateTime());
74
        }
75
76
        return $this;
77
    }
78
79
    /**
80
     * Find a large amount of views by ViewReferences.
81
     *
82
     * @param ViewReference[] $viewReferences
83
     *
84
     * @return View[]|null The entity instance or NULL if the entities cannot be found.
85
     */
86
    public function findByViewReferences(array $viewReferences)
87
    {
88
        $pageIds = [];
89
        foreach ($viewReferences as $viewReference) {
90
            if ($viewReference instanceof BusinessPageReference) {
91
                $pageIds[] = $viewReference->getTemplateId();
92
            } else {
93
                $pageIds[] = $viewReference->getViewId();
94
            }
95
        }
96
97
        $qb = $this->createQueryBuilder($this->mainAlias);
98
        $qb->andWhere($this->mainAlias.'.id IN (:pageIds)')
99
            ->setParameter('pageIds', $pageIds);
100
101
        $pages = $qb->getQuery()->getResult();
102
103
        foreach ($pages as $page) {
104
            $pageId = $page->getId();
105
            $viewReference = array_filter(
106
                $viewReferences,
107
                function ($e) use ($pageId) {
108
                    return $e->getViewId() == $pageId;
109
                });
110
            if (!empty($viewReference[0])) {
111
                $page->setCurrentLocale($viewReference[0]->getLocale());
112
            }
113
        }
114
115
        return $pages;
116
    }
117
118
    /**
119
     * Get the the view that is a homepage and a published one.
120
     *
121
     * @param string $locale
122
     *
123
     * @return Page
124
     */
125
    public function findOneByHomepage($locale = 'fr')
126
    {
127
        //the query builder
128
        $qb = $this->createQueryBuilder($this->mainAlias);
129
        $qb
130
            ->where($this->mainAlias.'.homepage = true')
131
            ->andWhere($this->mainAlias.'.status = :status')
132
            ->setMaxResults(1)
133
            ->setParameter('status', PageStatus::PUBLISHED);
134
        // Use Translation Walker
135
        $query = $qb->getQuery();
136
        $view = $query->getOneOrNullResult();
137
        $view->translate($locale);
138
139
        return $view;
140
    }
141
142
    /**
143
     * Get PageSeo.
144
     *
145
     * @param string $method leftJoin|innerJoin
146
     *
147
     * @return ViewRepository
148
     */
149
    public function joinSeo($method = 'leftJoin')
150
    {
151
        $this->getInstance()->$method($this->mainAlias.'.seo', 'seo')->addSelect('seo');
152
153
        return $this;
154
    }
155
156
    /**
157
     * Get PageSeo.
158
     *
159
     * @param string $method leftJoin|innerJoin
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
160
     *
161
     * @return ViewRepository
162
     */
163
    public function joinTranslations($locale)
164
    {
165
        $this->getInstance()
166
            ->innerJoin($this->mainAlias.'.translations', 'translation', Expr\Join::WITH, 'translation.locale = :locale')
167
            ->setParameter('locale', $locale);
168
169
        return $this;
170
    }
171
172
    /**
173
     * Filter the query by the sitemap index (=visibility).
174
     *
175
     * @param array $ids
176
     *
177
     * @return ViewRepository
178
     */
179
    public function filterByIds($ids)
180
    {
181
        $this->getInstance()->andWhere($this->mainAlias.'.id IN (:ids)')->setParameter('ids', $ids);
182
183
        return $this;
184
    }
185
}
186