Passed
Push — master ( 8cf0de...a7615b )
by Matthias
02:06
created

DependencyInjectionManager::getFrameworkManager()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 14
cp 0
rs 6.7272
cc 7
eloc 23
nc 7
nop 1
crap 56
1
<?php
2
/*
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 zepi
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 *
25
 */
26
27
/**
28
 * The DependenyInjectionManager manages the initiation of new
29
 * objects. The manager will analyze the construct method of the
30
 * given class name and loads the needed objects for the construction.
31
 * 
32
 * @package Zepi\Turbo\Manager
33
 * @author Matthias Zobrist <[email protected]>
34
 * @copyright Copyright (c) 2016 zepi
35
 */
36
37
namespace Zepi\Turbo\Manager;
38
39
use \Zepi\Turbo\Framework;
40
use \Zepi\Turbo\Exception;
41
use \ReflectionClass;
42
43
/**
44
 * The DependenyInjectionManager manages the initiation of new
45
 * objects. The manager will analyze the construct method of the
46
 * given class name and loads the needed objects for the construction.
47
 * 
48
 * @author Matthias Zobrist <[email protected]>
49
 * @copyright Copyright (c) 2016 zepi
50
 */
51
class DependencyInjectionManager
52
{
53
    /**
54
     * @access protected
55
     * @var Framework
56
     */
57
    protected $framework;
58
    
59
    /**
60
     * Constructs the object
61
     * 
62
     * @access public
63
     * @param \Zepi\Turbo\Framework $framework
64
     */
65 17
    public function __construct(Framework $framework)
66
    {
67 17
        $this->framework = $framework;
68 17
    }
69
    
70
    /**
71
     * Initiates the given class name
72
     *
73
     * @param string $className
74
     * @param array $additionalParameters
75
     * @return object
76
     * 
77
     * @throws \Zepi\Turbo\Exception Cannot find correct value for parameter "{parameterName}" in class "{className}".
78
     */
79
    public function initiateObject($className, $additionalParameters = array())
80
    {
81
        $reflection = new ReflectionClass($className);
82
        
83
        if (!$reflection->hasMethod('__construct')) {
84
            return new $className();
85
        }
86
        
87
        $constructor = $reflection->getConstructor();
88
        
89
        $parameters = array();
90
        foreach ($constructor->getParameters() as $parameter) {
91
            $parameterValue = null;
92
            
93
            if (isset($additionalParameters[$parameter->name])) {
94
                $parameterValue = $additionalParameters[$parameter->name];
95
            } else if ($parameter->getClass() !== null) {
96
                $parameterValue = $this->getInstance($parameter->getClass());
97
            } 
98
            
99
            if ($parameterValue === null) {
100
                throw new Exception('Cannot find correct value for parameter "' . $parameter->name . '" in class "' . $className . '".');
101
            }
102
            
103
            $parameters[] = $parameterValue;
104
        }
105
        
106
        return $reflection->newInstanceArgs($parameters);
107
    }
108
    
109
    /**
110
     * Returns the instance for the given class
111
     * 
112
     * @param \ReflectionClass $parameterClass
113
     * @return null|object
114
     */
115
    protected function getInstance(ReflectionClass $parameterClass)
116
    {
117
        if (!class_exists($parameterClass->name, true)) {
118
            return null;
119
        }
120
        
121
        if (strpos($parameterClass->name, 'Zepi\\Turbo\\') === 0) {
122
            return $this->getFrameworkInstance($parameterClass);
123
        }
124
        
125
        if ($parameterClass->isInstantiable()) {
126
            return $this->framework->getInstance($parameterClass->name);
127
        }
128
    }
129
    
130
    /**
131
     * Returns the instance for the given class if the class path
132
     * starts with Zepi\Turbo.
133
     * 
134
     * @param \ReflectionClass $parameterClass
135
     * @return object
136
     */
137
    protected function getFrameworkInstance(ReflectionClass $parameterClass)
138
    {
139
        if ($parameterClass->name == 'Zepi\\Turbo\\Framework') {
140
            return $this->framework;
141
        }
142
        
143
        if ($parameterClass->name == 'Zepi\\Turbo\\Request\\RequestAbstract') {
144
            return $this->framework->getRequest();
145
        }
146
        
147
        if ($parameterClass->name == 'Zepi\\Turbo\\Response\\Response') {
148
            return $this->framework->getResponse();
149
        }
150
        
151
        if ($parameterClass->getNamespaceName() === 'Zepi\\Turbo\\Manager') {
152
            return $this->getFrameworkManager($parameterClass->name);
153
        }
154
    }
155
    
156
    /**
157
     * Returns the framework manager for the given class name
158
     * 
159
     * @param string $className
160
     * @return object
161
     * 
162
     * @throws \Zepi\Turbo\Exception Cannot find framework manager "{className}".
163
     */
164
    protected function getFrameworkManager($className)
165
    {
166
        switch ($className) {
167
            case 'Zepi\\Turbo\\Manager\\DataSourceManager':
168
                return $this->framework->getDataSourceManager();
169
            break;
170
            
171
            case 'Zepi\\Turbo\\Manager\\DependencyInjectionManager':
172
                return $this->framework->getDependencyInjectionManager();
173
            break;
174
                
175
            case 'Zepi\\Turbo\\Manager\\ModuleManager':
176
                return $this->framework->getModuleManager();
177
            break;
178
                
179
            case 'Zepi\\Turbo\\Manager\\RequestManager':
180
                return $this->framework->getRequestManager();
181
            break;
182
                
183
            case 'Zepi\\Turbo\\Manager\\RouteManager':
184
                return $this->framework->getRouteManager();
185
            break;
186
            
187
            case 'Zepi\\Turbo\\Manager\\RuntimeManager':
188
                return $this->framework->getRuntimeManager();
189
            break;
190
            
191
            default:
192
                throw new Exception('Cannot find framework manager "' . $className . '".');
193
            break;
1 ignored issue
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
194
        }
195
    }
196
}
197