Completed
Branch feature/router-decorator (f7629b)
by Frédéric
02:12
created

addSetSessionLocaleMiddlewareToAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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