Completed
Pull Request — master (#47)
by Şəhriyar
12:55
created

LocalizedRouter::controller()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 16
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 18
nop 3
crap 42

1 Method

Rating   Name   Duplication   Size   Complexity  
B LocalizedRouter::localizeUris() 0 28 6
1
<?php namespace App\Services\Routing;
2
3
use Illuminate\Container\Container;
4
use Illuminate\Contracts\Events\Dispatcher;
5
use Illuminate\Routing\Router;
6
7
class LocalizedRouter extends Router
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12 15
    public function __construct(Dispatcher $events, Container $container = null)
13
    {
14 15
        parent::__construct($events, $container);
15
16 15
        $this->routes = new LocalizedRouteCollection;
17 15
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 15
    protected function addRoute($methods, $uri, $action)
23
    {
24 15
        if (isset($action['locale'])) {
25
            // Now we apply our Localization modifications.
26 15
            $uri = $this->localizeUris($uri, $action['locale']);
27
        }
28
29 15
        return parent::addRoute($methods, $uri, $action);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 15
    public function resource($name, $controller, array $options = [])
36
    {
37 15
        if ($this->container && $this->container->bound(LocalizedResourceRegistrar::class)) {
38
            $registrar = $this->container->make(LocalizedResourceRegistrar::class);
39
        } else {
40 15
            $registrar = new LocalizedResourceRegistrar($this);
41
        }
42
43 15
        $registrar->register($name, $controller, $options);
44 15
    }
45
46
    /**
47
     * Translates URIs
48
     *
49
     * @param string $uri
50
     * @param string $locale
51
     *
52
     * @return string
53
     */
54 15
    private function localizeUris($uri, $locale)
55
    {
56 15
        $uriExploded = explode('/', trim($uri, '/'));
57 15
        $localizedUriTranslationBitParts = [];
58 15
        while (list($level, $bitName) = each($uriExploded)) {
59 15
            if ($level == 0) {
60 15
                $localizedUriTranslationBitParts[$level] = 'routes.' . $bitName . '.';
61
            } else {
62 15
                $localizedUriTranslationBitParts[$level] = trim($localizedUriTranslationBitParts[$level - 1], '.') . '.' . $bitName;
63
            }
64
        }
65 15
        foreach ($localizedUriTranslationBitParts as $level => &$translationBitPart) {
66 15
            $phraseToGetTranslationFor = $translationBitPart;
67 15
            if (preg_match('#(?<!routes)\.\{[^\}]+\}\.#', $translationBitPart)) { // For lower-level paths, in order not to hit 'routes.' index.
68
                $phraseToGetTranslationFor = preg_replace('#\{[^\}]+\}\.?#', '', $translationBitPart);
69
            }
70 15
            app('translator')->setLocale($locale);
71 15
            $translatedPhrase = app('translator')->get($phraseToGetTranslationFor);
72 15
            if (false !== strpos($translatedPhrase, '.')) {
73 15
                $translationBitPart = $uriExploded[$level];
74
            } else {
75 15
                $translationBitPart = $translatedPhrase;
76
            }
77 15
            unset($translationBitPart); // Delete the reference (won't delete the original).
78
        }
79
80 15
        return implode('/', $localizedUriTranslationBitParts);
81
    }
82
}
83