Passed
Push — master ( 1cde08...5efba4 )
by Iman
07:34 queued 03:12
created

RouteController::setRoute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule;
4
5
use Route;
6
7
class RouteController
8
{
9
    public static function routeController($prefix, $controller, $namespace = null)
10
    {
11
        $prefix = trim($prefix, '/').'/';
12
13
        try {
14
            Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']);
15
            $ctrl = self::getControllerPath($controller, $namespace);
16
            foreach (self::getControllerMethods($ctrl) as $method) {
17
                self::setRoute($prefix, $controller, $method);
18
            }
19
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
20
21
        }
22
    }
23
24
    /**
25
     * @param $prefix
26
     * @param $controller
27
     * @param $method
28
     * @param $wildcards
29
     */
30
    private static function routePost($prefix, $controller, $method, $wildcards)
31
    {
32
        $methodName = substr($method->name, 4);
33
        $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/(?=[A-Z])/', $methodName) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $slug = array_filter(/** @scrutinizer ignore-type */ preg_split('/(?=[A-Z])/', $methodName));
Loading history...
34
        $slug = strtolower(implode('-', $slug));
35
        Route::post($prefix.$slug.$wildcards, [
36
            'uses' => $controller.'@'.$method->name,
37
            'as' => $controller.'Post'.$methodName,
38
        ]);
39
    }
40
41
    /**
42
     * @param $prefix
43
     * @param $controller
44
     * @param $method
45
     * @param $wildcards
46
     */
47
    private static function routeGet($prefix, $controller, $method, $wildcards)
48
    {
49
        $methodName = substr($method->name, 3);
50
        $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/(?=[A-Z])/', $methodName) can also be of type false; however, parameter $input of array_filter() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $slug = array_filter(/** @scrutinizer ignore-type */ preg_split('/(?=[A-Z])/', $methodName));
Loading history...
51
        $slug = strtolower(implode('-', $slug));
52
        $slug = ($slug == 'index') ? '' : $slug;
53
        Route::get($prefix.$slug.$wildcards,
54
            ['uses' => $controller.'@'.$method->name,
55
            'as' => $controller.'Get'.$methodName]);
56
    }
57
58
    /**
59
     * @param $ctrl
60
     * @return array|\ReflectionMethod[]
61
     * @throws \ReflectionException
62
     */
63
    private static function getControllerMethods($ctrl)
64
    {
65
        $controller_methods = (new \ReflectionClass($ctrl))->getMethods(\ReflectionMethod::IS_PUBLIC);
66
        $controller_methods = array_filter($controller_methods, function ($method) {
67
            return ($method->class !== 'Illuminate\Routing\Controller' && $method->name !== 'getIndex');
68
        });
69
70
        return $controller_methods;
71
    }
72
73
    /**
74
     * @param $controller
75
     * @param $namespace
76
     * @return string
77
     */
78
    private static function getControllerPath($controller, $namespace)
79
    {
80
        $ns = $namespace ?: ctrlNamespace();
81
        $ctrl = $ns.'\\'.$controller;
82
83
        return $ctrl;
84
    }
85
86
    /**
87
     * @param $prefix
88
     * @param $controller
89
     * @param $method
90
     */
91
    private static function setRoute($prefix, $controller, $method)
92
    {
93
        $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}';
94
        if (str_start($method, 'get')) {
95
            self::routeGet($prefix, $controller, $method, $wildcards);
96
        } elseif (str_start($method, 'post')) {
97
            self::routePost($prefix, $controller, $method, $wildcards);
98
        }
99
    }
100
}