Completed
Pull Request — dev (#19)
by Arnaud
02:52
created

MenuBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 6
c 2
b 0
f 2
lcom 1
cbo 6
dl 0
loc 102
ccs 0
cts 50
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A mainMenu() 0 14 2
B topMenu() 0 33 3
1
<?php
2
3
namespace LAG\AdminBundle\Menu;
4
5
use Knp\Menu\ItemInterface;
6
use LAG\AdminBundle\Admin\Factory\AdminFactory;
7
use LAG\AdminBundle\Menu\Factory\MenuFactory;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
10
/**
11
 * Create menu handled by the AdminBundle (main, top)
12
 */
13
class MenuBuilder
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $menusConfiguration;
19
20
    /**
21
     * @var MenuFactory
22
     */
23
    protected $menuFactory;
24
25
    /**
26
     * @var AdminFactory
27
     */
28
    protected $adminFactory;
29
30
    /**
31
     * @var RequestStack
32
     */
33
    protected $requestStack;
34
35
    /**
36
     * MenuBuilder constructor.
37
     *
38
     * @param array $menusConfiguration
39
     * @param MenuFactory $menuFactory
40
     * @param AdminFactory $adminFactory
41
     * @param RequestStack $requestStack
42
     */
43
    public function __construct(
44
        array $menusConfiguration,
45
        MenuFactory $menuFactory,
46
        AdminFactory $adminFactory,
47
        RequestStack $requestStack
48
    ) {
49
        $this->menusConfiguration = $menusConfiguration;
50
        $this->menuFactory = $menuFactory;
51
        $this->adminFactory = $adminFactory;
52
        $this->requestStack = $requestStack;
53
    }
54
55
    /**
56
     * Create main menu.
57
     *
58
     * @return ItemInterface
59
     */
60
    public function mainMenu()
61
    {
62
        if (!array_key_exists('main', $this->menusConfiguration)) {
63
            $this->menusConfiguration['main'] = [];
64
        }
65
        $this->menusConfiguration['main']['attr'] = [
66
            'id' => 'side-menu',
67
            'class' => 'nav in',
68
        ];
69
70
        return $this
71
            ->menuFactory
72
            ->create('main', $this->menusConfiguration['main']);
73
    }
74
75
    /**
76
     * Create dynamic top menu.
77
     *
78
     * @return ItemInterface
79
     * @throws \Exception
80
     */
81
    public function topMenu()
82
    {
83
        // get current request
84
        $request = $this
85
            ->requestStack
86
            ->getCurrentRequest();
87
88
        if (empty($request->get('_route_params')['_admin'])) {
89
            $menusConfiguration['top'] = [];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$menusConfiguration was never initialized. Although not strictly required by PHP, it is generally a good practice to add $menusConfiguration = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
90
        } else {
91
            // get current action from admin
92
            $action = $this
93
                ->adminFactory
94
                ->getAdminFromRequest($request)
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 84 can be null; however, LAG\AdminBundle\Admin\Fa...::getAdminFromRequest() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
95
                ->getCurrentAction();
96
97
            // menu configuration
98
            $menusConfiguration = $action
99
                ->getConfiguration()
100
                ->getParameter('menus');
101
102
            if (!array_key_exists('top', $menusConfiguration)) {
103
                $menusConfiguration['top'] = [];
104
            }
105
            $menusConfiguration['top']['attr'] = [
106
                'class' => 'nav navbar-top-links navbar-right in',
107
            ];
108
        }
109
110
        return $this
111
            ->menuFactory
112
            ->create('top', $menusConfiguration['top']);
113
    }
114
}
115