DependencyResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 9
c 4
b 0
f 2
lcom 1
cbo 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B resolveArguments() 0 17 5
A getDependency() 0 10 3
1
<?php
2
3
namespace JsonSpec\Behat\Context\ArgumentResolver;
4
5
use Behat\Behat\Context\Argument\ArgumentResolver;
6
use Fesor\JsonMatcher\Helper\JsonHelper;
7
use Fesor\JsonMatcher\JsonMatcherFactory;
8
use JsonSpec\JsonLoader;
9
use ReflectionClass;
10
11
/**
12
 * Class DependencyResolver
13
 * @package JsonSpec\Behat\Context\ArgumentResolver
14
 */
15
class DependencyResolver implements ArgumentResolver
16
{
17
18
    /**
19
     * @var array
20
     */
21
    private $dependencies;
22
23
    /**
24
     * @param JsonMatcherFactory $matcherFactory
25
     * @param JsonLoader $jsonLoader
26
     * @param JsonHelper $jsonHelper
27
     */
28
    public function __construct(
29
        JsonMatcherFactory $matcherFactory,
30
        JsonLoader $jsonLoader,
31
        JsonHelper $jsonHelper
32
    )
33
    {
34
        $this->dependencies = [
35
            $matcherFactory,
36
            $jsonLoader,
37
            $jsonHelper
38
        ];
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function resolveArguments(ReflectionClass $classReflection, array $arguments)
45
    {
46
        $constructor = $classReflection->getConstructor();
47
        if ($constructor !== null) {
48
            $parameters = $constructor->getParameters();
49
            foreach ($parameters as $parameter) {
50
                if (
51
                    null !== $parameter->getClass() &&
52
                    null !== ($dependency = $this->getDependency($parameter->getClass()->name))
53
                ) {
54
                    $arguments[$parameter->name] = $dependency;
55
                }
56
            }
57
        }
58
59
        return $arguments;
60
    }
61
62
    /**
63
     * @param string $className
64
     * @return object
65
     */
66
    private function getDependency($className)
67
    {
68
        foreach ($this->dependencies as $dependency) {
69
            if (is_a($dependency, $className, true)) {
70
                return $dependency;
71
            }
72
        }
73
74
        return null;
75
    }
76
77
}
78