Completed
Push — master ( cc6520...55dbed )
by Łukasz
14s
created

AbstractElement::resolveOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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
namespace FSi\Bundle\AdminBundle\Admin;
11
12
use FSi\Bundle\AdminBundle\Exception\MissingOptionException;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
/**
16
 * @author Norbert Orzechowicz <[email protected]>
17
 */
18
abstract class AbstractElement implements Element
19
{
20
    /**
21
     * @var array
22
     */
23
    private $options;
24
25
    /**
26
     * @var array
27
     */
28
    private $unresolvedOptions;
29
30
    /**
31
     * @param array $options
32
     */
33
    public function __construct(array $options = [])
34
    {
35
        $this->unresolvedOptions = $options;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getRouteParameters()
42
    {
43
        return [
44
            'element' => $this->getId(),
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getOption($name)
52
    {
53
        $this->resolveOptions();
54
        if (!$this->hasOption($name)) {
55
            throw new MissingOptionException(sprintf(
56
                'Option with name "%s" does not exist in element "%s"',
57
                $name,
58
                get_class($this)
59
            ));
60
        }
61
62
        return $this->options[$name];
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getOptions()
69
    {
70
        $this->resolveOptions();
71
        return $this->options;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function hasOption($name)
78
    {
79
        $this->resolveOptions();
80
        return isset($this->options[$name]);
81
    }
82
83
    private function resolveOptions()
84
    {
85
        if (!is_array($this->options)) {
86
            $optionsResolver = new OptionsResolver();
87
            $this->setDefaultOptions($optionsResolver);
88
            $this->options = $optionsResolver->resolve($this->unresolvedOptions);
89
            unset($this->unresolvedOptions);
90
        }
91
    }
92
}
93