Completed
Push — master ( d708fb...6202fd )
by Sinnarasa
02:37
created

Middleware::classMiddleware()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 2
Metric Value
c 7
b 0
f 2
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 3
nop 0
1
<?php
2
3
namespace JetFire\Routing;
4
use ReflectionMethod;
5
6
7
/**
8
 * Class Middleware
9
 * @package JetFire\Routing
10
 */
11
class Middleware
12
{
13
14
    /**
15
     * @var Router
16
     */
17
    private $router;
18
19
    /**
20
     * @param Router $router
21
     */
22
    public function __construct(Router $router)
23
    {
24
        $this->router = $router;
25
    }
26
27
    /**
28
     * @description global middleware
29
     */
30
    public function globalMiddleware()
31
    {
32
        if (isset($this->router->collection->middleware['global_middleware']))
33
            foreach ($this->router->collection->middleware['global_middleware'] as $class)
34
                if (class_exists($class)) $this->callHandler($class);
35
    }
36
37
    /**
38
     * @description block middleware
39
     */
40
    public function blockMiddleware()
41
    {
42
        if (isset($this->router->collection->middleware['block_middleware']))
43 View Code Duplication
            if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) {
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
                $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
45
                $this->callHandler($class);
46
            }
47
    }
48
49
    /**
50
     * @description controller middleware
51
     */
52
    public function classMiddleware()
53
    {
54
        if (isset($this->router->collection->middleware['class_middleware'])) {
55
            $ctrl = str_replace('\\', '/', $this->router->route->getTarget('controller'));
56
            if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) {
57
                $class = $this->router->collection->middleware['class_middleware'][$ctrl];
58
                $this->callHandler($class);
59
            }
60
        }
61
    }
62
63
    /**
64
     * @description route middleware
65
     */
66
    public function routeMiddleware()
67
    {
68 View Code Duplication
        if (isset($this->router->collection->middleware['route_middleware']))
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...
69
            if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) {
70
                $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']];
71
                $this->callHandler($class);
72
            }
73
    }
74
75
    /**
76
     * @param $class
77
     * @return mixed
78
     */
79
    private function callHandler($class){
80
        $instance = call_user_func($this->router->getConfig()['di'],$class);
81
        if (method_exists($instance, 'handle')) {
82
            $reflectionMethod = new ReflectionMethod($instance, 'handle');
83
            $dependencies = [];
84
            foreach ($reflectionMethod->getParameters() as $arg)
85
                if (!is_null($arg->getClass()))
86
                    $dependencies[] = $this->getClass($arg->getClass()->name);
87
            $dependencies = array_merge($dependencies, [$this->router->route]);
88
            $reflectionMethod->invokeArgs($instance, $dependencies);
89
        }
90
    }
91
92
    /**
93
     * @param $class
94
     * @return Route|RouteCollection|Router|mixed
95
     */
96
    private function getClass($class){
97
        switch($class){
98
            case 'JetFire\Routing\Route':
99
                return $this->router->route;
100
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
101
            case 'JetFire\Routing\Router':
102
                return $this->router;
103
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
104
            case 'JetFire\Routing\RouteCollection':
105
                return $this->router->collection;
106
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
107
            default:
108
                return call_user_func_array($this->router->getConfig()['di'],[$class]);
109
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
110
        }
111
    }
112
}
113