Completed
Push — refonte ( c517e2...35a59a )
by Arnaud
02:26
created

MenuConfiguration::configureOptions()   C

Complexity

Conditions 13
Paths 1

Size

Total Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 5.7078
c 0
b 0
f 0
cc 13
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
    public function __construct(string $menuName)
17
    {
18
        $this->menuName = $menuName;
19
20
        parent::__construct();
21
    }
22
23
    public function configureOptions(OptionsResolver $resolver)
24
    {
25
        $resolver
26
            ->setDefaults([
27
                'items' => [],
28
                'attr' => [],
29
                'position' => null,
30
                'css_class' => 'list-group nav flex-column navbar-nav menu-'.$this->menuName,
31
                'item_css_class' => '',
32
                //list-group-item list-group-item-action',
33
                'link_css_class' => 'nav-link',
34
                'template' => '',
35
                'brand' => false,
36
            ])
37
            ->setAllowedValues('position', [
38
                'horizontal',
39
                'vertical',
40
                null
41
            ])
42
            ->setNormalizer('position', function (Options $options, $value) {
43
                if ('top' === $this->menuName && null === $value) {
44
                    $value = 'horizontal';
45
                }
46
47
                if ('left' === $this->menuName && null === $value) {
48
                    $value = 'vertical';
49
                }
50
51
                return $value;
52
            })
53
            ->setNormalizer('template', function (Options $options, $value) {
54
                // Define bootstrap navbar component template
55
                if ('horizontal' === $options->offsetGet('position')) {
56
                    $value = '@LAGAdmin/Menu/menu.horizontal.html.twig';
57
58
                }
59
60
                // Define bootstrap nav component template
61
                if ('vertical' === $options->offsetGet('position')) {
62
                    $value ='@LAGAdmin/Menu/menu.vertical.html.twig';
63
                }
64
65
                return $value;
66
            })
67
            ->setNormalizer('attr', function (Options $options, $value) {
68
                $position = $options->offsetGet('position');
69
70
                if (!key_exists('class', $value)) {
71
                    $value['class'] = '';
72
                }
73
74
                if ('horizontal' === $position) {
75
                    $value['class'] .= ' navbar navbar-expand-lg navbar-dark bg-dark fixed-top';
76
                }
77
78
                if ('vertical' === $position) {
79
                    $value['class'] .= ' list-group';
80
                }
81
                $value['class'] = trim($value['class']);
82
83
                return $value;
84
            })
85
            ->setNormalizer('item_css_class', function (Options $options, $value) {
86
                $position = $options->offsetGet('position');
87
88
                if (!$value) {
89
                    $value = '';
90
                }
91
92
                if ('horizontal' === $position) {
93
                    $value .= ' ';
94
                }
95
96
                if ('vertical' === $position) {
97
                    $value .= ' list-group-item list-group-item-action';
98
                }
99
100
                return trim($value);
101
            })
102
        ;
103
    }
104
}
105