Completed
Branch develop (598d0f)
by Benjamin
03:23
created

ConfigResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 13 4
A resolve() 0 14 4
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Config;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class ConfigResolver {
8
    /** @var OptionsResolver */
9
    private $resolver;
10
    /** @var ConfigResolver[] */
11
    private $children = [];
12
    public function __construct(ConfigInterface $configuration)
13
    {
14
        $this->resolver = new OptionsResolver();
15
        $this->configure($configuration);
16
    }
17
    private function configure($configuration) {
18
        $configuration->configureOptions($this->resolver);
19
        foreach ($configuration::getChildren() as $key => $children_class) {
20
            $children = new $children_class();
21
            if(!$children instanceof ConfigInterface) {
22
                $message = sprintf("The children must implements interface %s", ConfigInterface::class);
23
                throw new \Exception($message);
24
            }
25
            if($children instanceof ConfigTreeInterface) {
26
                $this->children[$key] = new ConfigTreeResolver($children);
27
            }
28
            else {
29
                $this->children[$key] = new ConfigResolver($children);
30
            }
31
        }
32
    }
33
    public function resolve($data)
34
    {
35
        $resolved = $this->resolver->resolve($data);
36
        foreach ($this->children as $key => $children) {
37
            if($children instanceof ConfigTreeResolver) {
38
                foreach ($resolved[$key] as $sub_key => $sub_data) {
39
                    $resolved[$key][$sub_key] = $children->resolve($sub_data);
40
                }
41
            }
42
            else {
43
                $resolved[$key] = $children->resolve($resolved[$key]);
44
            }
45
        }
46
        return $resolved;
47
    }
48
}
49