SelfArgumentResolver::doResolve()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 0
loc 25
ccs 17
cts 17
cp 1
crap 6
rs 9.0777
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Diezz\YamlToObjectMapper\Resolver;
4
5
class SelfArgumentResolver extends CustomArgumentResolver
6
{
7
    private ScalarArgumentResolver $path;
8
    private array $visited = [];
9
10
    /**
11
     * @param ScalarArgumentResolver $path
12
     */
13 6
    public function __construct(ScalarArgumentResolver $path)
14
    {
15 6
        $this->path = $path;
16 6
    }
17
18 2
    public function setVisited(array $visited): void
19
    {
20 2
        $this->visited = $visited;
21 2
    }
22
23
    /**
24
     * @throws ArgumentResolverException
25
     * @var Context $context
26
     */
27 6
    protected function doResolve($context = null)
28
    {
29 6
        $result = $context->getMappingConfig();
30 6
        $path = $this->getPath($context);
31 6
        $pathRep = implode('.', $path);
32
33 6
        foreach ($path as $item) {
34 6
            $resolver = $result->findByPath($item);
35 6
            if ($resolver instanceof SystemArgumentResolver) {
36 2
                $result = $resolver;
37 6
            } else if ($resolver instanceof CustomArgumentResolver) {
38 5
                if ($resolver instanceof self) {
39 2
                    if (in_array($item, $this->visited, true)) {
40 2
                        throw new CircularDependencyException($this->visited);
41
                    }
42 2
                    $this->visited[] = $item;
43 2
                    $resolver->setVisited($this->visited);
44
                }
45 5
                $result = $resolver->resolve($context);
46
            } else {
47 1
                throw new ArgumentResolverException("Path '$pathRep' couldn't be resolved");
48
            }
49
        }
50
51 3
        return $result;
52
    }
53
54 6
    private function getPath($context): array
55
    {
56 6
        return explode(".", $this->path->resolve($context));
57
    }
58
}
59