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

Route::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Prefix;
4
5
use App;
6
use CaribouFute\LocaleRoute\Prefix\Base;
7
use Illuminate\Routing\Router as IlluminateRouter;
8
use Illuminate\Routing\UrlGenerator;
9
10
class Route extends Base
11
{
12
    protected $separator = '.';
13
    protected $url;
14
    protected $router;
15
16 46
    public function __construct(UrlGenerator $url, IlluminateRouter $router)
17
    {
18 46
        $this->url = $url;
19 46
        $this->router = $router;
20 46
    }
21
22 8
    public function localeRoute($locale = null, $name = null, $parameters = [], $absolute = true)
23
    {
24 8
        $locale = $locale ?: App::getLocale();
25 8
        $name = $name ?: $this->getCurrentRouteName();
26
27 8
        $localeRoute = $this->switchLocale($locale, $name);
28 8
        $localeUrl = $this->url->route($localeRoute, $parameters, $absolute);
29 8
        $localeUrl = rtrim($localeUrl, '?');
30
31 8
        return $localeUrl;
32
    }
33
34 3
    public function getCurrentRouteName()
35
    {
36 3
        $currentRoute = $this->router->current();
37 3
        $currentRouteName = $currentRoute ? $currentRoute->getName() : '';
38
39 3
        return $currentRouteName;
40
    }
41
42 1
    public function otherLocale($locale = null, $parameters = null, $absolute = true)
43
    {
44 1
        $name = $this->getCurrentRouteName();
45 1
        $parameters = $parameters ?: $this->getCurrentRouteParameters();
46
47 1
        return $this->localeRoute($locale, $name, $parameters, $absolute);
48
    }
49
50 1
    public function getCurrentRouteParameters()
51
    {
52 1
        $currentRoute = $this->router->current();
53 1
        $currentRouteParameters = $currentRoute ? $currentRoute->parameters() : [];
54
55 1
        return $currentRouteParameters;
56
    }
57
58 2
    public function otherRoute($name, $parameters = null, $absolute = true)
59
    {
60 2
        return $this->localeRoute(null, $name, $parameters, $absolute);
61
    }
62
}
63