Completed
Pull Request — dev (#43)
by Arnaud
03:21
created

MenuBuilder::topMenu()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6.0184

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 41
ccs 23
cts 25
cp 0.92
rs 8.439
cc 6
eloc 25
nc 8
nop 1
crap 6.0184
1
<?php
2
3
namespace LAG\AdminBundle\Menu;
4
5
use Exception;
6
use Knp\Menu\ItemInterface;
7
use LAG\AdminBundle\Admin\Factory\AdminFactory;
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 AdminFactory
28
     */
29
    protected $adminFactory;
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 AdminFactory $adminFactory
42
     * @param RequestStack $requestStack
43
     */
44 1
    public function __construct(
45
        array $menusConfiguration,
46
        MenuFactory $menuFactory,
47
        AdminFactory $adminFactory,
48
        RequestStack $requestStack
49
    ) {
50 1
        $this->menusConfiguration = $menusConfiguration;
51 1
        $this->menuFactory = $menuFactory;
52 1
        $this->adminFactory = $adminFactory;
53 1
        $this->requestStack = $requestStack;
54 1
    }
55
56
    /**
57
     * Create main menu.
58
     *
59
     * @param array $options
60
     * @return ItemInterface
61
     */
62 1
    public function mainMenu(array $options = [])
63
    {
64 1
        if (!array_key_exists('main', $this->menusConfiguration)) {
65
            $this->menusConfiguration['main'] = [];
66
        }
67 1
        $this->menusConfiguration['main']['attr'] = [
68 1
            'id' => 'side-menu',
69 1
            'class' => 'nav in',
70
        ];
71 1
        $entity = null;
72
73 1
        if (array_key_exists('entity', $options)) {
74
            $entity = $options['entity'];
75
        }
76
77 1
        return $this
78
            ->menuFactory
79 1
            ->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 1
    public function topMenu(array $options = [])
90
    {
91
        // get current request
92 1
        $request = $this
93
            ->requestStack
94 1
            ->getCurrentRequest();
95 1
        $entity = null;
96 1
        $menusConfiguration = [];
97 1
        $menusConfiguration['top'] = [];
98
99
        // request should exists and should have admin parameters
100 1
        if ($request !== null && !empty($request->get('_route_params')['_admin'])) {
101
            // get current action from admin
102 1
            $admin = $this
103
                ->adminFactory
104 1
                ->getAdminFromRequest($request);
105
106 1
            if ($admin->isCurrentActionDefined()) {
107
                // menu configuration
108
                $menusConfiguration = $admin
109 1
                    ->getCurrentAction()
110 1
                    ->getConfiguration()
111 1
                    ->getParameter('menus');
112
113 1
                if (!array_key_exists('top', $menusConfiguration)) {
114 1
                    $menusConfiguration['top'] = [];
115 1
                }
116 1
                $menusConfiguration['top']['attr'] = [
117 1
                    'class' => 'nav navbar-top-links navbar-right in',
118
                ];
119 1
            }
120 1
        }
121
122 1
        if (array_key_exists('entity', $options)) {
123
            $entity = $options['entity'];
124
        }
125
126 1
        return $this
127
            ->menuFactory
128 1
            ->create('top', $menusConfiguration['top'], $entity);
129
    }
130
}
131