Base   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 47
ccs 20
cts 20
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A removeLocale() 0 6 1
A addLocale() 0 3 1
A locale() 0 3 1
A prefix() 0 11 3
A __construct() 0 3 1
A switchLocale() 0 6 1
1
<?php
2
3
namespace CaribouFute\LocaleRoute\Prefix;
4
5
use CaribouFute\LocaleRoute\LocaleConfig;
6
use Illuminate\Support\Str;
7
8
abstract class Base
9
{
10
    protected $localeConfig;
11
    protected $separator;
12
13 61
    public function __construct(LocaleConfig $localeConfig)
14
    {
15 61
        $this->localeConfig = $localeConfig;
16
    }
17
18 41
    public function switchLocale($locale, $string)
19
    {
20 41
        $unlocalized = $this->removeLocale($string);
21 41
        $localized = $this->addLocale($locale, $unlocalized);
22
23 41
        return $localized;
24
    }
25
26 45
    public function removeLocale($string)
27
    {
28 45
        $prefix = $this->prefix($string);
29 45
        $unlocalized = str_replace($prefix, '', $string);
30
31 45
        return $unlocalized;
32
    }
33
34 48
    public function prefix($string)
35
    {
36 48
        foreach ($this->localeConfig->locales() as $locale) {
37 48
            $prefix = $locale . $this->separator;
38
39 48
            if (Str::startsWith($string, $prefix)) {
40 7
                return $prefix;
41
            }
42
        }
43
44 43
        return '';
45
    }
46
47 44
    public function addLocale($locale, $string)
48
    {
49 44
        return $locale . $this->separator . $string;
50
    }
51
52 1
    public function locale($string)
53
    {
54 1
        return rtrim($this->prefix($string), $this->separator);
55
    }
56
}
57