ControllerDispatcher::call()   C
last analyzed

Complexity

Conditions 11
Paths 17

Size

Total Lines 46
Code Lines 31

Duplication

Lines 13
Ratio 28.26 %

Importance

Changes 0
Metric Value
dl 13
loc 46
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 31
nc 17
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
41 View Code Duplication
        if (!class_exists($this->router->route->getTarget('controller'))) {
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...
42
            throw new \Exception('Class not found : "' . $this->router->route->getTarget('controller') . '"');
43
        }
44
45
        $classInstance = [
46
            Route::class => $this->router->route,
47
            Response::class => $this->router->response,
48
            RouteCollection::class => $this->router->collection,
49
        ];
50
51
        $reflectionMethod = new ReflectionMethod($this->router->route->getTarget('controller'), $this->router->route->getTarget('action'));
52
        $dependencies = [];
53
        $count = 0;
54
55
        foreach ($reflectionMethod->getParameters() as $arg) {
56
            if (!is_null($arg->getClass())) {
57
                if (isset($classInstance[$arg->getClass()->name])) {
58
                    $dependencies[] = $classInstance[$arg->getClass()->name];
59
                } else {
60
                    $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$arg->getClass()->name]);
61
                }
62
            } else {
63
                $count++;
64
            }
65
        }
66
67
        if ($count == count($this->router->route->getParameters()) || ($this->router->route->getParameters() == '' && $count == 0)) {
68
            $dependencies = array_merge($dependencies, ($this->router->route->getParameters() == '') ? [] : $this->router->route->getParameters());
69
            $content = $reflectionMethod->invokeArgs($this->getController($classInstance), $dependencies);
70 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...
71
                $this->router->response = $content;
72
            } else {
73
                if (is_array($content)) {
74
                    $this->router->route->addTarget('data', $content);
75
                    $content = json_encode($content);
76
                }
77
                $this->router->callMiddleware('between');
78
                $this->router->response->setContent($content);
79
            }
80
        } else {
81
            $this->router->response->setStatusCode(404);
82
        }
83
    }
84
85
86
    /**
87
     * @param array $classInstance
88
     * @return object
89
     * @throws \Exception
90
     */
91
    private function getController($classInstance = [])
92
    {
93
        $reflector = new ReflectionClass($this->router->route->getTarget('controller'));
94 View Code Duplication
        if (!$reflector->isInstantiable()) {
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...
95
            throw new \Exception('Target [' . $this->router->route->getTarget('controller') . '] is not instantiable.');
96
        }
97
        $constructor = $reflector->getConstructor();
98
        if (is_null($constructor)) {
99
            $class = $this->router->route->getTarget('controller');
100
            return call_user_func_array($this->router->route->getTarget('di'), [$class]);
101
        }
102
        $dependencies = [];
103
        foreach ($constructor->getParameters() as $dep) {
104
            $class = $dep->getClass()->name;
105
            if (isset($classInstance[$class])) {
106
                $dependencies[] = $classInstance[$class];
107
            } else {
108
                $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$class]);
109
            }
110
        }
111
        return $reflector->newInstanceArgs($dependencies);
112
    }
113
114
}
115