Completed
Pull Request — dev (#19)
by Arnaud
02:52
created

MenuConfiguration::configureOptions()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 42
Code Lines 22

Duplication

Lines 12
Ratio 28.57 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 42
ccs 0
cts 31
cp 0
rs 8.439
cc 5
eloc 22
nc 1
nop 1
crap 30
1
<?php
2
3
namespace LAG\AdminBundle\Menu\Configuration;
4
5
use LAG\AdminBundle\Configuration\Configuration;
6
use Symfony\Component\OptionsResolver\Options;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
class MenuConfiguration extends Configuration
10
{
11
    /**
12
     * Define allowed parameters and values for this configuration, using optionsResolver component.
13
     *
14
     * @param OptionsResolver $resolver
15
     */
16
    public function configureOptions(OptionsResolver $resolver)
17
    {
18
        // menu html attributes
19
        $resolver
20
            ->setDefault('attr', [])
21 View Code Duplication
            ->setNormalizer('attr', function(Options $options, $attr) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
23
                if (!is_array($attr)) {
24
                    $attr = [];
25
                }
26
27
                if (empty($attr['id'])) {
28
                    $attr['id'] = uniqid('admin-menu-');
29
                }
30
31
                return $attr;
32
            })
33
        ;
34
35
        // menu item
36
        $resolver
37
            ->setDefault('items', [])
38
            ->setNormalizer('items', function(Options $options, $items) {
39
40
                if (!is_array($items)) {
41
                    $items = [];
42
                }
43
                $resolver = new OptionsResolver();
44
                $resolvedItems = [];
45
46
                foreach ($items as $name => $item) {
47
                    $itemConfiguration = new MenuItemConfiguration();
48
                    $itemConfiguration->configureOptions($resolver);
49
                    $itemConfiguration->setParameters($resolver->resolve($item));
50
51
                    $resolvedItems[$name] = $itemConfiguration;
52
                }
53
54
                return $resolvedItems;
55
            })
56
        ;
57
    }
58
}
59