Completed
Pull Request — master (#47)
by Şəhriyar
33:20
created

LocalizedRouteCollection::getByAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php namespace App\Services\Routing;
2
3
use Illuminate\Routing\Route;
4
use Illuminate\Routing\RouteCollection;
5
6
class LocalizedRouteCollection extends RouteCollection
7
{
8
    /**
9
     * {@inheritdoc}
10
     */
11
    protected function addLookups($route)
12
    {
13 19
        $locale = app('translator')->getLocale();
14
15 19
        // If the route has a name, we will add it to the name look-up table so that we
16
        // will quickly be able to find any route associate with a name and not have
17
        // to iterate through every route every time we need to perform a look-up.
18
        $action = $route->getAction();
19
20 19
        if (isset($action['as'])) {
21
            $this->nameList[$action['as']][$locale] = $route;
22 19
        }
23 19
24
        // When the route is routing to a controller we will also store the action that
25
        // is used by the route. This will let us reverse route to controllers while
26
        // processing a request and easily generate URLs to the given controllers.
27
        if (isset($action['controller'])) {
28
            $this->addToActionList($action, $route);
29 19
        }
30 19
    }
31
32 19
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getByName($name)
36
    {
37
        $locale = app('translator')->getLocale();
38
        if (is_array($this->nameList[$name])) { // array with localized routes
39
            return $this->nameList[$name][$locale];
40
        } elseif ($this->nameList[$name] instanceof Route) { // no localization available, so it'll be a route object
41
            return $this->nameList[$name];
42 19
        }
43
44 19
        return null;
45 19
    }
46 19
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getByAction($action)
51
    {
52
        $locale = app('translator')->getLocale();
53
        if (is_array($this->actionList[$action])) { // array with localized routes
54
            return $this->actionList[$action][$locale];
55 4
        } elseif ($this->actionList[$action] instanceof Route) { // no localization available, so it'll be a route object
56
            return $this->actionList[$action];
57 4
        }
58
59 4
        return null;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function refreshNameLookups()
66
    {
67
        $this->nameList = [];
68 4
69
        /** @var \Illuminate\Routing\Route $route */
70 4
        foreach ($this->allRoutes as $route) {
71
            if ($route->getName()) {
72 4
                if (isset($route->getAction()['locale'])) {
73 4
                    $this->nameList[$route->getName()][$route->getAction()['locale']] = $route;
74
                } else {
75
                    $this->nameList[$route->getName()] = $route;
76
                }
77
            }
78
        }
79
    }
80
}
81