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

LocalizedRouteCollection::getByAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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