Completed
Push — master ( 4c64c5...1a9d0b )
by
unknown
29s
created

Dependency::resolve()   B

Complexity

Conditions 5
Paths 11

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 23
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 18
nc 11
nop 2
crap 30
1
<?php
2
3
namespace Zewa;
4
5
use Zewa\Exception\Exception;
6
7
class Dependency
8
{
9
    /**
10
     * @var Container
11
     */
12
    private $dependencies;
13
14
    public function __construct(Config $config, Container $container)
15
    {
16
        $local = [
17
            '\Zewa\Config' => $config,
18
            '\Zewa\Container' => $container,
19
            '\Zewa\Dependency' => $this
20
        ];
21
22
        $this->dependencies = $container;
23
        $this->dependencies->set('__dependencies', $local);
24
    }
25
26
    private function isDependencyLoaded(string $class) : bool
27
    {
28
        $dependencies = $this->dependencies->get('__dependencies');
29
30
        if (isset($dependencies[$class]) === false) {
31
            return false;
32
        }
33
34
        return true;
35
    }
36
37
    private function getDependency(string $class)
38
    {
39
        $dependencies = $this->dependencies->get('__dependencies');
40
        return $dependencies[$class] ?? null;
41
    }
42
43
    private function load(string $class, $dependency, bool $persist)
44
    {
45
        if ($persist === true) {
46
            $dependencies = $this->dependencies->get('__dependencies');
47
            $dependencies[$class] = $dependency;
48
            $this->dependencies->set('__dependencies', $dependencies);
49
        }
50
51
//        $this->injectAppInstance($dependency);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        return $dependency;
53
    }
54
55
    public function resolve($class, $persist = false)
56
    {
57
        if ($this->isDependencyLoaded($class)) {
58
            return $this->getDependency($class);
59
        }
60
61
        try {
62
            $reflectionClass = new \ReflectionClass($class);
63
64
            $constructor = $reflectionClass->getConstructor();
65
66
            if (!$constructor || $constructor->getParameters() === 0) {
67
                $dependency = new $class;
68
                return $this->load($class, $dependency, $persist);
69
            }
70
71
            $params = $this->constructConstructorParameters($reflectionClass);
72
            $dependency = $reflectionClass->newInstanceArgs($params);
73
74
            return $this->load($class, $dependency, $persist);
75
        } catch (\Exception $e) {
76
            echo "<PRE>";
77
            print_r($e->getMessage()); //->getMessage();
78
            var_dump($e->getTrace()[0]);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($e->getTrace()[0]); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
79
            echo "</PRE>";
80
            return false;
81
        }
82
    }
83
84
    /**
85
     * @param \ReflectionClass $reflection
86
     * @return array
87
     */
88
    private function constructConstructorParameters(\ReflectionClass $reflection) : array
89
    {
90
        $constructor = $reflection->getConstructor();
91
        $params = $constructor->getParameters();
92
93
        $injection = [];
94
95 View Code Duplication
        foreach ($params as $param) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            // Here we should perform a bunch of checks, such as: isArray(), isCallable(), isDefaultValueAvailable()
97
            // isOptional() etc.
98
            if (is_null($param->getClass())) {
99
                $injection[] = null;
100
                continue;
101
            }
102
            $injection[] = $this->resolve("\\" . $param->getClass()->getName());
103
        }
104
105
        return $injection;
106
    }
107
108
109
    private function injectAppInstance($dependency)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
110
    {
111
        if ($dependency instanceof Controller) {
112
            $dependency->setEvent($this->resolve('\Sabre\Event\Emitter'));
0 ignored issues
show
Bug introduced by
The method setEvent() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
            $dependency->setRequest($this->resolve('\Zewa\HTTP\Request'));
0 ignored issues
show
Bug introduced by
The method setRequest() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
            $dependency->setRouter($this->resolve('\Zewa\Router'));
0 ignored issues
show
Bug introduced by
The method setRouter() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            $dependency->setConfig($this->resolve('\Zewa\Config'));
0 ignored issues
show
Bug introduced by
The method setConfig() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
            $dependency->setDependency($this->resolve('\Zewa\Dependency'));
0 ignored issues
show
Bug introduced by
The method setDependency() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
            $dependency->setView($this->resolve('\Zewa\View'));
0 ignored issues
show
Bug introduced by
The method setView() does not seem to exist on object<Zewa\Controller>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
        }
119
    }
120
}
121