SelfArgumentResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 52
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A setVisited() 0 3 1
A doResolve() 0 25 6
A __construct() 0 3 1
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