Url::addLocale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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