Url   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 4
b 0
f 0
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addLocale() 0 7 2
A trimUrl() 0 3 2
A rawRouteUrl() 0 6 1
A __construct() 0 4 1
A switchLocale() 0 6 1
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Prefix;
4
5
use CaribouFute\LocaleRoute\LocaleConfig;
6
use Illuminate\Translation\Translator;
7
8
class Url extends Base
9
{
10
    protected $separator = '/';
11
    protected $translator;
12
13 61
    public function __construct(LocaleConfig $localeConfig, Translator $translator)
14
    {
15 61
        parent::__construct($localeConfig);
16 61
        $this->translator = $translator;
17
    }
18
19 44
    public function trimUrl($url): string
20
    {
21 44
        return trim($url, '/ ') ?: '/';
22
    }
23
24 29
    public function rawRouteUrl($locale, $route, array $options = []): string
25
    {
26 29
        $unlocaleUrl = $options[$locale] ??
27 1
            $this->translator->get('routes.' . $route, [], $locale);
28
29 29
        return $this->trimUrl($unlocaleUrl);
30
    }
31
32 39
    public function switchLocale($locale, $url, array $options = []): string
33
    {
34 39
        $unlocalized = $this->removeLocale($url);
35 39
        $localized = $this->addLocale($locale, $unlocalized, $options);
36
37 39
        return $this->trimUrl($localized);
38
    }
39
40 41
    public function addLocale($locale, $unlocalized, $options = []): string
41
    {
42 41
        $localized = $this->localeConfig->addLocaleToUrl($options) ?
43 40
            parent::addLocale($locale, $unlocalized) :
44 2
            $unlocalized;
45
46 41
        return $this->trimUrl($localized);
47
    }
48
}
49