Completed
Push — master ( 220ceb...ab9a78 )
by Sinnarasa
02:32
created

ControllerDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JetFire\Routing\Dispatcher;
4
5
use JetFire\Routing\Route;
6
use ReflectionClass;
7
use ReflectionMethod;
8
9
/**
10
 * Class ControllerDispatcher
11
 * @package JetFire\Routing\Dispatcher
12
 */
13
class ControllerDispatcher implements DispatcherInterface
14
{
15
16
    /**
17
     * @var Route
18
     */
19
    private $route;
20
21
    /**
22
     * @param Route $route
23
     */
24
    public function __construct(Route $route)
25
    {
26
        $this->route = $route;
27
    }
28
29
    /**
30
     * @return mixed
31
     * @throws \Exception
32
     */
33
    public function call()
34
    {
35
        $reflectionMethod = new ReflectionMethod($this->route->getTarget('controller'), $this->route->getTarget('action'));
36
        $dependencies = ($this->route->getParameters() == '') ? [] : $this->route->getParameters();
37
        foreach ($reflectionMethod->getParameters() as $arg) {
38
            if (!is_null($arg->getClass())) {
39
                $class = $arg->getClass()->name;
40
                array_unshift($dependencies, call_user_func_array($this->route->getTarget('di'),[$class]));
41
            }
42
        }
43 View Code Duplication
        if ($this->route->getResponse('code') == 202)
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...
44
            $this->route->setResponse(['code' => 200, 'message' => 'OK', 'type' => 'text/html']);
45
        return $reflectionMethod->invokeArgs($this->getController(), $dependencies);
46
    }
47
48
49
    /**
50
     * @return object
51
     * @throws \Exception
52
     */
53
    private function getController()
54
    {
55
        $reflector = new ReflectionClass($this->route->getTarget('controller'));
56
        if (!$reflector->isInstantiable())
57
            throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
58
        $constructor = $reflector->getConstructor();
59
        if (is_null($constructor)) {
60
            $class = $this->route->getTarget('controller');
61
            return call_user_func_array($this->route->getTarget('di'),[$class]);
62
        }
63
        $dependencies = $constructor->getParameters();
64
        $arguments = [];
65
        foreach ($dependencies as $dep) {
66
            $class = $dep->getClass()->name;
67
            array_push($arguments, call_user_func_array($this->route->getTarget('di'),[$class]));
68
        }
69
        return $reflector->newInstanceArgs($arguments);
70
    }
71
72
}
73