Passed
Pull Request — master (#317)
by Łukasz
02:39
created

ElementItem::configureOptions()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 1
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\Menu\Item;
13
14
use FSi\Bundle\AdminBundle\Admin\Element;
15
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
16
use Symfony\Component\OptionsResolver\Options;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
class ElementItem extends RoutableItem
20
{
21
    /**
22
     * @var Element
23
     */
24
    private $element;
25
26
    public function __construct(string $name, Element $element)
27
    {
28
        parent::__construct($name);
29
30
        $this->element = $element;
31
    }
32
33
    public function getElement(): Element
34
    {
35
        return $this->element;
36
    }
37
38
    public function getRoute(): string
39
    {
40
        return $this->element->getRoute();
41
    }
42
43
    public function getRouteParameters(): array
44
    {
45
        return $this->element->getRouteParameters();
46
    }
47
48
    protected function configureOptions(OptionsResolver $optionsResolver): void
49
    {
50
        parent::configureOptions($optionsResolver);
51
52
        $optionsResolver->setDefaults([
53
            'elements' => [],
54
        ]);
55
56
        $optionsResolver->setAllowedTypes('elements', ['array']);
57
58
        $optionsResolver->setNormalizer('elements', function (Options $options, array $value) {
59
            foreach ($value as $element) {
60
                if (!($element instanceof Element)) {
61
                    throw new InvalidOptionsException(sprintf(
62
                        'Instance of FSi\Bundle\AdminBundle\Admin\Element expected but got %s',
63
                        is_object($element) ? get_class($element) : gettype($element)
64
                    ));
65
                }
66
            }
67
68
            return $value;
69
        });
70
    }
71
}
72