Passed
Pull Request — master (#138)
by Arnaud
08:18 queued 05:22
created

MenuConfiguration::getMenuName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Configuration;
4
5
use JK\Configuration\Configuration;
6
use Symfony\Component\OptionsResolver\Options;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class MenuConfiguration extends Configuration
10
{
11
    /**
12
     * @var string
13
     */
14
    private $menuName;
15
16
    /**
17
     * @var string
18
     */
19
    private $applicationName;
20
21
    /**
22
     * MenuConfiguration constructor.
23
     */
24
    public function __construct(string $menuName, string $applicationName)
25
    {
26
        parent::__construct();
27
28
        $this->menuName = $menuName;
29
        $this->applicationName = $applicationName;
30
    }
31
32
    public function configureOptions(OptionsResolver $resolver)
33
    {
34
        $resolver
35
            ->setDefaults([
36
                'items' => [],
37
                'attr' => [],
38
                'position' => null,
39
                'css_class' => 'list-group nav flex-column navbar-nav menu-'.$this->menuName,
40
                'item_css_class' => '',
41
                'link_css_class' => 'nav-link',
42
                'template' => '',
43
                'brand' => null,
44
            ])
45
            ->setAllowedValues('position', [
46
                'horizontal',
47
                'vertical',
48
                null,
49
            ])
50
            ->setNormalizer('position', function (Options $options, $value) {
51
                if ('top' === $this->menuName && null === $value) {
52
                    $value = 'horizontal';
53
                }
54
55
                if ('left' === $this->menuName && null === $value) {
56
                    $value = 'vertical';
57
                }
58
59
                return $value;
60
            })
61
            ->setNormalizer('template', function (Options $options, $value) {
62
                // Define bootstrap navbar component template
63
                if ('horizontal' === $options->offsetGet('position')) {
64
                    $value = '@LAGAdmin/Menu/menu.horizontal.html.twig';
65
                }
66
67
                // Define bootstrap nav component template
68
                if ('vertical' === $options->offsetGet('position')) {
69
                    $value = '@LAGAdmin/Menu/menu.vertical.html.twig';
70
                }
71
72
                return $value;
73
            })
74
            ->setNormalizer('attr', function (Options $options, $value) {
75
                $position = $options->offsetGet('position');
76
77
                if (!key_exists('class', $value)) {
78
                    $value['class'] = '';
79
                }
80
81
                if ('horizontal' === $position) {
82
                    $value['class'] .= ' navbar navbar-expand-lg navbar-dark bg-dark fixed-top';
83
                }
84
85
                if ('vertical' === $position) {
86
                    $value['class'] .= ' list-group';
87
                }
88
                $value['class'] = trim($value['class']);
89
90
                // Do not return an array with an empty string as css class
91
                if ('' === $value['class']) {
92
                    unset($value['class']);
93
                }
94
95
                return $value;
96
            })
97
            ->setNormalizer('item_css_class', function (Options $options, $value) {
98
                $position = $options->offsetGet('position');
99
100
                if (!$value) {
101
                    $value = '';
102
                }
103
104
                if ('horizontal' === $position) {
105
                    $value .= ' navbar-nav mr-auto';
106
                }
107
108
                if ('vertical' === $position) {
109
                    $value .= ' list-group-item list-group-item-action';
110
                }
111
112
                return trim($value);
113
            })
114
            ->setNormalizer('brand', function (Options $options, $value) {
115
                if (null === $value && 'horizontal' === $options->offsetGet('position')) {
116
                    $value = $this->applicationName;
117
                }
118
119
                return $value;
120
            })
121
        ;
122
    }
123
124
    public function getMenuName(): string
125
    {
126
        return $this->menuName;
127
    }
128
129
    public function getRoute(): string
130
    {
131
        return $this->parameters->get('route');
132
    }
133
}
134