Passed
Push — master ( 1438ee...6641ad )
by Quentin
06:42 queued 12s
created

HasRoutes   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 57
dl 0
loc 122
rs 10
c 2
b 0
f 0
wmc 15

7 Methods

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