Test Failed
Pull Request — master (#122)
by
unknown
30:03 queued 23:05
created

RouteParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 103
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 19 3
A set() 0 10 1
A reset() 0 9 1
A setCustomRoot() 0 6 1
A localize() 0 14 3
A resolveRoute() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Locale\Parsers;
4
5
use Illuminate\Contracts\Translation\Translator;
6
use Thinktomorrow\Locale\Values\ApplicationLocale;
7
use Thinktomorrow\Locale\Values\Root;
8
9
class RouteParser
10
{
11
    /** @var string */
12
    private $routename;
13
14
    /** @var string */
15
    private $locale;
16
17
    /** @var string */
18
    private $localeSegment = null;
19
20
    /** @var array */
21
    private $available_locales = [];
22
23
    /** @var bool */
24
    private $secure = null;
25
26
    /** @var array */
27
    private $parameters = [];
28
29
    /** @var Root */
30
    private $customRoot;
31
32
    /** @var Translator */
33
    private $translator;
34
35
    /** @var UrlParser */
36
    private $urlParser;
37
38
    public function __construct(UrlParser $urlParser, Translator $translator)
39
    {
40
        $this->urlParser = $urlParser;
41
        $this->translator = $translator;
42
    }
43
44
    public function get(): string
45
    {
46
        $translationKey = config('thinktomorrow.locale.routes_filename').'.'.$this->routename;
47
48
        $url = $this->translator->get($translationKey, [], $this->locale);
49
50
        // If route translation does not exist, laravel returns us back the langkey.
51
        $url = ($url == $translationKey)
52
            ? $this->resolveRoute($this->routename, $this->parameters)
53
            : UriParameters::replace($url, $this->parameters);
54
55
        $parser = $this->urlParser->set($url)->secure($this->secure)->localize($this->localeSegment, $this->available_locales);
56
57
        if ($this->customRoot) {
58
            $parser->setCustomRoot($this->customRoot);
59
        }
60
61
        return $parser->get();
62
    }
63
64
    public function set(string $routename, array $parameters = [], $secure = null)
65
    {
66
        $this->reset();
67
68
        $this->routename = $routename;
69
        $this->parameters = SanitizeParameters::rawurlencode($parameters);
70
        $this->secure = $secure;
71
72
        return $this;
73
    }
74
75
    private function reset()
76
    {
77
        $this->routename = null;
78
        $this->customRoot = null;
79
        $this->secure = null;
80
        $this->parameters = [];
81
        $this->locale = null;
82
        $this->localeSegment = null;
83
    }
84
85
    public function setCustomRoot(Root $customRoot)
86
    {
87
        $this->customRoot = $customRoot;
88
89
        return $this;
90
    }
91
92
    public function localize(string $localeSegment = null, array $available_locales): self
93
    {
94
        $this->localeSegment = $localeSegment;
95
        $this->available_locales = $available_locales;
96
97
        // Our route translator requires the corresponding application locale
98
        $locale = (!$localeSegment || $localeSegment == '/')
0 ignored issues
show
Bug Best Practice introduced by
The expression $localeSegment of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
99
            ? $available_locales['/']
100
            : $available_locales[$localeSegment];
101
102
        $this->locale = ApplicationLocale::from($locale)->__toString();
103
104
        return $this;
105
    }
106
107
    public function resolveRoute($routekey, $parameters = [])
108
    {
109
        return $this->urlParser->resolveRoute($routekey, $parameters);
110
    }
111
}
112