Passed
Push — master ( faa96a...cae79b )
by Rafael
41:05
created

UrlHelper::unparseUrl()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 6.5222
cc 9
nc 256
nop 0
crap 90
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Url;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
/**
18
 * Class UrlHelper
19
 *
20
 * @author Timo Hund <[email protected]>
21
 */
22
class UrlHelper {
23
24
    /**
25
     * @var string
26
     */
27
    protected $initialUrl;
28
29
    /**
30
     * @var array
31
     */
32
    protected $urlParts = [];
33
34
    /**
35
     * @var array
36
     */
37
    protected $queryParts = [];
38
39
    /**
40
     * @var bool
41
     */
42
    protected $wasParsed = false;
43
44
    /**
45
     * UrlHelper constructor.
46
     * @param string $url
47
     */
48 40
    public function __construct($url)
49
    {
50 40
        $this->initialUrl = $url;
51 40
        $this->parseInitialUrl();
52 40
    }
53
54
    /**
55
     * @return void
56
     */
57 40
    protected function parseInitialUrl()
58
    {
59 40
        if ($this->wasParsed) {
60
            return;
61
        }
62 40
        $parts = parse_url($this->initialUrl);
63 40
        if (!is_array($parts)) {
0 ignored issues
show
introduced by
The condition is_array($parts) is always true.
Loading history...
64
            throw new \InvalidArgumentException("Non parseable url passed to UrlHelper", 1498751529);
65
        }
66 40
        $this->urlParts = $parts;
67
68 40
        parse_str($this->urlParts['query'], $this->queryParts);
69
70 40
        $this->wasParsed = true;
71 40
    }
72
73
    /**
74
     * @param string $part
75
     * @param mixed $value
76
     */
77
    protected function setUrlPart($part, $value)
78
    {
79
        $this->urlParts[$part] = $value;
80
    }
81
82
    /**
83
     * @param $path
84
     * @return mixed
85
     */
86 3
    protected function getUrlPart($path)
87
    {
88 3
        return $this->urlParts[$path];
89
    }
90
91
    /**
92
     * @param string $host
93
     * @return UrlHelper
94
     */
95
    public function setHost(string $host)
96
    {
97
        $this->setUrlPart('host', $host);
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 3
    public function getHost(): string
105
    {
106 3
        return $this->getUrlPart('host');
107
    }
108
109
    /**
110
     * @param string $port
111
     * @return UrlHelper
112
     */
113
    public function setPort(string $port)
114
    {
115
        $this->setUrlPart('port', $port);
116
        return $this;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 3
    public function getPort(): string
123
    {
124 3
        return $this->getUrlPart('port');
125
    }
126
127
    /**
128
     * @param string $scheme
129
     * @return UrlHelper
130
     */
131
    public function setScheme(string $scheme)
132
    {
133
        $this->setUrlPart('scheme', $scheme);
134
        return $this;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getScheme(): string
141
    {
142
        return $this->getUrlPart('scheme');
143
    }
144
145
    /**
146
     * @param string $path
147
     * @return UrlHelper
148
     */
149
    public function setPath($path)
150 37
    {
151
        $this->setUrlPart('path', $path);
152 37
        return $this;
153 37
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getPath(): string
159
    {
160
        return $this->getUrlPart('path');
161
    }
162
163
    /**
164
     * @param string $parameterName
165
     * @throws \InvalidArgumentException
166
     * @return UrlHelper
167
     */
168
    public function removeQueryParameter(string $parameterName): UrlHelper
169
    {
170
        unset($this->queryParts[$parameterName]);
171 37
        return $this;
172
    }
173 37
174 37
    /**
175
     * @param string $parameterName
176
     * @param mixed $value
177
     * @throws \InvalidArgumentException
178
     * @return UrlHelper
179
     */
180 37
    public function addQueryParameter(string $parameterName, $value): UrlHelper
181
    {
182 37
        $this->queryParts[$parameterName] = $value;
183 37
        return $this;
184 37
    }
185 37
186 37
    /**
187 37
     * @return string
188 37
     */
189 37
    public function getUrl(): string
190 37
    {
191 37
        $this->urlParts['query'] = http_build_query($this->queryParts);
192 37
        return $this->unparseUrl();
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    protected function unparseUrl(): string
199
    {
200
        $scheme   = isset($this->urlParts['scheme']) ? $this->urlParts['scheme'] . '://' : '';
201
        $host     = $this->urlParts['host'] ?? '';
202
        $port     = $this->urlParts['port'] ? ':' . $this->urlParts['port'] : '';
203
        $user     = $this->urlParts['user'] ?? '';
204
        $user     = $this->urlParts['pass'] ? $user . ':' : $user;
205
        $pass     = $this->urlParts['pass'] ?? '';
206
        $pass     = ($user || $pass) ? "$pass@" : '';
207
        $path     = $this->urlParts['path'] ?? '';
208
        $query    = isset($this->urlParts['query']) && !empty($this->urlParts['query']) ? '?' . $this->urlParts['query'] : '';
209
        $fragment = isset($this->urlParts['fragment']) ? '#' . $this->urlParts['fragment'] : '';
210
        return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
211
    }
212
}
213