Passed
Push — master ( 683c38...4d0316 )
by Timo
22:01
created

UrlHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1.037
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
 * Class UrlHelper
18
 *
19
 * @author Timo Hund <[email protected]>
20
 * @package ApacheSolrForTypo3\Solr\System\Url
21
 */
22
class UrlHelper {
23
24
    /**
25
     * @var string
26
     */
27
    protected $initialUrl;
28
29
    /**
30
     * @var array
31
     */
32
    protected $parts = [];
33
34
    /**
35
     * @var bool
36
     */
37
    protected $wasParsed = false;
38
39
    /**
40
     * UrlHelper constructor.
41
     * @param string $url
42
     */
43 4
    public function __construct($url)
44
    {
45 4
        $this->initialUrl = $url;
46 4
    }
47
48
    /**
49
     * @return void
50
     */
51 4
    protected function parseInitialUrl()
52
    {
53 4
        if ($this->wasParsed) {
54 3
            return;
55
        }
56 4
        $parts = parse_url($this->initialUrl);
57 4
        if (!is_array($parts)) {
0 ignored issues
show
introduced by
The condition is_array($parts) is always true.
Loading history...
58
            throw new \InvalidArgumentException("Non parseable url passed to UrlHelper", 1498751529);
59
        }
60 4
        $this->parts = $parts;
61 4
        $this->wasParsed = true;
62 4
    }
63
64
    /**
65
     * @param string $parameterName
66
     * @throws \InvalidArgumentException
67
     * @return UrlHelper
68
     */
69 1
    public function removeQueryParameter(string $parameterName): UrlHelper
70
    {
71 1
        $this->parseInitialUrl();
72 1
        $queryParts = [];
73 1
        parse_str($this->parts['query'], $queryParts);
74 1
        unset($queryParts[$parameterName]);
75 1
        $this->parts['query'] = http_build_query($queryParts);
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * @param string $parameterName
82
     * @param mixed $value
83
     * @throws \InvalidArgumentException
84
     * @return UrlHelper
85
     */
86 4
    public function addQueryParameter(string $parameterName, $value): UrlHelper
87
    {
88 4
        $this->parseInitialUrl();
89 4
        $queryParts = [];
90 4
        parse_str($this->parts['query'], $queryParts);
91 4
        $queryParts[$parameterName] = $value;
92 4
        $this->parts['query'] = http_build_query($queryParts);
93
94 4
        return $this;
95
    }
96
97
    /**
98
     * @return string
99
     */
100 4
    public function getUrl(): string
101
    {
102 4
        return $this->unparse_url($this->parts);
103
    }
104
105
    /**
106
     * @param array $parsed_url
107
     * @return string
108
     */
109 4
    protected function unparse_url(array $parsed_url): string
110
    {
111 4
        $scheme   = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
112 4
        $host     = isset($parsed_url['host']) ? $parsed_url['host'] : '';
113 4
        $port     = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
114 4
        $user     = isset($parsed_url['user']) ? $parsed_url['user'] : '';
115 4
        $pass     = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
116 4
        $pass     = ($user || $pass) ? "$pass@" : '';
117 4
        $path     = isset($parsed_url['path']) ? $parsed_url['path'] : '';
118 4
        $query    = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
119 4
        $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
120 4
        return $scheme . $user . $pass . $host . $port . $path . $query . $fragment;
121
    }
122
123
}