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

DependencyFactory::newProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 7
cc 1
eloc 4
nc 1
nop 1
ccs 4
cts 4
cp 1
crap 1
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
11
final class DependencyFactory
12
{
13
    /**
14
     * Create dependency object
15
     *
16
     * @param \ReflectionClass $class
17
     *
18
     * @return Dependency
19
     */
20 44
    public function newAnnotatedDependency(\ReflectionClass $class)
21
    {
22 44
        $annotateClass = new AnnotatedClass(new AnnotationReader);
23 44
        $newInstance = $annotateClass->getNewInstance($class);
24 44
        $postConstruct = $annotateClass->getPostConstruct($class);
25 44
        $dependency = new Dependency($newInstance, $postConstruct);
26
27 44
        return $dependency;
28
    }
29
30
    /**
31
     * Create Provider binding
32
     *
33
     * @param \ReflectionClass $provider
34
     *
35
     * @return DependencyProvider
36
     */
37 12
    public function newProvider(\ReflectionClass $provider)
38
    {
39 12
        $dependency = $this->newAnnotatedDependency($provider);
40 12
        $dependency = new DependencyProvider($dependency);
41
42 12
        return $dependency;
43
    }
44
45
    /**
46
     * Create ToConstructor binding
47
     *
48
     * @param \ReflectionClass  $class
49
     * @param string            $name
50
     * @param InjectionPoints   $injectionPoints
51
     * @param \ReflectionMethod $postConstruct
52
     *
53
     * @return Dependency
54
     */
55 2
    public function newToConstructor(
56
        \ReflectionClass $class,
57
        $name,
58
        InjectionPoints $injectionPoints = null,
59
        \ReflectionMethod $postConstruct = null
60
    ) {
61 2
        $setterMethods = $injectionPoints ? $injectionPoints($class->name) : new SetterMethods([]);
62 2
        $postConstruct = $postConstruct ? new \ReflectionMethod($class, $postConstruct) : null;
63 2
        $newInstance = new NewInstance($class, $setterMethods, new Name($name));
64 2
        $dependency = new Dependency($newInstance, $postConstruct);
65
66 2
        return $dependency;
67
    }
68
}
69