Passed
Pull Request — master (#121)
by Arnaud
03:47
created

MenuItemConfiguration::configureOptions()   D

Complexity

Conditions 19
Paths 1

Size

Total Lines 107
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 62
nc 1
nop 1
dl 0
loc 107
rs 4.5166
c 0
b 0
f 0

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 LAG\AdminBundle\Utils\StringUtils;
7
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
class MenuItemConfiguration extends Configuration
12
{
13
    /**
14
     * @var ?string
15
     */
16
    private $position;
17
18
    /**
19
     * @var string
20
     */
21
    private $name;
22
23
    /**
24
     * MenuItemConfiguration constructor.
25
     *
26
     * @param string $name
27
     * @param string $position
28
     */
29
    public function __construct(string $name, ?string $position)
30
    {
31
        $this->position = $position;
32
        $this->name = $name;
33
34
        parent::__construct();
35
    }
36
37
    /**
38
     * Define allowed parameters and values for this configuration, using optionsResolver component.
39
     *
40
     * @param OptionsResolver $resolver
41
     */
42
    public function configureOptions(OptionsResolver $resolver)
43
    {
44
        // user can defined an admin name
45
        $resolver
46
            ->setDefault('admin', null)
47
            ->setDefault('action', null)
48
            ->setDefault('route', null)
49
            ->setDefault('url', null)
50
            ->setDefault('parameters', [])
51
            ->setDefault('text', '')
52
            ->setDefault('attr', [])
53
            ->setDefault('link_attr', [
54
                'class' => 'nav-link',
55
            ])
56
            ->setDefault('items', [])
57
            ->setDefault('icon', null)
58
            ->setDefault('link_css_class', 'nav-link')
59
            ->setNormalizer('admin', function (Options $options, $adminName) {
60
                // user has to defined either an admin name and an action name, or a route name with optional
61
                // parameters, or an url
62
                if (null === $adminName
63
                    && null === $options->offsetGet('route')
64
                    && null === $options->offsetGet('url')
65
                ) {
66
                    throw new InvalidOptionsException(
67
                        'You should either defined an admin name, or route name or an uri'
68
                    );
69
                }
70
71
                return $adminName;
72
            })
73
            // if an admin name is set, an action name can provided. This action will be the menu link
74
            ->setNormalizer('action', function (Options $options, $action) {
75
                // if an action name is provided, an admin name should be defined too
76
                if (null !== $action && null === $options->offsetGet('admin')) {
77
                    throw new InvalidOptionsException(
78
                        'You should provide an admin name for this action '.$action
79
                    );
80
                }
81
82
                // default to list action
83
                if (null !== $options->offsetGet('admin') && null === $action) {
84
                    $action = 'list';
85
                }
86
87
                return $action;
88
            })
89
            ->setAllowedTypes('parameters', 'array')
90
            ->setNormalizer('attr', function (Options $options, $attr) {
91
                if (!is_array($attr)) {
92
                    $attr = [];
93
                }
94
95
                if (empty($attr['id'])) {
96
                    $attr['id'] = uniqid('admin-menu-');
97
                }
98
99
                if ('horizontal' === $this->position && !key_exists('class', $attr)) {
100
                    $attr['class'] = 'nav-item';
101
                }
102
103
                return $attr;
104
            })
105
            ->setNormalizer('link_attr', function (Options $options, $value) {
106
                if (!is_array($value)) {
107
                    $value = [];
108
                }
109
110
                if (!key_exists('class', $value)) {
111
                    $value['class'] = 'nav-link';
112
                }
113
114
                return $value;
115
            })
116
            ->setNormalizer('items', function (Options $options, $items) {
117
                if (!is_array($items)) {
118
                    $items = [];
119
                }
120
                $resolver = new OptionsResolver();
121
                $resolvedItems = [];
122
123
                foreach ($items as $name => $item) {
124
                    $itemConfiguration = new MenuItemConfiguration($name, $this->position);
125
                    $itemConfiguration->configureOptions($resolver);
126
                    $itemConfiguration->setParameters($resolver->resolve($item));
127
128
                    $resolvedItems[$name] = $itemConfiguration;
129
                }
130
131
                return $resolvedItems;
132
            })
133
            ->setNormalizer('text', function (Options $options, $text) {
134
                if ($text) {
135
                    return $text;
136
                }
137
138
                if ($options->offsetGet('admin')) {
139
                    $text = ucfirst($options->offsetGet('admin'));
140
141
                    if (!StringUtils::endWith($text, 's')) {
142
                        $text .= 's';
143
                    }
144
145
                    return $text;
146
                }
147
148
                return ucfirst($this->name);
149
            })
150
        ;
151
    }
152
}
153