OptionDynamicValueResolver::resolve()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 8
nop 2
dl 0
loc 24
ccs 16
cts 16
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Darkilliant\ProcessBundle\Resolver;
6
7
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
8
9
/**
10
 * @internal
11
 */
12
class OptionDynamicValueResolver
13
{
14
    /** @var PropertyAccessorInterface */
15
    private $accessor;
16
17 1
    public function __construct(PropertyAccessorInterface $accessor)
18
    {
19 1
        $this->accessor = $accessor;
20 1
    }
21
22 1
    public function resolve(array $options, array $context): array
23
    {
24 1
        foreach ($options as $optionName => $optionValue) {
25 1
            if (is_array($optionValue)) {
26 1
                $options[$optionName] = $this->resolve($optionValue, $context);
27 1
            } elseif (!is_string($optionValue)) {
28 1
                continue;
29 1
            } elseif (false !== strpos($optionValue, '@!')) {
30 1
                $key = substr($optionValue, 2);
31 1
                list($firstLevel, $secondLevel) = explode('->', $key);
32
33 1
                if (isset($context[$firstLevel][$secondLevel])) {
34 1
                    $options[$optionName] = $context[$firstLevel][$secondLevel];
35
                }
36 1
            } elseif (false !== strpos($optionValue, '@')) {
37 1
                $key = substr($optionValue, 1);
38
39 1
                if ($this->accessor->isReadable($context, $key)) {
40 1
                    $options[$optionName] = $this->accessor->getValue($context, $key);
41
                }
42
            }
43
        }
44
45 1
        return $options;
46
    }
47
}
48