Completed
Branch 2.x (dc1b30)
by Akihito
02:25
created

Arguments::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 7
cc 2
eloc 4
c 2
b 1
f 0
nc 2
nop 2
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
use Doctrine\Common\Annotations\AnnotationReader;
10
use Ray\Di\Exception\Unbound;
11
12
final class Arguments
13
{
14
    /**
15
     * @var Argument[]
16
     */
17
    private $arguments = [];
18
19 31
    public function __construct(\ReflectionMethod $method, Name $name)
20
    {
21 31
        $parameters = $method->getParameters();
22 31
        foreach ($parameters as $parameter) {
23 31
            $this->arguments[] = new Argument($parameter, $name($parameter));
24 31
        }
25 31
    }
26
27
    /**
28
     * Return arguments
29
     *
30
     * @param Container $container
31
     *
32
     * @return Argument[]
33
     *
34
     * @throws Exception\Unbound
35
     */
36 30
    public function inject(Container $container)
37
    {
38 30
        $parameters = $this->arguments;
39 30
        foreach ($parameters as &$parameter) {
40 30
            $parameter = $this->getParameter($container, $parameter);
41 28
        }
42
43 28
        return $parameters;
44
    }
45
46
    /**
47
     * @param Container $container
48
     * @param Argument  $argument
49
     *
50
     * @return mixed
51
     *
52
     * @throws Unbound
53
     */
54 30
    private function getParameter(Container $container, Argument $argument)
55
    {
56 30
        $this->bindInjectionPoint($container, $argument);
57
        try {
58 30
            return $container->getDependency((string) $argument);
59 12
        } catch (Unbound $e) {
60 12
            list($hasDefaultValue, $defaultValue) = $this->getDefaultValue($argument);
61 12
            if ($hasDefaultValue) {
62 6
                return $defaultValue;
63
            }
64 7
            throw new Unbound($argument->getMeta(), 0, $e);
65
        }
66
    }
67
68
    /**
69
     * @param Argument $argument
70
     *
71
     * @return array [$hasDefaultValue, $defaultValue]
72
     */
73 12
    private function getDefaultValue(Argument $argument)
74
    {
75 12
        if ($argument->isDefaultAvailable()) {
76 6
            return [true, $argument->getDefaultValue()];
77
        }
78
79 7
        return [false, null];
80
    }
81
82 30
    private function bindInjectionPoint(Container $container, Argument $argument)
83
    {
84 30
        $isSelf = (string) $argument === 'Ray\Di\InjectionPointInterface-' . Name::ANY;
85 30
        if ($isSelf) {
86 2
            return;
87
        }
88 30
        (new Bind($container, 'Ray\Di\InjectionPointInterface'))->toInstance(new InjectionPoint($argument->get(), new AnnotationReader));
89 30
    }
90
}
91