Completed
Push — refonte ( 49f4cc...3d42cd )
by Arnaud
02:34
created

MenuConfiguration::configureOptions()   D

Complexity

Conditions 16
Paths 1

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 4.6278
c 0
b 0
f 0
cc 16
nc 1
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
     * @param string $menuName
25
     * @param string $applicationName
26
     */
27
    public function __construct(string $menuName, string $applicationName)
28
    {
29
        parent::__construct();
30
31
        $this->menuName = $menuName;
32
        $this->applicationName = $applicationName;
33
    }
34
35
    /**
36
     * @param OptionsResolver $resolver
37
     */
38
    public function configureOptions(OptionsResolver $resolver)
39
    {
40
        $resolver
41
            ->setDefaults([
42
                'items' => [],
43
                'attr' => [],
44
                'position' => null,
45
                'css_class' => 'list-group nav flex-column navbar-nav menu-'.$this->menuName,
46
                'item_css_class' => '',
47
                'link_css_class' => 'nav-link',
48
                'template' => '',
49
                'brand' => null,
50
            ])
51
            ->setAllowedValues('position', [
52
                'horizontal',
53
                'vertical',
54
                null
55
            ])
56
            ->setNormalizer('position', function (Options $options, $value) {
57
                if ('top' === $this->menuName && null === $value) {
58
                    $value = 'horizontal';
59
                }
60
61
                if ('left' === $this->menuName && null === $value) {
62
                    $value = 'vertical';
63
                }
64
65
                return $value;
66
            })
67
            ->setNormalizer('template', function (Options $options, $value) {
68
                // Define bootstrap navbar component template
69
                if ('horizontal' === $options->offsetGet('position')) {
70
                    $value = '@LAGAdmin/Menu/menu.horizontal.html.twig';
71
72
                }
73
74
                // Define bootstrap nav component template
75
                if ('vertical' === $options->offsetGet('position')) {
76
                    $value ='@LAGAdmin/Menu/menu.vertical.html.twig';
77
                }
78
79
                return $value;
80
            })
81
            ->setNormalizer('attr', function (Options $options, $value) {
82
                $position = $options->offsetGet('position');
83
84
                if (!key_exists('class', $value)) {
85
                    $value['class'] = '';
86
                }
87
88
                if ('horizontal' === $position) {
89
                    $value['class'] .= ' navbar navbar-expand-lg navbar-dark bg-dark fixed-top';
90
                }
91
92
                if ('vertical' === $position) {
93
                    $value['class'] .= ' list-group';
94
                }
95
                $value['class'] = trim($value['class']);
96
97
                // Do not return an array with an empty string as css class
98
                if ('' === $value['class']) {
99
                    unset($value['class']);
100
                }
101
102
                return $value;
103
            })
104
            ->setNormalizer('item_css_class', function (Options $options, $value) {
105
                $position = $options->offsetGet('position');
106
107
                if (!$value) {
108
                    $value = '';
109
                }
110
111
                if ('horizontal' === $position) {
112
                    $value .= ' navbar-nav mr-auto';
113
                }
114
115
                if ('vertical' === $position) {
116
                    $value .= ' list-group-item list-group-item-action';
117
                }
118
119
                return trim($value);
120
            })
121
            ->setNormalizer('brand', function (Options $options, $value) {
122
                if (null === $value && 'horizontal' === $options->offsetGet('position')) {
123
                    $value = $this->applicationName;
124
                }
125
126
                return $value;
127
            })
128
        ;
129
    }
130
}
131