ControllerInspector::isRoutable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mfrancois
5
 * Date: 17/09/2016
6
 * Time: 16:48
7
 */
8
9
namespace Distilleries\Expendable\Http\Router;
10
11
use ReflectionClass;
12
use ReflectionMethod;
13
use Illuminate\Support\Str;
14
15
class ControllerInspector
16
{
17
    /**
18
     * An array of HTTP verbs.
19
     *
20
     * @var array
21
     */
22
    protected $verbs = [
23
        'any', 'get', 'post', 'put', 'patch',
24
        'delete', 'head', 'options',
25
    ];
26
    /**
27
     * Get the routable methods for a controller.
28
     *
29
     * @param  string  $controller
30
     * @param  string  $prefix
31
     * @return array
32
     */
33 288
    public function getRoutable($controller, $prefix)
34
    {
35 288
        $routable = [];
36 288
        $reflection = new ReflectionClass($controller);
37 288
        $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
38
        // To get the routable methods, we will simply spin through all methods on the
39
        // controller instance checking to see if it belongs to the given class and
40
        // is a publicly routable method. If so, we will add it to this listings.
41 288
        foreach ($methods as $method) {
42 288
            if ($this->isRoutable($method)) {
43 288
                $data = $this->getMethodData($method, $prefix);
44 288
                $routable[$method->name][] = $data;
45
                // If the routable method is an index method, we will create a special index
46
                // route which is simply the prefix and the verb and does not contain any
47
                // the wildcard place-holders that each "typical" routes would contain.
48 288
                if ($data['plain'] == $prefix.'/index') {
49 288
                    $routable[$method->name][] = $this->getIndexData($data, $prefix);
50
                }
51
            }
52
        }
53 288
        return $routable;
54
    }
55
    /**
56
     * Determine if the given controller method is routable.
57
     *
58
     * @param  \ReflectionMethod  $method
59
     * @return bool
60
     */
61 288
    public function isRoutable(ReflectionMethod $method)
62
    {
63 288
        if ($method->class == 'Illuminate\Routing\Controller') {
64 288
            return false;
65
        }
66 288
        return Str::startsWith($method->name, $this->verbs);
67
    }
68
    /**
69
     * Get the method data for a given method.
70
     *
71
     * @param  \ReflectionMethod  $method
72
     * @param  string  $prefix
73
     * @return array
74
     */
75 288
    public function getMethodData(ReflectionMethod $method, $prefix)
76
    {
77 288
        $verb = $this->getVerb($name = $method->name);
78 288
        $uri = $this->addUriWildcards($plain = $this->getPlainUri($name, $prefix));
79 288
        return compact('verb', 'plain', 'uri');
80
    }
81
    /**
82
     * Get the routable data for an index method.
83
     *
84
     * @param  array   $data
85
     * @param  string  $prefix
86
     * @return array
87
     */
88 288
    protected function getIndexData($data, $prefix)
89
    {
90 288
        return ['verb' => $data['verb'], 'plain' => $prefix, 'uri' => $prefix];
91
    }
92
    /**
93
     * Extract the verb from a controller action.
94
     *
95
     * @param  string  $name
96
     * @return string
97
     */
98 288
    public function getVerb($name)
99
    {
100 288
        return head(explode('_', Str::snake($name)));
101
    }
102
    /**
103
     * Determine the URI from the given method name.
104
     *
105
     * @param  string  $name
106
     * @param  string  $prefix
107
     * @return string
108
     */
109 288
    public function getPlainUri($name, $prefix)
110
    {
111 288
        return $prefix.'/'.implode('-', array_slice(explode('_', Str::snake($name)), 1));
112
    }
113
    /**
114
     * Add wildcards to the given URI.
115
     *
116
     * @param  string  $uri
117
     * @return string
118
     */
119 288
    public function addUriWildcards($uri)
120
    {
121 288
        return $uri.'/{one?}/{two?}/{three?}/{four?}/{five?}';
122
    }
123
}