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

ControllerDispatcher   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 3.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 2
loc 60
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B call() 2 14 5
A getController() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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