Completed
Pull Request — master (#47)
by Şəhriyar
10:29
created

LocalizedRouteCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 74
ccs 12
cts 20
cp 0.6
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addToActionList() 0 5 1
A getByAction() 0 6 2
A getByName() 0 10 3
A addLookups() 0 20 3
1
<?php namespace App\Services\Routing;
2
3
use Illuminate\Routing\RouteCollection;
4
5
class LocalizedRouteCollection extends RouteCollection
6
{
7
    /**
8
     * Add the route to any look-up tables if necessary.
9
     *
10
     * @param  \Illuminate\Routing\Route  $route
11
     * @return void
12
     */
13 14
    protected function addLookups($route)
14
    {
15 14
        $locale = app('translator')->getLocale();
16
17
        // If the route has a name, we will add it to the name look-up table so that we
18
        // will quickly be able to find any route associate with a name and not have
19
        // to iterate through every route every time we need to perform a look-up.
20 14
        $action = $route->getAction();
21
22 14
        if (isset($action['as'])) {
23 14
            $this->nameList[$action['as']][$locale] = $route;
24
        }
25
26
        // When the route is routing to a controller we will also store the action that
27
        // is used by the route. This will let us reverse route to controllers while
28
        // processing a request and easily generate URLs to the given controllers.
29 14
        if (isset($action['controller'])) {
30 14
            $this->addToActionList($action, $route);
31
        }
32 14
    }
33
34
    /**
35
     * Add a route to the controller action dictionary.
36
     *
37
     * @param  array                     $action
38
     * @param  \Illuminate\Routing\Route $route
39
     *
40
     * @return void
41
     */
42 14
    protected function addToActionList($action, $route)
43
    {
44 14
        $locale = app('translator')->getLocale();
45 14
        $this->actionList[trim($action['controller'], '\\')][$locale] = $route;
46 14
    }
47
48
    /**
49
     * Get a route instance by its controller action.
50
     *
51
     * @param  string $action
52
     *
53
     * @return \Illuminate\Routing\Route|null
54
     */
55
    public function getByAction($action)
56
    {
57
        $locale = app('translator')->getLocale();
58
59
        return isset($this->actionList[$action][$locale]) ? $this->actionList[$action][$locale] : null;
60
    }
61
62
    /**
63
     * Get a route instance by its name.
64
     *
65
     * @param  string  $name
66
     * @return \Illuminate\Routing\Route|null
67
     */
68
    public function getByName($name)
69
    {
70
        $locale = app('translator')->getLocale();
71
72
        if (isset($this->nameList[$name])) {
73
            return $this->nameList[$name];
74
        }
75
76
        return isset($this->nameList[$locale . '.' . $name]) ? $this->nameList[$locale . '.' . $name] : null;
77
    }
78
}
79