MacroServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 35
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 32 4
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
}