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

MenuConfiguration::configureOptions()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 20
rs 9.8666
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('The menu items should an array of "%s" for menu "%s"', MenuItemConfiguration::class, $this->menuName));
44
                    }
45
                }
46
47
                return $value;
48
            })
49
        ;
50
    }
51
52
    public function all()
53
    {
54
        $values = parent::all();
55
56
        /** @var MenuItemConfiguration $value */
57
        foreach ($values['children'] as $name => $value) {
58
            $values['children'][$name] = $value->all();
59
        }
60
61
        return $values;
62
    }
63
64
    public function getMenuName(): string
65
    {
66
        return $this->menuName;
67
    }
68
69
    public function getRoute(): string
70
    {
71
        return $this->parameters->get('route');
72
    }
73
}
74