Completed
Push — master ( 0eceaa...aee3a6 )
by Ben
15:21 queued 05:47
created

UrlParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 29
cts 29
cp 1
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 10 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 63
    public function __construct(UrlGenerator $generator)
30
    {
31 63
        $this->generator = $generator;
32
33
        /*
34
         * Default url is the root as given by the application
35
         */
36 63
        $this->url = $this->rootFromApplication();
37 63
    }
38
39 37
    public function get(): string
40
    {
41 37
        if (is_bool($this->secure)) {
42 28
            $this->url->secure($this->secure);
43
        }
44
45 37
        return $this->generator->to(
46 37
            $this->url->localize($this->localeSegment, $this->available_locales)->get(), $this->parameters, $this->secure
47
        );
48
    }
49
50 38
    public function set(string $url): self
51
    {
52 38
        $this->url = Url::fromString($url);
53
54 37
        return $this;
55
    }
56
57 8
    public function setCustomRoot(Root $root)
58
    {
59 8
        $this->url->setCustomRoot($root);
60
61 8
        return $this;
62
    }
63
64 36
    public function localize(string $localeSegment = null, array $available_locales): self
65
    {
66 36
        $this->localeSegment = $localeSegment;
67 36
        $this->available_locales = $available_locales;
68
69 36
        return $this;
70
    }
71
72 7
    public function parameters(array $parameters = []): self
73
    {
74 7
        $this->parameters = $parameters;
75
76 7
        return $this;
77
    }
78
79 28
    public function secure($secure = true): self
80
    {
81 28
        $this->secure = (bool) $secure;
82
83 28
        return $this;
84
    }
85
86 63
    private function rootFromApplication(): Url
87
    {
88 63
        return Url::fromString($this->generator->formatRoot($this->generator->formatScheme($this->secure)));
89
    }
90
91
    /**
92
     * Resolve the route via the Illuminate UrlGenerator.
93
     *
94
     * @param $routekey
95
     * @param array $parameters
96
     *
97
     * @return string
98
     */
99 23
    public function resolveRoute($routekey, $parameters = [])
100
    {
101 23
        return $this->generator->route($routekey, $parameters, true);
102
    }
103
}
104