Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

MenuItemConfiguration::configureOptions()   C

Complexity

Conditions 14
Paths 1

Size

Total Lines 80
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 46
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 80
ccs 0
cts 63
cp 0
crap 210
rs 6.2666

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\Component\StringUtils\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 $name;
17
18
    public function __construct(string $name)
19
    {
20
        $this->name = $name;
21
22
        parent::__construct();
23
    }
24
25
    /**
26
     * Define allowed parameters and values for this configuration, using optionsResolver component.
27
     */
28
    public function configureOptions(OptionsResolver $resolver)
29
    {
30
        // user can defined an admin name
31
        $resolver
32
            ->setDefaults([
33
                'admin' => null,
34
                'action' => null,
35
                'route' => null,
36
                'route_parameters' => [],
37
                'uri' => null,
38
                'attributes' => [],
39
                'linkAttributes' => [
40
                    'class' => 'list-group-item list-group-item-action',
41
                ],
42
                'text' => null,
43
                'children' => [],
44
            ])
45
            ->setNormalizer('admin', function (Options $options, $adminName) {
46
                // user has to defined either an admin name and an action name, or a route name with optional
47
                // parameters, or an url
48
                if (null === $adminName
49
                    && null === $options->offsetGet('route')
50
                    && null === $options->offsetGet('uri')
51
                ) {
52
                    throw new InvalidOptionsException('You should either defined an admin name, or route name or an uri');
53
                }
54
55
                return $adminName;
56
            })
57
            // if an admin name is set, an action name can provided. This action will be the menu link
58
            ->setNormalizer('action', function (Options $options, $action) {
59
                // if an action name is provided, an admin name should be defined too
60
                if (null !== $action && null === $options->offsetGet('admin')) {
61
                    throw new InvalidOptionsException('You should provide an admin name for this action '.$action);
62
                }
63
64
                // default to list action
65
                if (null !== $options->offsetGet('admin') && null === $action) {
66
                    $action = 'list';
67
                }
68
69
                return $action;
70
            })
71
            ->setNormalizer('children', function (Options $options, $items) {
72
                if (!is_array($items)) {
73
                    $items = [];
74
                }
75
                $resolver = new OptionsResolver();
76
                $resolvedItems = [];
77
78
                foreach ($items as $name => $item) {
79
                    $itemConfiguration = new MenuItemConfiguration($name);
80
                    $itemConfiguration->configureOptions($resolver);
81
                    $itemConfiguration->setParameters($resolver->resolve($item));
82
83
                    $resolvedItems[$name] = $itemConfiguration;
84
                }
85
86
                return $resolvedItems;
87
            })
88
            ->setNormalizer('text', function (Options $options, $text) {
89
                if ($text) {
90
                    return $text;
91
                }
92
93
                if ($options->offsetGet('admin')) {
94
                    $text = ucfirst($options->offsetGet('admin'));
95
96
                    if (StringUtils::endsWith($text, 'y')) {
97
                        $text = substr($text, 0, strlen($text) - 1).'ie';
98
                    }
99
100
                    if (!StringUtils::endsWith($text, 's')) {
101
                        $text .= 's';
102
                    }
103
104
                    return $text;
105
                }
106
107
                return ucfirst($this->name);
108
            })
109
        ;
110
    }
111
112
    public function all()
113
    {
114
        $values = parent::all();
115
116
        /** @var MenuItemConfiguration $value */
117
        foreach ($values['children'] as $name => $value) {
118
            $values['children'][$name] = $value->all();
119
        }
120
121
        return $values;
122
    }
123
124
    public function getName(): string
125
    {
126
        return $this->name;
127
    }
128
}
129