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

UrlParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 97
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A get() 0 12 2
A set() 0 6 1
A setCustomRoot() 0 6 1
A localize() 0 7 1
A parameters() 0 6 1
A secure() 0 6 1
A rootFromApplication() 0 4 1
A resolveRoute() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Locale\Parsers;
4
5
use Illuminate\Routing\UrlGenerator;
6
use Thinktomorrow\Locale\Values\Root;
7
use Thinktomorrow\Locale\Values\Url;
8
9
class UrlParser
10
{
11
    /** @var Url */
12
    private $url;
13
14
    /** @var string */
15
    private $localeSegment = null;
16
17
    /** @var array */
18
    private $available_locales = [];
19
20
    /** @var bool */
21
    private $secure;
22
23
    /** @var array */
24
    private $parameters = [];
25
26
    /** @var UrlGenerator */
27
    private $generator;
28
29
    public function __construct(UrlGenerator $generator)
30
    {
31
        $this->generator = $generator;
32
33
        /*
34
         * Default url is the root as given by the application
35
         */
36
        $this->url = $this->rootFromApplication();
37
    }
38
39
    public function get(): string
40
    {
41
        if (is_bool($this->secure)) {
42
            $this->url->secure($this->secure);
43
        }
44
45
        return $this->generator->to(
46
            $this->url->localize($this->localeSegment, $this->available_locales)->get(),
47
            $this->parameters,
48
            $this->secure
49
        );
50
    }
51
52
    public function set(string $url): self
53
    {
54
        $this->url = Url::fromString($url);
55
56
        return $this;
57
    }
58
59
    public function setCustomRoot(Root $root)
60
    {
61
        $this->url->setCustomRoot($root);
62
63
        return $this;
64
    }
65
66
    public function localize(string $localeSegment = null, array $available_locales): self
67
    {
68
        $this->localeSegment = $localeSegment;
69
        $this->available_locales = $available_locales;
70
71
        return $this;
72
    }
73
74
    public function parameters(array $parameters = []): self
75
    {
76
        $this->parameters = $parameters;
77
78
        return $this;
79
    }
80
81
    public function secure($secure = true): self
82
    {
83
        $this->secure = (bool) $secure;
84
85
        return $this;
86
    }
87
88
    private function rootFromApplication(): Url
89
    {
90
        return Url::fromString($this->generator->formatRoot($this->generator->formatScheme($this->secure)));
91
    }
92
93
    /**
94
     * Resolve the route via the Illuminate UrlGenerator.
95
     *
96
     * @param $routekey
97
     * @param array $parameters
98
     *
99
     * @return string
100
     */
101
    public function resolveRoute($routekey, $parameters = [])
102
    {
103
        return $this->generator->route($routekey, $parameters, true);
104
    }
105
}
106