Completed
Push — master ( 9ec196...c21c38 )
by Alex
11:51
created

PageRepository::findFrontPages()   C

Complexity

Conditions 14
Paths 112

Size

Total Lines 74
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 1
Metric Value
c 4
b 3
f 1
dl 0
loc 74
rs 5.1279
cc 14
eloc 45
nc 112
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
* This file is part of the OrbitaleCmsBundle package.
5
*
6
* (c) Alexandre Rock Ancelet <[email protected]>
7
*
8
* For the full copyright and license information, please view the LICENSE
9
* file that was distributed with this source code.
10
*/
11
12
namespace Orbitale\Bundle\CmsBundle\Repository;
13
14
use Doctrine\ORM\Tools\Pagination\Paginator;
15
use Orbitale\Bundle\CmsBundle\Entity\Category;
16
use Orbitale\Bundle\CmsBundle\Entity\Page;
17
18
class PageRepository extends AbstractRepository
19
{
20
    /**
21
     * @param Category $category
22
     * @param string   $order
23
     * @param string   $orderBy
24
     * @param int      $page
25
     * @param int      $limit
26
     *
27
     * @return Paginator
28
     */
29
    public function findByCategory(Category $category, $order, $orderBy, $page, $limit)
30
    {
31
        $qb = $this->createQueryBuilder('page')
32
            ->where('page.category = :category')
33
            ->orderBy('page.'.$orderBy, $order)
34
            ->setMaxResults($limit)
35
            ->setFirstResult($limit * ($page - 1))
36
            ->setParameter('category', $category);
37
38
        return new Paginator($qb->getQuery()->useResultCache($this->cacheEnabled, $this->cacheTtl));
39
    }
40
41
    /**
42
     * Will search for pages to show in front depending on the arguments.
43
     * If slugs are defined, there's no problem in looking for nulled host or locale,
44
     * because slugs are unique, so it does not.
45
     *
46
     * @param array       $slugs
47
     * @param string|null $host
48
     * @param string|null $locale
49
     *
50
     * @return Page
51
     */
52
    public function findFrontPages(array $slugs = array(), $host = null, $locale = null)
53
    {
54
        $qb = $this->createQueryBuilder('page')
55
            ->where('page.enabled = :enabled')
56
            ->leftJoin('page.category', 'category')
57
            ->andWhere('page.category is null OR category.enabled = :enabled')
58
            ->setParameter('enabled', true)
59
        ;
60
61
        // Will search differently if we're looking for homepage.
62
        $searchForHomepage = count($slugs) === 0;
63
64
        if ($searchForHomepage) {
65
            $qb
66
                ->andWhere('page.homepage = :homepage')
67
                ->setParameter('homepage', true)
68
            ;
69
        } else {
70
            $qb
71
                ->andWhere('page.slug IN ( :slugs )')
72
                ->setParameter('slugs', $slugs)
73
            ;
74
        }
75
76
        $hostWhere = 'page.host IS NULL';
77
        if (null !== $host) {
78
            $hostWhere .= ' OR page.host = :host';
79
            $qb->setParameter('host', $host);
80
        }
81
        $qb->andWhere($hostWhere);
82
83
        $localeWhere = 'page.locale IS NULL';
84
        if (null !== $locale) {
85
            $localeWhere .= ' OR page.locale = :locale';
86
            $qb->setParameter('locale', $locale);
87
        }
88
        $qb->andWhere($localeWhere);
89
90
        // This will allow getting first the pages that match both criteria
91
        $qb
92
            ->orderBy('page.host', 'DESC')
93
            ->addOrderBy('page.locale', 'DESC')
94
        ;
95
96
        /** @var Page[] $results */
97
        $results = $qb->getQuery()
98
            ->useResultCache($this->cacheEnabled, $this->cacheTtl)
99
            ->getResult();
100
101
        if ($searchForHomepage) {
102
            $homepage = null;
103
104
            foreach ($results as $page) {
105
                if (
106
                    ($page->getLocale() && $page->getHost())
107
                    || $page->getHost() || $page->getLocale()
108
                    || !$page->getLocale() || !$page->getHost()
109
                ) {
110
                    $homepage = $page;
111
                    break;
112
                }
113
            }
114
115
            $results = $homepage ? array($homepage) : array();
116
        }
117
118
        $resultsSortedBySlug = array();
119
120
        foreach ($results as $page) {
121
            $resultsSortedBySlug[$page->getSlug()] = $page;
122
        }
123
124
        return $resultsSortedBySlug;
125
    }
126
}
127