Completed
Pull Request — dev (#19)
by Arnaud
14:27 queued 06:29
created

MenuBuilder::topMenu()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 8.5806
cc 4
eloc 22
nc 3
nop 0
crap 4
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
     * @return ItemInterface
59
     */
60 1
    public function mainMenu()
61
    {
62 1
        if (!array_key_exists('main', $this->menusConfiguration)) {
63
            $this->menusConfiguration['main'] = [];
64
        }
65 1
        $this->menusConfiguration['main']['attr'] = [
66
            'id' => 'side-menu',
67
            'class' => 'nav in',
68
        ];
69
70
        return $this
71 1
            ->menuFactory
72 1
            ->create('main', $this->menusConfiguration['main']);
73
    }
74
75
    /**
76
     * Create dynamic top menu.
77
     *
78
     * @return ItemInterface
79
     * @throws \Exception
80
     */
81 1
    public function topMenu()
82
    {
83
        // get current request
84
        $request = $this
85 1
            ->requestStack
86 1
            ->getCurrentRequest();
87 1
        $menusConfiguration = [];
88
89 1
        if ($request === null || empty($request->get('_route_params')['_admin'])) {
90 1
            $menusConfiguration['top'] = [];
91
        } else {
92
            // get current action from admin
93
            $action = $this
94 1
                ->adminFactory
95 1
                ->getAdminFromRequest($request)
96 1
                ->getCurrentAction();
97
98
            // menu configuration
99
            $menusConfiguration = $action
100 1
                ->getConfiguration()
101 1
                ->getParameter('menus');
102
103 1
            if (!array_key_exists('top', $menusConfiguration)) {
104 1
                $menusConfiguration['top'] = [];
105
            }
106 1
            $menusConfiguration['top']['attr'] = [
107
                'class' => 'nav navbar-top-links navbar-right in',
108
            ];
109
        }
110
111
        return $this
112 1
            ->menuFactory
113 1
            ->create('top', $menusConfiguration['top']);
114
    }
115
}
116