Completed
Push — master ( 319b50...204696 )
by Frédéric
02:31
created

Route   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A localeRoute() 0 8 3
A getLocaleOrNotLocaleRouteUrl() 0 12 2
A getCurrentRouteName() 0 7 2
A otherLocale() 0 7 2
A getCurrentRouteParameters() 0 7 2
A otherRoute() 0 4 1
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
use InvalidArgumentException;
10
11
class Route extends Base
12
{
13
    protected $separator = '.';
14
    protected $url;
15
    protected $router;
16
17 57
    public function __construct(UrlGenerator $url, IlluminateRouter $router)
18
    {
19 57
        $this->url = $url;
20 57
        $this->router = $router;
21 57
    }
22
23 11
    public function localeRoute($locale = null, $name = null, $parameters = [], $absolute = true)
24
    {
25 11
        $locale = $locale ?: App::getLocale();
26 11
        $name = $name ?: $this->getCurrentRouteName();
27 11
        $localeName = $this->switchLocale($locale, $name);
28
29 11
        return $this->getLocaleOrNotLocaleRouteUrl($localeName, $name, $parameters, $absolute);
30
    }
31
32 11
    protected function getLocaleOrNotLocaleRouteUrl($localeName = null, $name = null, $parameters = [], $absolute = true)
33
    {
34
        try {
35 11
            $url = $this->url->route($localeName, $parameters, $absolute);
36 2
        } catch (InvalidArgumentException $e) {
37 2
            $url = $this->url->route($name, $parameters, $absolute);
38
        }
39
40 11
        $url = rtrim($url, '?');
41
42 11
        return $url;
43
    }
44
45 4
    public function getCurrentRouteName()
46
    {
47 4
        $currentRoute = $this->router->current();
48 4
        $currentRouteName = $currentRoute ? $currentRoute->getName() : '';
49
50 4
        return $currentRouteName;
51
    }
52
53 2
    public function otherLocale($locale = null, $parameters = null, $absolute = true)
54
    {
55 2
        $name = $this->getCurrentRouteName();
56 2
        $parameters = $parameters ?: $this->getCurrentRouteParameters();
57
58 2
        return $this->localeRoute($locale, $name, $parameters, $absolute);
59
    }
60
61 2
    public function getCurrentRouteParameters()
62
    {
63 2
        $currentRoute = $this->router->current();
64 2
        $currentRouteParameters = $currentRoute ? $currentRoute->parameters() : [];
65
66 2
        return $currentRouteParameters;
67
    }
68
69 2
    public function otherRoute($name, $parameters = null, $absolute = true)
70
    {
71 2
        return $this->localeRoute(null, $name, $parameters, $absolute);
72
    }
73
}
74