Completed
Push — master ( cf1078...609837 )
by Alex
02:52
created

AbstractCmsController::getFinalTreeElement()   D

Complexity

Conditions 10
Paths 22

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
c 6
b 2
f 1
dl 0
loc 35
rs 4.8196
cc 10
eloc 20
nc 22
nop 2

How to fix   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\Controller;
13
14
use Orbitale\Bundle\CmsBundle\Entity\Category;
15
use Orbitale\Bundle\CmsBundle\Entity\Page;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
18
abstract class AbstractCmsController extends Controller
19
{
20
    /**
21
     * Slugs HAVE TO be ordered exactly as in the request.
22
     * This method will check that, in $elements, we have the same keys as in $slugs,
23
     * and that the hierarchy is correct.
24
     * This also prevents things like /children/parent to work,
25
     * as it should be /parent/children.
26
     *
27
     * @param array             $slugs
28
     * @param Page[]|Category[] $elements
29
     *
30
     * @return Category|Page
31
     */
32
    protected function getFinalTreeElement(array $slugs, array $elements)
33
    {
34
        // Will check that slugs and elements match
35
        $slugsElements = array_keys($elements);
36
        $sortedSlugs   = $slugs;
37
        sort($sortedSlugs);
38
        sort($slugsElements);
39
40
        if ($sortedSlugs !== $slugsElements || !count($slugs) || count($slugs) !== count($elements)) {
41
            throw $this->createNotFoundException();
42
        }
43
44
        /** @var Page|Category $element */
45
        $element = null;
46
        /** @var Page|Category $previousElement */
47
        $previousElement = null;
48
49
        foreach ($slugs as $slug) {
50
            $element = isset($elements[$slug]) ? $elements[$slug] : null;
51
            $match   = false;
52
            if ($element) {
53
                // Only for the first iteration
54
                $match = $previousElement
55
                    ? $element->getParent() && $previousElement->getSlug() === $element->getParent()->getSlug()
56
                    : true;
57
58
                $previousElement = $element;
59
            }
60
            if (!$match) {
61
                throw $this->createNotFoundException((new \ReflectionClass($element))->getShortName().' hierarchy not found.');
62
            }
63
        }
64
65
        return $element;
66
    }
67
}
68