Router::makeRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 4
c 4
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
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 70
    public function __construct(IlluminateRouter $router, PrefixUrl $url, PrefixRoute $route)
18
    {
19 70
        $this->router = $router;
20 70
        $this->url = $url;
21 70
        $this->route = $route;
22
    }
23
24 3
    public function any($uri, $action = [])
25
    {
26 3
        return $this->makeRoute('any', $uri, $action);
27
    }
28
29 27
    public function get($uri, $action = [])
30
    {
31 27
        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 45
    public function makeRoute($method, $uri, $action = [])
60
    {
61 45
        $route = $this->router->$method($uri, $action);
62 45
        $route = $this->addLocale($route, $action);
63 45
        $this->refreshRoutes();
64
65 45
        return $route;
66
    }
67
68 40
    public function addLocale(Route $route, $action = [])
69
    {
70 40
        $locale = $this->getActionLocale($route);
71
72 40
        if (!$locale) {
73 1
            return $route;
74
        }
75
76 39
        $route = $this->switchRouteLocale($locale, $route);
77 39
        $route = $this->switchUrlLocale($locale, $route, $action);
78
79 39
        $route->middleware($this->makeSetSessionLocale($locale));
80
81 39
        return $route;
82
    }
83
84 41
    public function getActionLocale(Route $route)
85
    {
86 41
        $locales = $this->getActionLocales($route);
87
88 41
        return is_array($locales) ? last($locales) : $locales;
89
    }
90
91 43
    public function getActionLocales(Route $route)
92
    {
93 43
        $action = $route->getAction();
94
95 43
        return is_array($action) && isset($action['locale']) ? $action['locale'] : [];
96
    }
97
98 40
    public function switchRouteLocale($locale, Route $route)
99
    {
100 40
        $name = $route->getName();
101 40
        if (!$name) {
102 1
            return $route;
103
        }
104
105 39
        $name = $this->route->switchLocale($locale, $name);
106 39
        $route = $this->setRouteName($name, $route);
107
108 39
        return $route;
109
    }
110
111 39
    public function setRouteName($name, Route $route)
112
    {
113 39
        $action = $route->getAction();
114 39
        $action['as'] = $name;
115 39
        $route->setAction($action);
0 ignored issues
show
Bug introduced by
It seems like $action can also be of type TValue; however, parameter $action of Illuminate\Routing\Route::setAction() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $route->setAction(/** @scrutinizer ignore-type */ $action);
Loading history...
116
117 39
        return $route;
118
    }
119
120 39
    public function switchUrlLocale($locale, Route $route, array $action = [])
121
    {
122 39
        $uri = $this->url->switchLocale($locale, $route->uri(), $action);
123 39
        $route->setUri($uri);
124
125 39
        return $route;
126
    }
127
128 39
    public function makeSetSessionLocale($locale)
129
    {
130 39
        return 'locale.session:' . $locale;
131
    }
132
133 38
    public function refreshRoutes()
134
    {
135 38
        $routeCollection = new RouteCollection;
136 38
        $routeCollection->hydrate($this->router->getRoutes());
137 38
        $routeCollection->refresh();
138 38
        $this->router->setRoutes($routeCollection);
139
    }
140
141 5
    public function __call($method, $arguments)
142
    {
143 5
        return call_user_func_array([$this->router, $method], $arguments);
144
    }
145
}
146