Completed
Push — master ( 1d9f9e...861a2b )
by Frédéric
02:08
created

LocaleRouter::any()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Routing;
4
5
use CaribouFute\LocaleRoute\LocaleConfig;
6
use CaribouFute\LocaleRoute\Prefix\Route as PrefixRoute;
7
use CaribouFute\LocaleRoute\Prefix\Url as PrefixUrl;
8
use Closure;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Routing\Route;
11
12
class LocaleRouter
13
{
14
    protected $localeConfig;
15
    protected $router;
16
    protected $prefixRoute;
17
    protected $prefixUrl;
18
    protected $app;
19
20 49
    public function __construct(
21
        LocaleConfig $localeConfig,
22
        Router $router,
23
        PrefixRoute $prefixRoute,
24
        PrefixUrl $prefixUrl,
25
        Application $app
26
    ) {
27 49
        $this->localeConfig = $localeConfig;
28 49
        $this->router = $router;
29 49
        $this->prefixRoute = $prefixRoute;
30 49
        $this->prefixUrl = $prefixUrl;
31 49
        $this->app = $app;
32 49
    }
33
34 1
    public function any($route, $action, $options = []): RouteCollection
35
    {
36 1
        return $this->makeRoutes('any', $route, $action, $options);
37
    }
38
39 17
    public function get($route, $action, $options = []): RouteCollection
40
    {
41 17
        return $this->makeRoutes('get', $route, $action, $options);
42
    }
43
44 1
    public function post($route, $action, $options = []): RouteCollection
45
    {
46 1
        return $this->makeRoutes('post', $route, $action, $options);
47
    }
48
49 1
    public function put($route, $action, $options = []): RouteCollection
50
    {
51 1
        return $this->makeRoutes('put', $route, $action, $options);
52
    }
53
54 1
    public function patch($route, $action, $options = []): RouteCollection
55
    {
56 1
        return $this->makeRoutes('patch', $route, $action, $options);
57
    }
58
59 1
    public function delete($route, $action, $options = []): RouteCollection
60
    {
61 1
        return $this->makeRoutes('delete', $route, $action, $options);
62
    }
63
64 1
    public function options($route, $action, $options = []): RouteCollection
65
    {
66 1
        return $this->makeRoutes('options', $route, $action, $options);
67
    }
68
69 23
    public function makeRoutes($method, $route, $action, $options = []): RouteCollection
70
    {
71 23
        $options = is_string($options) ? $this->convertOptionUrlsToArray($options) : $options;
72 23
        $routeCollection = new RouteCollection();
73
74 23
        foreach ($this->localeConfig->locales($options) as $locale) {
75 23
            $routeObject = $this->makeRoute($locale, $method, $route, $action, $options);
76 23
            $routeCollection->add($routeObject);
77
        }
78
79 23
        return $routeCollection;
80
    }
81
82 4
    protected function convertOptionUrlsToArray($options): array
83
    {
84 4
        $newOptions = [];
85
86 4
        foreach ($this->localeConfig->locales() as $locale) {
87 4
            $newOptions[$locale] = $options;
88
        }
89
90 4
        return $newOptions;
91
    }
92
93 23
    public function makeRoute($locale, $method, $route, $action, $options = []): Route
94
    {
95 23
        $url = $this->prefixUrl->rawRouteUrl($locale, $route, $options);
96
97 23
        $action = $this->convertToControllerAction($action);
98 23
        $action = $this->fillAction($locale, $route, $action, $options);
99
100 23
        $middleware = isset($options['middleware']) ? $options['middleware'] : [];
101
102 23
        $route = $this->router
103 23
            ->$method($url, $action)
104 23
            ->middleware($middleware);
105
106 23
        return $route;
107
    }
108
109 23
    protected function convertToControllerAction($action)
110
    {
111 23
        return is_string($action) || is_a($action, Closure::class) ?
112 23
            ['uses' => $action] :
113 23
            $action;
114
    }
115
116 23
    protected function fillAction($locale, $route, $action, $options): array
117
    {
118 23
        $action['locale'] = $locale;
119 23
        $action['as'] = $route;
120
121 23
        if (isset($options['add_locale_to_url'])) {
122 1
            $action['add_locale_to_url'] = $options['add_locale_to_url'];
123
        }
124
125 23
        return $action;
126
    }
127
128 3
    public function resource($route, $controller, $options = [])
129
    {
130 3
        $registrar = $this->app->make(ResourceRegistrar::class);
131 3
        $registrar->register($route, $controller, $options);
132 3
    }
133
}
134