Completed
Push — master ( 0b2e6b...2c86a7 )
by Paul
49:42 queued 39:17
created

ViewRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 172
Duplicated Lines 4.65 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 8
loc 172
rs 10
c 3
b 2
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 4 2
A getOneByUrl() 0 7 1
A filterBySitemapIndexed() 0 9 1
A getAll() 8 16 2
A run() 0 4 1
A findOneByHomepage() 0 16 1
A joinSeo() 0 6 1
A filterByIds() 0 6 1
B findByViewReferences() 0 31 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\CoreBundle\Entity\View;
8
use Victoire\Bundle\PageBundle\Entity\Page;
9
use Victoire\Bundle\PageBundle\Entity\PageStatus;
10
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
12
13
/**
14
 * The View repository.
15
 */
16
class ViewRepository extends NestedTreeRepository
17
{
18
    private $queryBuilder;
19
20
    /**
21
     * Get query builder instance.
22
     */
23
    public function getInstance()
24
    {
25
        return $this->queryBuilder ? $this->queryBuilder : $this->createQueryBuilder('page');
26
    }
27
28
    /**
29
     * Get the query builder for a view  by url.
30
     *
31
     * @param string $url The url
32
     *
33
     * @return \Doctrine\ORM\QueryBuilder The query builder
34
     */
35
    public function getOneByUrl($url)
36
    {
37
        return $this->createQueryBuilder('page')
38
            ->where('page.url = (:url)')
39
            ->setMaxResults(1)
40
            ->setParameter('url', $url);
41
    }
42
43
    /**
44
     * Filter the query by the sitemap index (=visibility).
45
     *
46
     * @param bool $indexed
47
     *
48
     * @return ViewRepository
49
     */
50
    public function filterBySitemapIndexed($indexed = true)
51
    {
52
        $qb = $this->getInstance();
53
        $qb->innerJoin('page.seo', 'seo')->addSelect('seo')
54
            ->andWhere('seo.sitemapIndexed = :sitemapIndexed')
55
            ->setParameter('sitemapIndexed', $indexed);
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get all rentals in the repository.
62
     *
63
     * @param bool $excludeUnpublished Should we get only the published Views ?
64
     *
65
     * @return ViewRepository
66
     */
67
    public function getAll($excludeUnpublished = false)
68
    {
69
        $this->queryBuilder = $this->getInstance();
70
71
        //If $excludeUnpublished === true, we exclude the non published results
72 View Code Duplication
        if ($excludeUnpublished) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            $this->queryBuilder
74
                ->andWhere('page.status = :status')
75
                ->orWhere('page.status = :scheduled_status AND page.publishedAt > :publicationDate')
76
                ->setParameter('status', PageStatus::PUBLISHED)
77
                ->setParameter('scheduled_status', PageStatus::SCHEDULED)
78
                ->setParameter('publicationDate', new \DateTime());
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * Run instance.
86
     *
87
     * @param string $method
88
     * @param string $hydrationMode
89
     *
90
     * @return array
91
     */
92
    public function run($method = 'getResult', $hydrationMode = Query::HYDRATE_OBJECT)
93
    {
94
        return $this->getInstance()->getQuery()->$method($hydrationMode);
95
    }
96
97
    /**
98
     * Find a large amount of views by ViewReferences.
99
     *
100
     * @param ViewReference[] $viewReferences
101
     *
102
     * @return View[]|null The entity instance or NULL if the entities cannot be found.
103
     */
104
    public function findByViewReferences(array $viewReferences)
105
    {
106
        $pageIds = [];
107
        foreach ($viewReferences as $viewReference) {
108
            if ($viewReference instanceof BusinessPageReference) {
109
                $pageIds[] = $viewReference->getTemplateId();
110
            } else {
111
                $pageIds[] = $viewReference->getViewId();
112
            }
113
        }
114
115
        $queryBuilder = $this->createQueryBuilder('page');
116
        $queryBuilder->andWhere('page.id IN (:pageIds)')
117
            ->setParameter('pageIds', $pageIds);
118
119
        $pages = $queryBuilder->getQuery()->getResult();
120
121
        foreach ($pages as $page) {
122
            $pageId = $page->getId();
123
            $viewReference = array_filter(
124
                $viewReferences,
125
                function ($e) use ($pageId) {
126
                    return $e->getViewId() == $pageId;
127
                });
128
            if (!empty($viewReference[0])) {
129
                $page->setCurrentLocale($viewReference[0]->getLocale());
130
            }
131
        }
132
133
        return $pages;
134
    }
135
136
    /**
137
     * Get the the view that is a homepage and a published one.
138
     *
139
     * @param string $locale
140
     *
141
     * @return Page
142
     */
143
    public function findOneByHomepage($locale = 'fr')
144
    {
145
        //the query builder
146
        $queryBuilder = $this->createQueryBuilder('page');
147
        $queryBuilder
148
            ->where('page.homepage = true')
149
            ->andWhere('page.status = :status')
150
            ->setMaxResults(1)
151
            ->setParameter('status', PageStatus::PUBLISHED);
152
        // Use Translation Walker
153
        $query = $queryBuilder->getQuery();
154
        $view = $query->getOneOrNullResult();
155
        $view->translate($locale);
156
157
        return $view;
158
    }
159
160
    /**
161
     * Get PageSeo.
162
     *
163
     * @param string $method leftJoin|innerJoin
164
     *
165
     * @return ViewRepository
166
     */
167
    public function joinSeo($method = 'leftJoin')
168
    {
169
        $this->getInstance()->$method('page.seo', 'seo')->addSelect('seo');
170
171
        return $this;
172
    }
173
174
    /**
175
     * Filter the query by the sitemap index (=visibility).
176
     *
177
     * @param array $ids
178
     *
179
     * @return ViewRepository
180
     */
181
    public function filterByIds($ids)
182
    {
183
        $this->getInstance()->andWhere('page.id IN (:ids)')->setParameter('ids', $ids);
184
185
        return $this;
186
    }
187
}
188