MacroServiceProvider::register()   B
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.1106

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 17
cts 21
cp 0.8095
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 1
nop 0
crap 4.1106
1
<?php
2
3
namespace Loadsman\LaravelPlugin\Providers;
4
5
use Illuminate\Support\ServiceProvider;
6
7
/**
8
 * Macroses for macroable services.
9
 */
10
class MacroServiceProvider extends ServiceProvider
11
{
12 3
    public function register()
13
    {
14
        /**
15
         * Present controller method as `post` route.
16
         */
17 3
        app('router')->macro('expose',
18
            function ($controller = '', Array $actions) {
19
                foreach ($actions as $action) {
20
                    $this->post(snake_case($action, '-'),
21
                        $controller.'@'.$action);
22
                }
23 3
            });
24
25
        /**
26
         * Expose all public methods of controller.
27
         * @see `expose` macro /\
28
         */
29 3
        app('router')->macro('exposeAll', function ($controllerClassName) {
30 3
            $controllerClassReflection = new \ReflectionClass($controllerClassName);
31 3
            $publicActions = $controllerClassReflection->getMethods(\ReflectionMethod::IS_PUBLIC);
32 3
            foreach ($publicActions as $publicAction) {
33 3
                $declaringClass = $publicAction->getDeclaringClass()->getName();
34 3
                $isOwn = $declaringClass === $controllerClassName;
35 3
                if (! $isOwn) {
36 3
                    continue;
37
                }
38 3
                $action = $publicAction->getName();
39 3
                $this->post(snake_case($action, '-'),
40 3
                    $controllerClassName.'@'.$action);
41 2
            }
42 3
        });
43
    }
44
}