Completed
Push — master ( beba29...2cba6a )
by Sinnarasa
01:46
created

ControllerDispatcher::call()   D

Complexity

Conditions 10
Paths 16

Size

Total Lines 39
Code Lines 28

Duplication

Lines 9
Ratio 23.08 %

Importance

Changes 0
Metric Value
dl 9
loc 39
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 28
nc 16
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
use JetFire\Routing\Response;
6
use JetFire\Routing\ResponseInterface;
7
use JetFire\Routing\Route;
8
use JetFire\Routing\RouteCollection;
9
use JetFire\Routing\Router;
10
use ReflectionClass;
11
use ReflectionMethod;
12
13
/**
14
 * Class ControllerDispatcher
15
 * @package JetFire\Routing\Dispatcher
16
 */
17
class ControllerDispatcher implements DispatcherInterface
18
{
19
20
    /**
21
     * @var Router
22
     */
23
    private $router;
24
25
26
    /**
27
     * @param Router $router
28
     */
29
    public function __construct(Router $router)
30
    {
31
        $this->router = $router;
32
    }
33
34
35
    /**
36
     * @throws \Exception
37
     */
38
    public function call()
39
    {
40
        $classInstance = [
41
            Route::class => $this->router->route,
42
            Response::class => $this->router->response,
43
            RouteCollection::class => $this->router->collection,
44
        ];
45
        
46
        $reflectionMethod = new ReflectionMethod($this->router->route->getTarget('controller'), $this->router->route->getTarget('action'));
47
        $dependencies = [];
48
        $count = 0;
49
50
        foreach ($reflectionMethod->getParameters() as $arg) {
51
            if (!is_null($arg->getClass())) {
52
                if (isset($classInstance[$arg->getClass()->name]))
53
                $dependencies[] = $classInstance[$arg->getClass()->name];
54
            else
55
                $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$arg->getClass()->name]);
56
            } else {
57
                $count++;
58
            }
59
        }
60
61
        if ($count == count($this->router->route->getParameters()) || ($this->router->route->getParameters() == '' && $count == 0)) {
62
            $dependencies = array_merge($dependencies, ($this->router->route->getParameters() == '') ? [] : $this->router->route->getParameters());
63
            $content = $reflectionMethod->invokeArgs($this->getController($classInstance), $dependencies);
64 View Code Duplication
            if ($content instanceof ResponseInterface) {
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...
65
                $this->router->response = $content;
66
            } else {
67
                if (is_array($content)) {
68
                    $this->router->route->addTarget('data', $content);
69
                    $content = json_encode($content);
70
                }
71
                $this->router->response->setContent($content);
72
            }
73
        } else {
74
            $this->router->response->setStatusCode(404);
75
        }
76
    }
77
78
79
    /**
80
     * @param array $classInstance
81
     * @return object
82
     * @throws \Exception
83
     */
84
    private function getController($classInstance = [])
85
    {
86
        $reflector = new ReflectionClass($this->router->route->getTarget('controller'));
87
        if (!$reflector->isInstantiable()) {
88
            throw new \Exception('Target [' . $this->router->route->getTarget('controller') . '] is not instantiable.');
89
        }
90
        $constructor = $reflector->getConstructor();
91
        if (is_null($constructor)) {
92
            $class = $this->router->route->getTarget('controller');
93
            return call_user_func_array($this->router->route->getTarget('di'), [$class]);
94
        }
95
        $dependencies = [];
96
        foreach ($constructor->getParameters() as $dep) {
97
            $class = $dep->getClass()->name;
98
            if (isset($classInstance[$class])) {
99
                $dependencies[] = $classInstance[$class];
100
            } else {
101
                $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$class]);
102
            }
103
        }
104
        return $reflector->newInstanceArgs($dependencies);
105
    }
106
107
}
108