Passed
Push — master ( d5568c...ac874d )
by Timo
28:49 queued 03:56
created

UrlHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 72
    public function __construct($url)
49
    {
50 72
        $this->initialUrl = $url;
51 72
        $this->parseInitialUrl();
52 72
    }
53
54
    /**
55
     * @return void
56
     */
57 72
    protected function parseInitialUrl()
58
    {
59 72
        if ($this->wasParsed) {
60
            return;
61
        }
62 72
        $parts = parse_url($this->initialUrl);
63 72
        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 72
        $this->urlParts = $parts;
67
68 72
        parse_str($this->urlParts['query'], $this->queryParts);
69
70 72
        $this->wasParsed = true;
71 72
    }
72
73
    /**
74
     * @param string $part
75
     * @param mixed $value
76
     */
77 6
    protected function setUrlPart($part, $value)
78
    {
79 6
        $this->urlParts[$part] = $value;
80 6
    }
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 3
    public function setHost(string $host)
96
    {
97 3
        $this->setUrlPart('host', $host);
98 3
        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 $scheme
111
     * @return UrlHelper
112
     */
113 2
    public function setScheme(string $scheme)
114
    {
115 2
        $this->setUrlPart('scheme', $scheme);
116 2
        return $this;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 3
    public function getScheme(): string
123
    {
124 3
        return $this->getUrlPart('scheme');
125
    }
126
127
    /**
128
     * @param string $path
129
     * @return UrlHelper
130
     */
131 1
    public function setPath($path)
132
    {
133 1
        $this->setUrlPart('path', $path);
134 1
        return $this;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getPath(): string
141
    {
142
        return $this->getUrlPart('path');
143
    }
144
145
    /**
146
     * @param string $parameterName
147
     * @throws \InvalidArgumentException
148
     * @return UrlHelper
149
     */
150 51
    public function removeQueryParameter(string $parameterName): UrlHelper
151
    {
152 51
        unset($this->queryParts[$parameterName]);
153 51
        return $this;
154
    }
155
156
    /**
157
     * @param string $parameterName
158
     * @param mixed $value
159
     * @throws \InvalidArgumentException
160
     * @return UrlHelper
161
     */
162 4
    public function addQueryParameter(string $parameterName, $value): UrlHelper
163
    {
164 4
        $this->queryParts[$parameterName] = $value;
165 4
        return $this;
166
    }
167
168
    /**
169
     * @return string
170
     */
171 69
    public function getUrl(): string
172
    {
173 69
        $this->urlParts['query'] = http_build_query($this->queryParts);
174 69
        return $this->unparseUrl();
175
    }
176
177
    /**
178
     * @return string
179
     */
180 69
    protected function unparseUrl(): string
181
    {
182 69
        $scheme   = isset($this->urlParts['scheme']) ? $this->urlParts['scheme'] . '://' : '';
183 69
        $host     = $this->urlParts['host'] ?? '';
184 69
        $port     = $this->urlParts['port'] ? ':' . $this->urlParts['port'] : '';
185 69
        $user     = $this->urlParts['user'] ?? '';
186 69
        $user     = $this->urlParts['pass'] ? $user . ':' : $user;
187 69
        $pass     = $this->urlParts['pass'] ?? '';
188 69
        $pass     = ($user || $pass) ? "$pass@" : '';
189 69
        $path     = $this->urlParts['path'] ?? '';
190 69
        $query    = isset($this->urlParts['query']) && !empty($this->urlParts['query']) ? '?' . $this->urlParts['query'] : '';
191 69
        $fragment = isset($this->urlParts['fragment']) ? '#' . $this->urlParts['fragment'] : '';
192 69
        return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
193
    }
194
}
195