Completed
Push — master ( 3b4c18...beba29 )
by Sinnarasa
04:35
created

ControllerDispatcher::call()   C

Complexity

Conditions 10
Paths 16

Size

Total Lines 48
Code Lines 28

Duplication

Lines 9
Ratio 18.75 %

Importance

Changes 0
Metric Value
dl 9
loc 48
rs 5.3454
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
            /*if (is_array($content)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
74
                $this->router->route->addTarget('data', $content);
75
                if (isset($this->router->route->getParams()['ajax']) && $this->router->route->getParams()['ajax'] === true) {
76
                    $this->router->response->setContent(json_encode($content));
77
                    $this->router->response->setHeaders(['Content-Type' => 'application/json']);
78
                }
79
            } elseif (!is_null($content)) {
80
                $this->router->response->setContent($content);
81
            }*/
82
        } else {
83
            $this->router->response->setStatusCode(404);
84
        }
85
    }
86
87
88
    /**
89
     * @param array $classInstance
90
     * @return object
91
     * @throws \Exception
92
     */
93
    private function getController($classInstance = [])
94
    {
95
        $reflector = new ReflectionClass($this->router->route->getTarget('controller'));
96
        if (!$reflector->isInstantiable()) {
97
            throw new \Exception('Target [' . $this->router->route->getTarget('controller') . '] is not instantiable.');
98
        }
99
        $constructor = $reflector->getConstructor();
100
        if (is_null($constructor)) {
101
            $class = $this->router->route->getTarget('controller');
102
            return call_user_func_array($this->router->route->getTarget('di'), [$class]);
103
        }
104
        $dependencies = [];
105
        foreach ($constructor->getParameters() as $dep) {
106
            $class = $dep->getClass()->name;
107
            if (isset($classInstance[$class])) {
108
                $dependencies[] = $classInstance[$class];
109
            } else {
110
                $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$class]);
111
            }
112
        }
113
        return $reflector->newInstanceArgs($dependencies);
114
    }
115
116
}
117