Completed
Pull Request — master (#90)
by Arnaud
17:45
created

MenuBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Menu;
4
5
use Exception;
6
use Knp\Menu\ItemInterface;
7
use LAG\AdminBundle\Admin\Request\RequestHandler;
8
use LAG\AdminBundle\Menu\Factory\MenuFactory;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
/**
12
 * Create menu handled by the AdminBundle (main, top)
13
 */
14
class MenuBuilder
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $menusConfiguration;
20
21
    /**
22
     * @var MenuFactory
23
     */
24
    protected $menuFactory;
25
26
    /**
27
     * @var RequestHandler
28
     */
29
    protected $requestHandler;
30
31
    /**
32
     * @var RequestStack
33
     */
34
    protected $requestStack;
35
36
    /**
37
     * MenuBuilder constructor.
38
     *
39
     * @param array $menusConfiguration
40
     * @param MenuFactory $menuFactory
41
     * @param RequestHandler $requestHandler
42
     * @param RequestStack $requestStack
43
     */
44
    public function __construct(
45
        array $menusConfiguration,
46
        MenuFactory $menuFactory,
47
        RequestHandler $requestHandler,
48
        RequestStack $requestStack
49
    ) {
50
        $this->menusConfiguration = $menusConfiguration;
51
        $this->menuFactory = $menuFactory;
52
        $this->requestHandler = $requestHandler;
53
        $this->requestStack = $requestStack;
54
    }
55
56
    /**
57
     * Create main menu.
58
     *
59
     * @param array $options
60
     * @return ItemInterface
61
     */
62
    public function mainMenu(array $options = [])
63
    {
64
        if (!array_key_exists('main', $this->menusConfiguration)) {
65
            $this->menusConfiguration['main'] = [];
66
        }
67
        $this->menusConfiguration['main']['attr'] = [
68
            'id' => 'side-menu',
69
            'class' => 'nav in',
70
        ];
71
        $entity = null;
72
73
        if (array_key_exists('entity', $options)) {
74
            $entity = $options['entity'];
75
        }
76
       
77
        return $this
78
            ->menuFactory
79
            ->create('main', $this->menusConfiguration['main'], $entity);
80
    }
81
82
    /**
83
     * Create dynamic top menu.
84
     *
85
     * @param array $options
86
     * @return ItemInterface
87
     * @throws Exception
88
     */
89
    public function topMenu(array $options = [])
90
    {
91
        // get current request
92
        $request = $this
93
            ->requestStack
94
            ->getCurrentRequest();
95
        $entity = null;
96
        $menusConfiguration = [];
97
        $menusConfiguration['top'] = [];
98
99
        // request should exists and should have admin parameters
100
        if ($request !== null && !empty($request->get('_route_params')['_admin'])) {
101
            // get current action from admin
102
            $admin = $this
103
                ->requestHandler
104
                ->handle($request)
105
            ;
106
107
            if ($admin->isCurrentActionDefined()) {
108
                // menu configuration
109
                $menusConfiguration = $admin
110
                    ->getCurrentAction()
111
                    ->getConfiguration()
112
                    ->getParameter('menus')
113
                ;
114
115
                if (!array_key_exists('top', $menusConfiguration)) {
116
                    $menusConfiguration['top'] = [];
117
                }
118
                $menusConfiguration['top']['attr'] = [
119
                    //'class' => 'nav navbar-top-links navbar-right in',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
                    'class' => 'navbar-nav bd-navbar-nav flex-row',
121
                ];
122
            }
123
        }
124
125
        if (array_key_exists('entity', $options)) {
126
            $entity = $options['entity'];
127
        }
128
129
        return $this
130
            ->menuFactory
131
            ->create('top', $menusConfiguration['top'], $entity)
132
            ;
133
    }
134
}
135