Completed
Pull Request — master (#13)
by Arnaud
05:33 queued 02:50
created

MenuBuilder::topMenu()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.0164

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 39
ccs 21
cts 23
cp 0.913
rs 8.439
cc 5
eloc 25
nc 6
nop 1
crap 5.0164
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 1
    public function __construct(
44
        array $menusConfiguration,
45
        MenuFactory $menuFactory,
46
        AdminFactory $adminFactory,
47
        RequestStack $requestStack
48
    ) {
49 1
        $this->menusConfiguration = $menusConfiguration;
50 1
        $this->menuFactory = $menuFactory;
51 1
        $this->adminFactory = $adminFactory;
52 1
        $this->requestStack = $requestStack;
53 1
    }
54
55
    /**
56
     * Create main menu.
57
     *
58
     * @param array $options
59
     * @return ItemInterface
60
     */
61 1
    public function mainMenu(array $options = [])
62
    {
63 1
        if (!array_key_exists('main', $this->menusConfiguration)) {
64
            $this->menusConfiguration['main'] = [];
65
        }
66 1
        $this->menusConfiguration['main']['attr'] = [
67 1
            'id' => 'side-menu',
68 1
            'class' => 'nav in',
69
        ];
70 1
        $entity = null;
71
72 1
        if (array_key_exists('entity', $options)) {
73
            $entity = $options['entity'];
74
        }
75
76 1
        return $this
77
            ->menuFactory
78 1
            ->create('main', $this->menusConfiguration['main'], $entity);
79
    }
80
81
    /**
82
     * Create dynamic top menu.
83
     *
84
     * @param array $options
85
     * @return ItemInterface
86
     * @throws \Exception
87
     */
88 1
    public function topMenu(array $options = [])
89
    {
90
        // get current request
91 1
        $request = $this
92
            ->requestStack
93 1
            ->getCurrentRequest();
94 1
        $menusConfiguration = [];
95
96 1
        if ($request === null || empty($request->get('_route_params')['_admin'])) {
97 1
            $menusConfiguration['top'] = [];
98 1
        } else {
99
            // get current action from admin
100 1
            $action = $this
101
                ->adminFactory
102 1
                ->getAdminFromRequest($request)
103 1
                ->getCurrentAction();
104
105
            // menu configuration
106
            $menusConfiguration = $action
107 1
                ->getConfiguration()
108 1
                ->getParameter('menus');
109
110 1
            if (!array_key_exists('top', $menusConfiguration)) {
111 1
                $menusConfiguration['top'] = [];
112 1
            }
113 1
            $menusConfiguration['top']['attr'] = [
114 1
                'class' => 'nav navbar-top-links navbar-right in',
115
            ];
116
        }
117 1
        $entity = null;
118
119 1
        if (array_key_exists('entity', $options)) {
120
            $entity = $options['entity'];
121
        }
122
123 1
        return $this
124
            ->menuFactory
125 1
            ->create('top', $menusConfiguration['top'], $entity);
126
    }
127
}
128