Passed
Pull Request — 2.x (#729)
by Antonio Carlos
06:06
created

HasRoutes::getRouteMiddleware()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 18
ccs 5
cts 7
cp 0.7143
crap 3.2098
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Services\Routing;
4
5
use Illuminate\Support\Facades\Route;
6
7
trait HasRoutes
8
{
9 76
    protected function registerRoutes(
10
        $router,
11
        $groupOptions,
12
        $middlewares,
13
        $supportSubdomainRouting,
14
        $namespace,
15
        $routesFile
16
    ) {
17 76
        if (file_exists($routesFile)) {
18 75
            $hostRoutes = function ($router) use (
19 75
                $middlewares,
20 75
                $namespace,
21 75
                $routesFile
22
            ) {
23 75
                $router->group(
24
                    [
25 75
                        'namespace' => $namespace,
26 75
                        'middleware' => $middlewares,
27
                    ],
28 75
                    function ($router) use ($routesFile) {
29 75
                        require $routesFile;
30 75
                    }
31
                );
32 75
            };
33
34 75
            $router->group(
35 75
                $groupOptions + [
36 75
                    'domain' => config('twill.admin_app_url'),
37
                ],
38
                $hostRoutes
39
            );
40
41 75
            if ($supportSubdomainRouting) {
42
                $router->group(
43
                    $groupOptions + [
44
                        'domain' =>
45
                            config('twill.admin_app_subdomain', 'admin') .
46
                            '.{subdomain}.' .
47
                            config('app.url'),
48
                    ],
49
                    $hostRoutes
50
                );
51
            }
52
        }
53 76
    }
54
55 76
    public function registerRoutePatterns()
56
    {
57 76
        if (($patterns = config('twill.admin_route_patterns')) != null) {
58
            if (is_array($patterns)) {
59
                foreach ($patterns as $label => $pattern) {
60
                    Route::pattern($label, $pattern);
61
                }
62
            }
63
        }
64 76
    }
65
66
    /**
67
     * @return array
68
     */
69 76
    protected function getRouteGroupOptions(): array
70
    {
71
        $groupOptions = [
72 76
            'as' => 'admin.',
73 76
            'middleware' => [config('twill.admin_middleware_group', 'web')],
74 76
            'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
75
        ];
76
77 76
        return $groupOptions;
78
    }
79
80 76
    public function getRouteMiddleware($middleware = null)
81
    {
82 76
        if (is_array($middleware)) {
83
            return $middleware;
84
        }
85
86
        $middleware = [
87 76
            'twill_auth:twill_users',
88
            'impersonate',
89
            'validateBackHistory',
90
            'localization',
91
        ];
92
93 76
        if ($this->supportSubdomainRouting()) {
94
            array_unshift($middlewares, 'supportSubdomainRouting');
95
        }
96
97 76
        return $middleware;
98
    }
99
100 76
    public function supportSubdomainRouting()
101
    {
102 76
        return config('twill.support_subdomain_admin_routing', false);
103
    }
104
105 7
    public function registerCapsuleRoutes($router, $capsule)
106
    {
107 7
        if (file_exists($capsule['routes_file'])) {
108 7
            $this->registerRoutes(
109 7
                $router,
110 7
                $this->getRouteGroupOptions(),
111 7
                $this->getRouteMiddleware(),
112 7
                $this->supportSubdomainRouting(),
113 7
                $this->manager->capsuleNamespace(
114 7
                    $capsule['name'],
115 7
                    'controllers'
116
                ),
117 7
                $capsule['routes_file']
118
            );
119
        }
120 7
    }
121
}
122