MenuBuilder::reorderMenuItems()   C
last analyzed

Complexity

Conditions 11
Paths 56

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 43.4988

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 11
cts 31
cp 0.3548
rs 6.8351
c 0
b 0
f 0
cc 11
nc 56
nop 1
crap 43.4988

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
namespace SumoCoders\FrameworkCoreBundle\Menu;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\ItemInterface;
7
use SumoCoders\FrameworkCoreBundle\Event\ConfigureMenuEvent;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
10
class MenuBuilder
11
{
12
    /**
13
     * @var EventDispatcherInterface
14
     */
15
    private $eventDispatcher;
16
17
    /**
18
     * @var FactoryInterface
19
     */
20
    private $factory;
21
22
    /**
23
     * @param EventDispatcherInterface $eventDispatcher
24
     */
25 9
    public function setEventDispatcher($eventDispatcher)
26
    {
27 9
        $this->eventDispatcher = $eventDispatcher;
28 9
    }
29
30
    /**
31
     * @return EventDispatcherInterface
32
     */
33 1
    public function getEventDispatcher()
34
    {
35 1
        return $this->eventDispatcher;
36
    }
37
38
    /**
39
     * @param FactoryInterface $factory
40
     */
41 9
    public function setFactory($factory)
42
    {
43 9
        $this->factory = $factory;
44 9
    }
45
46
    /**
47
     * @return \Knp\Menu\FactoryInterface
48
     */
49 9
    public function getFactory()
50
    {
51 9
        return $this->factory;
52
    }
53
54
    /**
55
     * @return ItemInterface
56
     */
57 8
    public function createMainMenu()
58
    {
59 8
        $menu = $this->factory->createItem('root');
60 8
        $menu->setChildrenAttribute('class', 'nav navbar-nav');
61
62 8
        $this->eventDispatcher->dispatch(
63 8
            ConfigureMenuEvent::EVENT_NAME,
64 8
            new ConfigureMenuEvent(
65 8
                $this->getFactory(),
66 8
                $menu
67
            )
68
        );
69
70 8
        $this->reorderMenuItems($menu);
71
72 8
        return $menu;
73
    }
74
75
    /**
76
     * Reorderd the items in the menu based on the extra data
77
     *
78
     * @param ItemInterface $menu
79
     */
80 8
    protected function reorderMenuItems(ItemInterface $menu)
81
    {
82 8
        $menuOrderArray = [];
83 8
        $addLast = [];
84 8
        $alreadyTaken = [];
85
86 8
        foreach ($menu->getChildren() as $menuItem) {
87
            if ($menuItem->hasChildren()) {
88
                $this->reorderMenuItems($menuItem);
89
            }
90
91
            $orderNumber = $menuItem->getExtra('orderNumber');
92
93
            if ($orderNumber !== null) {
94
                if (!isset($menuOrderArray[$orderNumber])) {
95
                    $menuOrderArray[$orderNumber] = $menuItem->getName();
96
                } else {
97
                    $alreadyTaken[$orderNumber] = $menuItem->getName();
98
                }
99
            } else {
100
                $addLast[] = $menuItem->getName();
101
            }
102
        }
103
104 8
        ksort($menuOrderArray);
105
106 8
        if (!empty($alreadyTaken)) {
107
            foreach ($alreadyTaken as $key => $value) {
108
                $keysArray = array_keys($menuOrderArray);
109
                $position = array_search($key, $keysArray);
110
111
                if ($position === false) {
112
                    continue;
113
                }
114
115
                $menuOrderArray = array_merge(
116
                    array_slice($menuOrderArray, 0, $position),
117
                    [$value],
118
                    array_slice($menuOrderArray, $position)
119
                );
120
            }
121
        }
122
123 8
        ksort($menuOrderArray);
124
125 8
        if (!empty($addLast)) {
126
            foreach ($addLast as $value) {
127
                $menuOrderArray[] = $value;
128
            }
129
        }
130
131 8
        if (!empty($menuOrderArray)) {
132
            $menu->reorderChildren($menuOrderArray);
133
        }
134 8
    }
135
}
136