Completed
Push — master ( 3da2aa...854c20 )
by Frédéric
02:21
created

Router::getRouter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Routing;
4
5
use CaribouFute\LocaleRoute\Prefix\Route as PrefixRoute;
6
use CaribouFute\LocaleRoute\Prefix\Url as PrefixUrl;
7
use CaribouFute\LocaleRoute\Routing\RouteCollection;
8
use Illuminate\Contracts\Routing\Registrar as IlluminateRouter;
9
use Illuminate\Routing\Route;
10
11
class Router
12
{
13
    protected $router;
14
    protected $url;
15
    protected $route;
16
17 68
    public function __construct(IlluminateRouter $router, PrefixUrl $url, PrefixRoute $route)
18
    {
19 68
        $this->router = $router;
20 68
        $this->url = $url;
21 68
        $this->route = $route;
22 68
    }
23
24
    public function getRouter()
25
    {
26
        return $this->router;
27
    }
28
29 21
    public function get($uri, $action = [])
30
    {
31 21
        return $this->makeRoute('get', $uri, $action);
32
    }
33
34 3
    public function post($uri, $action = [])
35
    {
36 3
        return $this->makeRoute('post', $uri, $action);
37
    }
38
39 3
    public function put($uri, $action = [])
40
    {
41 3
        return $this->makeRoute('put', $uri, $action);
42
    }
43
44 3
    public function patch($uri, $action = [])
45
    {
46 3
        return $this->makeRoute('patch', $uri, $action);
47
    }
48
49 3
    public function delete($uri, $action = [])
50
    {
51 3
        return $this->makeRoute('delete', $uri, $action);
52
    }
53
54 3
    public function options($uri, $action = [])
55
    {
56 3
        return $this->makeRoute('options', $uri, $action);
57
    }
58
59 36
    public function makeRoute($method, $uri, $action = [])
60
    {
61 36
        $route = $this->router->$method($uri, $action);
62 36
        $route = $this->addLocale($route, $action);
63 36
        $this->refreshRoutes();
64
65 36
        return $route;
66
    }
67
68 32
    public function addLocale(Route $route, $action = [])
69
    {
70 32
        $locale = $this->getActionLocale($route);
71
72 32
        if (!$locale) {
73 1
            return $route;
74
        }
75
76 31
        $route = $this->switchRouteLocale($locale, $route);
77 31
        $route = $this->switchUrlLocale($locale, $route, $action);
78
79 31
        $route->middleware($this->makeSetSessionLocale($locale));
80
81 31
        return $route;
82
    }
83
84 33
    public function getActionLocale(Route $route)
85
    {
86 33
        $locales = $this->getActionLocales($route);
87
88 33
        return is_array($locales) ? last($locales) : $locales;
89
    }
90
91 35
    public function getActionLocales(Route $route)
92
    {
93 35
        $action = $route->getAction();
94
95 35
        return is_array($action) && isset($action['locale']) ? $action['locale'] : [];
96
    }
97
98 32
    public function switchRouteLocale($locale, Route $route)
99
    {
100 32
        $name = $route->getName();
101 32
        if (!$name) {
102 1
            return $route;
103
        }
104
105 31
        $name = $this->route->switchLocale($locale, $name);
106 31
        $route = $this->setRouteName($name, $route);
107
108 31
        return $route;
109
    }
110
111 31
    public function setRouteName($name, Route $route)
112
    {
113 31
        $action = $route->getAction();
114 31
        $action['as'] = $name;
115 31
        $route->setAction($action);
116
117 31
        return $route;
118
    }
119
120 31
    public function switchUrlLocale($locale, Route $route, array $action = [])
121
    {
122 31
        $uri = $this->url->switchLocale($locale, $route->uri(), $action);
123 31
        $route->setUri($uri);
124
125 31
        return $route;
126
    }
127
128 31
    public function makeSetSessionLocale($locale)
129
    {
130 31
        return 'locale.session:' . $locale;
131
    }
132
133 30
    public function refreshRoutes()
134
    {
135 30
        $routeCollection = new RouteCollection;
136 30
        $routeCollection->hydrate($this->router->getRoutes());
137 30
        $routeCollection->refresh();
138 30
        $this->router->setRoutes($routeCollection);
139 30
    }
140
141 5
    public function __call($method, $arguments)
142
    {
143 5
        return call_user_func_array([$this->router, $method], $arguments);
144
    }
145
}
146