Completed
Pull Request — master (#154)
by Arnaud
03:37
created

MenuItemConfiguration::configureOptions()   C

Complexity

Conditions 14
Paths 1

Size

Total Lines 82
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 14.5378

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