Completed
Push — master ( 43176b...352566 )
by Jonathan André
12s
created

BaseApplication::resourceRoutes()   F

Complexity

Conditions 18
Paths 516

Size

Total Lines 50
Code Lines 29

Duplication

Lines 28
Ratio 56 %

Importance

Changes 0
Metric Value
cc 18
eloc 29
c 0
b 0
f 0
nc 516
nop 4
dl 28
loc 50
rs 3.2824

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
            if (in_array('index', $options['only']) && !in_array('index', $options['except'])) {
0 ignored issues
show
Bug introduced by
It seems like $options['only'] can also be of type string; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            if (in_array('index', /** @scrutinizer ignore-type */ $options['only']) && !in_array('index', $options['except'])) {
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
                Route::get($uri, $controller.'@index')
36
                     ->name($options['name'].'.index');
37
            }
38
39 View Code Duplication
            if (in_array('show', $options['only']) && !in_array('show', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
                Route::get($uri.'/{uuid}', $controller.'@show')
41
                     ->name($options['name'].'.show');
42
            }
43
44 View Code Duplication
            if (in_array('store', $options['only']) && !in_array('store', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
                Route::post($uri, $controller.'@store')
46
                     ->name($options['name'].'.store');
47
            }
48
49 View Code Duplication
            if (in_array('update', $options['only']) && !in_array('update', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
                Route::put($uri.'/{uuid}', $controller.'@update')
51
                     ->name($options['name'].'.update');
52
            }
53
54 View Code Duplication
            if (in_array('destroy', $options['only']) && !in_array('destroy', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
                Route::delete($uri.'/{uuid}', $controller.'@destroy')
56
                     ->name($options['name'].'.destroy');
57
            }
58
59 View Code Duplication
            if (in_array('destroyed', $options['only']) && !in_array('destroyed', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
                Route::get($uri.'/destroyed', $controller.'@destroyed')
61
                     ->name($options['name'].'.destroyed');
62
            }
63
64 View Code Duplication
            if (in_array('restoreDestroyed', $options['only']) && !in_array('restoreDestroyed', $options['except'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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