DependencyFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 10
c 0
b 0
f 0
dl 0
loc 45
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A newToConstructor() 0 11 2
A newAnnotatedDependency() 0 7 1
A newProvider() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Aop\ReflectionClass;
8
use ReflectionMethod;
9
10
use function assert;
11
use function class_exists;
12
13
final class DependencyFactory
14
{
15
    /**
16
     * Create dependency object
17
     *
18
     * @param ReflectionClass<object> $class
19
     */
20
    public function newAnnotatedDependency(ReflectionClass $class): Dependency
21
    {
22
        $annotateClass = new AnnotatedClass();
23
        $newInstance = $annotateClass->getNewInstance($class);
24
        $postConstruct = $annotateClass->getPostConstruct($class);
25
26
        return new Dependency($newInstance, $postConstruct);
27
    }
28
29
    /**
30
     * Create Provider binding
31
     *
32
     * @param ReflectionClass<object> $provider
33
     */
34
    public function newProvider(ReflectionClass $provider, string $context): DependencyProvider
35
    {
36
        $dependency = $this->newAnnotatedDependency($provider);
37
38
        return new DependencyProvider($dependency, $context);
39
    }
40
41
    /**
42
     * Create ToConstructor binding
43
     *
44
     * @param ReflectionClass<object>      $class
45
     * @param string|array<string, string> $name
46
     */
47
    public function newToConstructor(
48
        ReflectionClass $class,
49
        string|array $name,
50
        ?InjectionPoints $injectionPoints = null,
51
        ?ReflectionMethod $postConstruct = null
52
    ): Dependency {
53
        assert(class_exists($class->name));
54
        $setterMethods = $injectionPoints instanceof InjectionPoints ? $injectionPoints($class->name) : new SetterMethods([]);
55
        $newInstance = new NewInstance($class, $setterMethods, new Name($name));
56
57
        return new Dependency($newInstance, $postConstruct);
58
    }
59
}
60