Completed
Push — master ( f6dfaa...d4ac13 )
by Ben
03:58
created

LocaleUrl::extractLocaleFromParameters()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 5
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Thinktomorrow\Locale\Services\UrlParser;
7
8
class LocaleUrl
9
{
10
    /**
11
     * @var Locale
12
     */
13
    private $locale;
14
15
    /**
16
     * @var UrlParser
17
     */
18
    private $parser;
19
20
    /**
21
     * @var Illuminate\Contracts\Routing\UrlGenerator
22
     */
23
    private $generator;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $placeholder;
29
30 51
    public function __construct(Locale $locale, UrlParser $parser, UrlGenerator $generator, $config = [])
31
    {
32 51
        $this->locale = $locale;
33 51
        $this->parser = $parser;
34 51
        $this->generator = $generator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $generator of type object<Illuminate\Contracts\Routing\UrlGenerator> is incompatible with the declared type object<Thinktomorrow\Loc...s\Routing\UrlGenerator> of property $generator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
36 51
        $this->placeholder = isset($config['placeholder']) ? $config['placeholder'] : null;
37 51
    }
38
39
    /**
40
     * Generate a localized url
41
     *
42
     * @param $url
43
     * @param null $locale
44
     * @param array $extra
45
     * @param null $secure
46
     * @return mixed
47
     */
48 51
    public function to($url, $locale = null, $extra = [], $secure = null)
49
    {
50 51
        $url = $this->parser->set($url)
51 51
                            ->localize($locale)
52 51
                            ->get();
53
54 51
        return $this->resolveUrl($url, $extra, $secure);
55
    }
56
57
    /**
58
     * Generate a localized route
59
     *
60
     * @param $name
61
     * @param array $parameters
62
     * @param bool $absolute
63
     * @return mixed
64
     */
65 33
    public function route($name, $parameters = [], $absolute = true)
66
    {
67 33
        $locale = $this->extractLocaleFromParameters($parameters);
68
69 33
        $url = $this->resolveRoute($name,$parameters,$absolute);
70
71 33
        return $this->to($url,$locale);
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $this->extractLocaleFromParameters($parameters) on line 67 can also be of type string; however, Thinktomorrow\Locale\LocaleUrl::to() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
72
    }
73
74
    /**
75
     * Isolate locale value from parameters
76
     *
77
     * @param array $parameters
78
     * @return null|string
79
     */
80 33
    private function extractLocaleFromParameters(&$parameters = [])
81
    {
82 33
        $locale = null;
83
84 33
        if(!is_array($parameters))
85 20
        {
86 13
            $locale = $this->locale->get($parameters);
87
88
            // If locale is the only parameter, we make sure the 'real' parameters is flushed
89 13
            if($locale == $parameters) $parameters = [];
90 8
        }
91 28
        elseif(!is_null($this->placeholder) && array_key_exists($this->placeholder,$parameters))
92
        {
93 5
            $locale = $this->locale->get($parameters[$this->placeholder]);
94 5
            unset($parameters[$this->placeholder]);
95 3
        }
96
97 33
        return $locale;
98
    }
99
100
    /**
101
     * Generate url via illuminate
102
     *
103
     * @param $url
104
     * @param array $extra
105
     * @param null $secure
106
     * @return string
107
     */
108 51
    private function resolveUrl($url, $extra = [], $secure = null)
109
    {
110 51
        return $this->generator->to($url, $extra, $secure);
111
    }
112
113
    /**
114
     * Generate route via illuminate
115
     *
116
     * @param $name
117
     * @param array $parameters
118
     * @param bool $absolute
119
     * @return string
120
     */
121 33
    private function resolveRoute($name, $parameters = [], $absolute = true)
122
    {
123 33
        return $this->generator->route($name, $parameters, $absolute);
124
    }
125
}