Passed
Pull Request — 2.x (#1446)
by Harings
13:09
created

TwillRoutes   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
c 0
b 0
f 0
dl 0
loc 106
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A registerRoutes() 0 41 3
A registerCapsuleRoutes() 0 10 2
A getRouteGroupOptions() 0 6 1
A registerRoutePatterns() 0 6 4
A supportSubdomainRouting() 0 3 1
A getRouteMiddleware() 0 18 3
1
<?php
2
3
namespace A17\Twill;
4
5
use A17\Twill\Helpers\Capsule;
6
7
class TwillRoutes
8
{
9
    public function registerRoutes(
10
        $router,
11
        $groupOptions,
12
        $middlewares,
13
        $supportSubdomainRouting,
14
        $namespace,
15
        $routesFile
16
    ): void {
17
        if (file_exists($routesFile)) {
18
            $hostRoutes = function ($router) use (
19
                $middlewares,
20
                $namespace,
21
                $routesFile
22
            ) {
23
                $router->group(
24
                    [
25
                        'namespace' => $namespace,
26
                        'middleware' => $middlewares,
27
                    ],
28
                    function ($router) use ($routesFile) {
29
                        require $routesFile;
30
                    }
31
                );
32
            };
33
34
            $router->group(
35
                $groupOptions + [
36
                    'domain' => config('twill.admin_app_url'),
37
                ],
38
                $hostRoutes
39
            );
40
41
            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
    }
54
55
    public function registerRoutePatterns(): void
56
    {
57
        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
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getRouteGroupOptions(): array
70
    {
71
        return [
72
            'as' => config('twill.admin_route_name_prefix', 'admin.'),
73
            'middleware' => [config('twill.admin_middleware_group', 'web')],
74
            'prefix' => rtrim(ltrim(config('twill.admin_app_path'), '/'), '/'),
75
        ];
76
    }
77
78
    public function getRouteMiddleware($middleware = null): array
79
    {
80
        if (is_array($middleware)) {
81
            return $middleware;
82
        }
83
84
        $middleware = [
85
            'twill_auth:twill_users',
86
            'impersonate',
87
            'validateBackHistory',
88
            'localization',
89
        ];
90
91
        if ($this->supportSubdomainRouting()) {
92
            array_unshift($middleware, 'supportSubdomainRouting');
93
        }
94
95
        return $middleware;
96
    }
97
98
    public function supportSubdomainRouting()
99
    {
100
        return config('twill.support_subdomain_admin_routing', false);
101
    }
102
103
    public function registerCapsuleRoutes($router, Capsule $capsule): void
104
    {
105
        if ($capsule->routesFileExists()) {
106
            $this->registerRoutes(
107
                $router,
108
                $this->getRouteGroupOptions(),
109
                $this->getRouteMiddleware(),
110
                $this->supportSubdomainRouting(),
111
                $capsule->getControllersNamespace(),
112
                $capsule->getRoutesFile()
113
            );
114
        }
115
    }
116
}
117