ConfigResolver::configure()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 7
cts 10
cp 0.7
crap 4.432
rs 9.9332
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Config;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
class ConfigResolver
8
{
9
    /** @var OptionsResolver */
10
    private $resolver;
11
    /** @var ConfigResolver[] */
12
    private $children = [];
13 8
    public function __construct(ConfigInterface $configuration)
14
    {
15 8
        $this->resolver = new OptionsResolver();
16 8
        $this->configure($configuration);
17 8
    }
18 8
    private function configure($configuration)
19
    {
20 8
        $configuration->configureOptions($this->resolver);
21 8
        foreach ($configuration::getChildren() as $key => $childrenClass) {
22 8
            $children = new $childrenClass();
23 8
            if (!$children instanceof ConfigInterface) {
24
                $message = sprintf("The children must implements interface %s", ConfigInterface::class);
25
                throw new \Exception($message);
26
            }
27 8
            if ($children instanceof ConfigTreeInterface) {
28 8
                $this->children[$key] = new ConfigTreeResolver($children);
29
            } else {
30
                $this->children[$key] = new ConfigResolver($children);
31
            }
32
        }
33 8
    }
34 8
    public function resolve($data)
35
    {
36 8
        $resolved = $this->resolver->resolve($data);
37 8
        foreach ($this->children as $key => $children) {
38 8
            if ($children instanceof ConfigTreeResolver) {
39 8
                foreach ($resolved[$key] as $subKey => $subData) {
40 8
                    $resolved[$key][$subKey] = $children->resolve($subData);
41
                }
42
            } else {
43
                $resolved[$key] = $children->resolve($resolved[$key]);
44
            }
45
        }
46 8
        return $resolved;
47
    }
48
}
49