Passed
Pull Request — master (#1114)
by Iman
04:37
created

RouteController::getControllerPath()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 2
dl 0
loc 6
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
16
            $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}';
17
            foreach (self::getControllerMethods($controller, $namespace) as $method) {
18
                if (str_start($method, 'get')) {
19
                    self::routeGet($prefix, $controller, $method, $wildcards);
20
                } elseif (str_start($method, 'post')) {
21
                    self::routePost($prefix, $controller, $method, $wildcards);
22
                }
23
            }
24
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
25
26
        }
27
    }
28
29
    /**
30
     * @param $prefix
31
     * @param $controller
32
     * @param $method
33
     * @param $wildcards
34
     * @return array
35
     */
36
    private static function routePost($prefix, $controller, $method, $wildcards)
37
    {
38
        $methodName = substr($method->name, 4);
39
        $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

39
        $slug = array_filter(/** @scrutinizer ignore-type */ preg_split('/(?=[A-Z])/', $methodName));
Loading history...
40
        $slug = strtolower(implode('-', $slug));
41
        Route::post($prefix.$slug.$wildcards, [
42
            'uses' => $controller.'@'.$method->name,
43
            'as' => $controller.'Post'.$methodName,
44
        ]);
45
    }
46
47
    /**
48
     * @param $prefix
49
     * @param $controller
50
     * @param $method
51
     * @param $wildcards
52
     */
53
    private static function routeGet($prefix, $controller, $method, $wildcards)
54
    {
55
        $methodName = substr($method->name, 3);
56
        $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

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