Url::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\Common\Request;
4
5
use ZoiloMora\ElasticAPM\Helper\Encoding;
6
7
/**
8
 * A complete Url, with scheme, host and path.
9
 */
10
final class Url implements \JsonSerializable
11
{
12
    /**
13
     * The raw, unparsed URL of the HTTP request line, e.g https://example.com:443/search?q=elasticsearch.
14
     * This URL may be absolute or relative.
15
     * For more details, see https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2.
16
     *
17
     * @var string|null
18
     */
19
    private $raw;
20
21
    /**
22
     * The protocol of the request, e.g. 'https:'.
23
     *
24
     * @var string|null
25
     */
26
    private $protocol;
27
28
    /**
29
     * The full, possibly agent-assembled URL of the request, e.g https://example.com:443/search?q=elasticsearch#top.
30
     *
31
     * @var string|null
32
     */
33
    private $full;
34
35
    /**
36
     * The hostname of the request, e.g. 'example.com'.
37
     *
38
     * @var string|null
39
     */
40
    private $hostname;
41
42
    /**
43
     * The port of the request, e.g. '443'
44
     *
45
     * @var string|int|null
46
     */
47
    private $port;
48
49
    /**
50
     * The path of the request, e.g. '/search'
51
     *
52
     * @var string|null
53
     */
54
    private $pathname;
55
56
    /**
57
     * The search describes the query string of the request. It is expected to have values delimited by ampersands.
58
     *
59
     * @var string|null
60
     */
61
    private $search;
62
63
    /**
64
     * The hash of the request URL, e.g. 'top'
65
     *
66
     * @var string|null
67
     */
68
    private $hash;
69
70
    /**
71
     * @param string|null $raw
72
     * @param string|null $protocol
73
     * @param string|null $full
74
     * @param string|null $hostname
75
     * @param string|int|null $port
76
     * @param string|null $pathname
77
     * @param string|null $search
78
     * @param string|null $hash
79
     */
80 10
    public function __construct(
81
        $raw = null,
82
        $protocol = null,
83
        $full = null,
84
        $hostname = null,
85
        $port = null,
86
        $pathname = null,
87
        $search = null,
88
        $hash = null
89
    ) {
90 10
        $this->raw = $raw;
91 10
        $this->protocol = $protocol;
92 10
        $this->full = $full;
93 10
        $this->hostname = $hostname;
94 10
        $this->port = $port;
95 10
        $this->pathname = $pathname;
96 10
        $this->search = $search;
97 10
        $this->hash = $hash;
98 10
    }
99
100
    /**
101
     * @return string|null
102
     */
103 1
    public function raw()
104
    {
105 1
        return $this->raw;
106
    }
107
108
    /**
109
     * @return string|null
110
     */
111 2
    public function protocol()
112
    {
113 2
        return $this->protocol;
114
    }
115
116
    /**
117
     * @return string|null
118
     */
119 2
    public function full()
120
    {
121 2
        return $this->full;
122
    }
123
124
    /**
125
     * @return string|null
126
     */
127 1
    public function hostname()
128
    {
129 1
        return $this->hostname;
130
    }
131
132
    /**
133
     * @return int|string|null
134
     */
135 1
    public function port()
136
    {
137 1
        return $this->port;
138
    }
139
140
    /**
141
     * @return string|null
142
     */
143 1
    public function pathname()
144
    {
145 1
        return $this->pathname;
146
    }
147
148
    /**
149
     * @return string|null
150
     */
151 2
    public function search()
152
    {
153 2
        return $this->search;
154
    }
155
156
    /**
157
     * @return string|null
158
     */
159 1
    public function hash()
160
    {
161 1
        return $this->hash;
162
    }
163
164
    /**
165
     * @return Url
166
     */
167 7
    public static function discover()
168
    {
169 7
        return new self(
170 7
            null,
171 7
            self::getProtocol(),
172 7
            self::getFull(),
173 7
            self::getHostname(),
174 7
            self::getPort(),
175 7
            self::getPathname(),
176 7
            self::getSearch()
177 7
        );
178
    }
179
180
    /**
181
     * @return string
182
     */
183 7
    private static function getProtocol()
184
    {
185 7
        return true === array_key_exists('HTTPS', $_SERVER)
186 7
            ? 'https'
187 7
            : 'http';
188
    }
189
190
    /**
191
     * @return string|null
192
     */
193 7
    private static function getFull()
194
    {
195 7
        if (null === self::getServerParams('HTTP_HOST')) {
196 6
            return null;
197
        }
198
199 1
        return sprintf(
200 1
            '%s://%s%s',
201 1
            self::getProtocol(),
202 1
            $_SERVER['HTTP_HOST'],
203 1
            $_SERVER['REQUEST_URI']
204 1
        );
205
    }
206
207
    /**
208
     * @return string|null
209
     */
210 7
    private static function getHostname()
211
    {
212 7
        return self::getServerParams('SERVER_NAME');
213
    }
214
215
    /**
216
     * @return string|null
217
     */
218 7
    private static function getPort()
219
    {
220 7
        return self::getServerParams('SERVER_PORT');
221
    }
222
223
    /**
224
     * @return string|null
225
     */
226 7
    private static function getPathname()
227
    {
228 7
        return self::getServerParams('SCRIPT_NAME');
229
    }
230
231
    /**
232
     * @return string|null
233
     */
234 7
    private static function getSearch()
235
    {
236 7
        $query = self::getServerParams('QUERY_STRING');
237 7
        if (null === $query) {
238 6
            return null;
239
        }
240
241 1
        return sprintf(
242 1
            '?%s',
243
            $query
244 1
        );
245
    }
246
247
    /**
248
     * @param string $key
249
     * @return string|null
250
     */
251 7
    private static function getServerParams($key)
252
    {
253 7
        if (false === array_key_exists($key, $_SERVER)) {
254 7
            return null;
255
        }
256
257 7
        return $_SERVER[$key];
258
    }
259
260
    /**
261
     * @return array
262
     */
263 1
    public function jsonSerialize()
264
    {
265
        return [
266 1
            'raw' => Encoding::keywordField($this->raw),
267 1
            'protocol' => Encoding::keywordField($this->protocol),
268 1
            'full' => Encoding::keywordField($this->full),
269 1
            'hostname' => Encoding::keywordField($this->hostname),
270 1
            'port' => Encoding::keywordField($this->port),
271 1
            'pathname' => Encoding::keywordField($this->pathname),
272 1
            'search' => Encoding::keywordField($this->search),
273 1
            'hash' => Encoding::keywordField($this->hash),
274 1
        ];
275
    }
276
}
277