Passed
Branch 2.0 (72e182)
by Philippe
02:18
created

RouteParser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 16 3
A set() 0 10 1
A reset() 0 9 1
A setCustomRoot() 0 6 1
A localize() 0 12 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\Root;
7
8
class RouteParser
9
{
10
    /** @var string */
11
    private $routename;
12
13
    /** @var string */
14
    private $locale;
15
16
    /** @var string */
17
    private $localeSegment = null;
18
19
    /** @var array */
20
    private $available_locales = [];
21
22
    /** @var bool */
23
    private $secure = null;
24
25
    /** @var array */
26
    private $parameters = [];
27
28
    /** @var Root */
29
    private $customRoot;
30
31
    /** @var Translator */
32
    private $translator;
33
34
    /** @var UrlParser */
35
    private $urlParser;
36
37 55
    public function __construct(UrlParser $urlParser , Translator $translator)
38
    {
39 55
        $this->urlParser = $urlParser;
40 55
        $this->translator = $translator;
41 55
    }
42
43 39
    public function get(): string
44
    {
45
        // TODO: take route filename from config
46 39
        $url = $this->translator->get('routes.'.$this->routename, [], $this->locale);
47
48
        // If route translation does not exist, laravel returns us back the langkey.
49 39
        $url = ($url == 'routes.'.$this->routename)
50 32
            ? $this->resolveRoute($this->routename, $this->parameters)
51 37
            : UriParameters::replace($url, $this->parameters);
52
53 37
        $parser = $this->urlParser->set($url)->secure($this->secure)->localize($this->localeSegment, $this->available_locales);
54
55 37
        if($this->customRoot) $parser->setCustomRoot($this->customRoot);
56
57 37
        return $parser->get();
58
    }
59
60 39
    public function set(string $routename, array $parameters = [], $secure = null)
61
    {
62 39
        $this->reset();
63
64 39
        $this->routename = $routename;
65 39
        $this->parameters = $parameters;
66 39
        $this->secure = $secure;
67
68 39
        return $this;
69
    }
70
71 39
    private function reset()
72
    {
73 39
        $this->routename = null;
74 39
        $this->customRoot = null;
75 39
        $this->secure = null;
76 39
        $this->parameters = [];
77 39
        $this->locale = null;
78 39
        $this->localeSegment = null;
79 39
    }
80
81 8
    public function setCustomRoot(Root $customRoot)
82
    {
83 8
        $this->customRoot = $customRoot;
84
85 8
        return $this;
86
    }
87
88 38
    public function localize(string $localeSegment = null, array $available_locales): self
89
    {
90 38
        $this->localeSegment = $localeSegment;
91 38
        $this->available_locales = $available_locales;
92
93
        // Our route translator requires the corresponding locale
94 38
        $this->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...
95 24
            ? $available_locales['/']
96 32
            : $available_locales[$localeSegment];
97
98 38
        return $this;
99
    }
100
101 32
    public function resolveRoute($routekey, $parameters = [])
102
    {
103 32
        return $this->urlParser->resolveRoute($routekey, $parameters, true);
0 ignored issues
show
Unused Code introduced by
The call to UrlParser::resolveRoute() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
104
    }
105
}
106