Completed
Pull Request — master (#154)
by Arnaud
04:40
created

MenuConfiguration::configureOptions()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 20
rs 9.7666
1
<?php
2
3
namespace LAG\AdminBundle\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Exception\Exception;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
class MenuConfiguration extends Configuration
11
{
12
    /**
13
     * @var string
14
     */
15
    private $menuName;
16
17
    /**
18
     * MenuConfiguration constructor.
19
     */
20
    public function __construct(string $menuName)
21
    {
22
        parent::__construct();
23
24
        $this->menuName = $menuName;
25
    }
26
27
    public function configureOptions(OptionsResolver $resolver)
28
    {
29
        $resolver
30
            ->setDefaults([
31
                'children' => [],
32
                'attributes' => [
33
                    'class' => 'list-group cms-menu-'.$this->menuName,
34
                ],
35
            ])
36
            ->setNormalizer('children', function (Options $options, $value) {
37
                if (!is_array($value)) {
38
                    throw new Exception('The menu items should an array for menu "'.$this->menuName.'"');
39
                }
40
41
                foreach ($value as $item) {
42
                    if (!$item instanceof MenuItemConfiguration) {
43
                        throw new Exception(sprintf(
44
                            'The menu items should an array of "%s" for menu "%s"',
45
                            MenuItemConfiguration::class,
46
                            $this->menuName)
47
                        );
48
                    }
49
                }
50
51
                return $value;
52
            })
53
        ;
54
    }
55
56
    public function all()
57
    {
58
        $values = parent::all();
59
60
        /** @var MenuItemConfiguration $value */
61
        foreach ($values['children'] as $name => $value) {
62
            $values['children'][$name] = $value->all();
63
        }
64
65
        return $values;
66
    }
67
68
    public function getMenuName(): string
69
    {
70
        return $this->menuName;
71
    }
72
73
    public function getRoute(): string
74
    {
75
        return $this->parameters->get('route');
76
    }
77
}
78