Passed
Push — master ( 481a43...abc19c )
by Iman
04:22
created

CbRouter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A routeController() 0 11 3
A routeGet() 0 8 2
A getControllerPath() 0 6 2
A makeSlug() 0 7 2
A getControllerMethods() 0 8 2
A setRoute() 0 7 3
A routePost() 0 7 1
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule;
4
5
use Route;
6
7
class CbRouter
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->name);
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, 4);
33
        $slug = self::makeSlug($methodName);
34
        Route::post($prefix.$slug.$wildcards, [
35
            'uses' => $controller.'@'.$method,
36
            'as' => $controller.'Post'.$methodName,
37
        ]);
38
    }
39
40
    /**
41
     * @param $prefix
42
     * @param $controller
43
     * @param $method
44
     * @param $wildcards
45
     */
46
    private static function routeGet($prefix, $controller, $method, $wildcards)
47
    {
48
        $methodName = substr($method, 3);
49
        $slug = self::makeSlug($methodName);
50
        $slug = ($slug == 'index') ? '' : $slug;
51
        Route::get($prefix.$slug.$wildcards, [
52
            'uses' => $controller.'@'.$method,
53
            'as' => $controller.'Get'.$methodName
54
        ]);
55
    }
56
57
    /**
58
     * @param $ctrl
59
     * @return array|\ReflectionMethod[]
60
     * @throws \ReflectionException
61
     */
62
    private static function getControllerMethods($ctrl)
63
    {
64
        $controller_methods = (new \ReflectionClass($ctrl))->getMethods(\ReflectionMethod::IS_PUBLIC);
65
        $controller_methods = array_filter($controller_methods, function ($method) {
66
            return ($method->class !== 'Illuminate\Routing\Controller' && $method->name !== 'getIndex');
67
        });
68
69
        return $controller_methods;
70
    }
71
72
    /**
73
     * @param $controller
74
     * @param $namespace
75
     * @return string
76
     */
77
    private static function getControllerPath($controller, $namespace)
78
    {
79
        $ns = $namespace ?: ctrlNamespace();
80
        $ctrl = $ns.'\\'.$controller;
81
82
        return $ctrl;
83
    }
84
85
    /**
86
     * @param $prefix
87
     * @param $controller
88
     * @param $method
89
     */
90
    private static function setRoute($prefix, $controller, $method)
91
    {
92
        $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}';
93
        if (starts_with($method, 'get')) {
94
            self::routeGet($prefix, $controller, $method, $wildcards);
95
        } elseif (starts_with($method, 'post')) {
96
            self::routePost($prefix, $controller, $method, $wildcards);
97
        }
98
    }
99
100
    /**
101
     * @param $methodName
102
     * @return array|string
103
     */
104
    private static function makeSlug($methodName)
105
    {
106
        $slug = preg_split('/(?=[A-Z])/', $methodName) ?: [];
107
        $slug = array_filter($slug);
108
        $slug = strtolower(implode('-', $slug));
109
110
        return $slug;
111
    }
112
}