AbstractElement::getRouteParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Admin;
13
14
use FSi\Bundle\AdminBundle\Exception\MissingOptionException;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
17
abstract class AbstractElement implements Element
18
{
19
    /**
20
     * @var array
21
     */
22
    private $options;
23
24
    /**
25
     * @var array
26
     */
27
    private $unresolvedOptions;
28
29
    public function __construct(array $options = [])
30
    {
31
        $this->unresolvedOptions = $options;
32
    }
33
34
    public function getRouteParameters(): array
35
    {
36
        return [
37
            'element' => $this->getId(),
38
        ];
39
    }
40
41
    public function getOption(string $name)
42
    {
43
        $this->resolveOptions();
44
45
        if (!$this->hasOption($name)) {
46
            throw new MissingOptionException(sprintf(
47
                'Option with name "%s" does not exist in element "%s"',
48
                $name,
49
                get_class($this)
50
            ));
51
        }
52
53
        return $this->options[$name];
54
    }
55
56
    public function getOptions(): array
57
    {
58
        $this->resolveOptions();
59
60
        return $this->options;
61
    }
62
63
    public function hasOption($name): bool
64
    {
65
        $this->resolveOptions();
66
        return isset($this->options[$name]);
67
    }
68
69
    private function resolveOptions(): void
70
    {
71
        if (!is_array($this->options)) {
0 ignored issues
show
introduced by
The condition is_array($this->options) is always true.
Loading history...
72
            $optionsResolver = new OptionsResolver();
73
            $this->configureOptions($optionsResolver);
74
            $this->options = $optionsResolver->resolve($this->unresolvedOptions);
75
            unset($this->unresolvedOptions);
76
        }
77
    }
78
}
79