BaseApplication   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 20
eloc 34
dl 0
loc 66
rs 10
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A routesWithPermission() 0 5 1
F resourceRoutes() 0 50 18
A publicRoutes() 0 5 1
1
<?php
2
3
namespace Bludata\Framework\Laravel\Application;
4
5
use Closure;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Facades\Route;
8
9
class BaseApplication extends Application
10
{
11
    public function publicRoutes(string $uri, string $controller, Closure $routes = null, array $options = [])
12
    {
13
        $options['only'] = $options['only'] ?? ['index', 'show', 'destroyed'];
14
15
        return $this->resourceRoutes($uri, $controller, $routes, $options);
16
    }
17
18
    public function routesWithPermission(string $uri, string $controller, Closure $routes = null, array $options = [])
19
    {
20
        $options['only'] = $options['only'] ?? ['store', 'update', 'destroyed', 'restoreDestroyed'];
21
22
        return $this->resourceRoutes($uri, $controller, $routes, $options);
23
    }
24
25
    public function resourceRoutes(string $uri, string $controller, Closure $routes = null, array $options = [])
26
    {
27
        $options['name'] = $options['name'] ?? $uri;
28
        $options['only'] = $options['only'] ?? [];
29
        $options['except'] = $options['except'] ?? [];
30
31
        $exceptAll = isset($options['except'][0]) && $options['except'][0] == '*';
32
33
        if (!$exceptAll) {
34
            if (in_array('index', $options['only']) && !in_array('index', $options['except'])) {
35
                Route::get($uri, $controller.'@index')
36
                        ->name($options['name'].'.index');
37
            }
38
39
            if (in_array('show', $options['only']) && !in_array('show', $options['except'])) {
40
                Route::get($uri.'/{uuid}', $controller.'@show')
41
                        ->name($options['name'].'.show');
42
            }
43
44
            if (in_array('store', $options['only']) && !in_array('store', $options['except'])) {
45
                Route::post($uri, $controller.'@store')
46
                        ->name($options['name'].'.store');
47
            }
48
49
            if (in_array('update', $options['only']) && !in_array('update', $options['except'])) {
50
                Route::put($uri.'/{uuid}', $controller.'@update')
51
                        ->name($options['name'].'.update');
52
            }
53
54
            if (in_array('destroy', $options['only']) && !in_array('destroy', $options['except'])) {
55
                Route::delete($uri.'/{uuid}', $controller.'@destroy')
56
                        ->name($options['name'].'.destroy');
57
            }
58
59
            if (in_array('destroyed', $options['only']) && !in_array('destroyed', $options['except'])) {
60
                Route::get($uri.'/destroyed', $controller.'@destroyed')
61
                        ->name($options['name'].'.destroyed');
62
            }
63
64
            if (in_array('restoreDestroyed', $options['only']) && !in_array('restoreDestroyed', $options['except'])) {
65
                Route::post($uri.'/{uuid}', $controller.'@restoreDestroyed')
66
                        ->name($options['name'].'.restoreDestroyed');
67
            }
68
        }
69
70
        if ($routes instanceof Closure) {
71
            $routes($uri, $controller, $options);
72
        }
73
74
        return $this;
75
    }
76
}
77