Url::hasAuth()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport
10
 */
11
12
namespace Bee4\Transport;
13
14
/**
15
 * Define a valid URL component
16
 * Allow to check if it's a valid Url and define all components
17
 * @package Bee4\Transport
18
 *
19
 * @method string|Url scheme(string $value = null) Getter and setter for scheme property
20
 * @method string|Url host(string $value = null) Getter and setter for host property
21
 * @method int|Url port(int $value = null) Getter and setter for port property
22
 * @method string|Url user(string $value = null) Getter and setter for user property
23
 * @method string|Url pass(string $value = null) Getter and setter for pass property
24
 * @method string|Url path(string $value = null) Getter and setter for path property
25
 * @method string|Url query(string $value = null) Getter and setter for query property
26
 * @method string|Url fragment(string $value = null) Getter and setter for fragment property
27
 */
28
class Url
29
{
30
    /**
31
     * scheme - http
32
     * @var string
33
     */
34
    protected $scheme;
35
36
    /**
37
     * host - example.com
38
     * @var string
39
     */
40
    protected $host;
41
42
    /**
43
     * port - 80, 443...
44
     * @var int
45
     */
46
    protected $port;
47
48
    /**
49
     * user - credential if authentication used
50
     * @var string
51
     */
52
    protected $user;
53
54
    /**
55
     * pass - credential if authentication used
56
     * @var string
57
     */
58
    protected $pass;
59
60
    /**
61
     * path - /my-page.html
62
     * @var string
63
     */
64
    protected $path;
65
66
    /**
67
     * query - ?xx=yy&zz=aa
68
     * @var string
69
     */
70
    protected $query;
71
72
    /**
73
     * fragment - #anchor
74
     * @var string
75
     */
76
    protected $fragment;
77
78
    /**
79
     * Standard ports for specific schemes
80
     * @var array
81
     */
82
    private static $defaultPorts = [
83
        'http' => 80,
84
        'https' => 443,
85
        'ftp' => 21,
86
        'ssh' => 22
87
    ];
88
89
    /**
90
     * @param string $url
91
     */
92 47
    public function __construct($url)
93
    {
94 47
        if (!is_string($url)) {
95 1
            throw new \InvalidArgumentException('url given must be a valid string!');
96
        }
97 46
        if (!Url::isValid($url)) {
98 4
            throw new \InvalidArgumentException(
99
                'url given is not a valid url (according to PHP FILTER_VALIDATE_URL). '.$url
100 4
            );
101
        }
102
103
        //Define default entries
104 43
        if (($parsed = parse_url($url)) !== false) {
105 43
            foreach ($parsed as $name => $value) {
106 43
                $this->$name($value);
107 43
            }
108 43
        }
109 43
    }
110
111
    /**
112
     * Check if the given string is an URL or not
113
     * @param string $url
114
     * @return boolean
115
     */
116 46
    public static function isValid($url)
117
    {
118 46
        return filter_var($url, FILTER_VALIDATE_URL)!==false;
119
    }
120
121
    /**
122
     * Check if the current URL use Basic Auth or not
123
     * @return boolean
124
     */
125 1
    public function hasAuth()
126
    {
127 1
        return isset($this->user)&&isset($this->pass);
128
    }
129
130
    /**
131
     * Rebuilt the URL with all known parts
132
     * @return string
133
     */
134 25
    public function toString()
135
    {
136 25
        $url = $this->scheme.':';
137 25
        if (trim($this->host) != '') {
138 22
            $url .= '//';
139 22
            if (trim($this->user) != '') {
140 3
                $url .= $this->user.((trim($this->pass) != '')?':'.$this->pass:'').'@';
141 3
            }
142 22
            $url .= $this->host;
143
144 22
            if (trim($this->port) != '') {
145 14
                $url .= ':'.$this->port;
146 14
            }
147 22
        }
148
149 25
        $url .= (trim($this->path) != '')?$this->path:'';
150 25
        $url .= (trim($this->query) != '')?'?'.$this->query:'';
151 25
        $url .= (trim($this->fragment) != '')?'#'.$this->fragment:'';
152
153 25
        if (!self::isValid($url)) {
154 2
            throw new \RuntimeException('Built URL is not a valid one: '.$url);
155
        }
156
157 23
        return $url;
158
    }
159
160
    /**
161
     * Encapsulate all setters / getters and method calls
162
     * @param string $name
163
     * @param array $arguments
164
     * @return mixed
165
     * @throws \BadMethodCallException
166
     */
167 43
    public function __call($name, array $arguments)
168
    {
169
        //Define getter and setter for each valid property
170 43
        if (property_exists($this, $name)) {
171 43
            if (count($arguments) == 0) {
172 15
                return $this->retrieve($name);
173 43
            } elseif (count($arguments) == 1) {
174 43
                $this->populate($name, $arguments[0]);
175 43
                return $this;
176
            } else {
177 1
                throw new \BadMethodCallException('Invalid parameters given for: '.$name);
178
            }
179
        } else {
180 1
            throw new \BadMethodCallException('Invalid method in Url implementation: '.$name);
181
        }
182
    }
183
184
    /**
185
     * Transform to string with dynamic cast
186
     * @return String
187
     */
188 14
    public function __toString()
189
    {
190
        try {
191 14
            return $this->toString();
192 1
        } catch (\Exception $ex) {
193 1
            return __CLASS__.'::INVALID';
194
        }
195
    }
196
197
    /**
198
     * Fill a property with the given value
199
     * @param string $name
200
     * @param mixed $value
201
     * @throws \InvalidArgumentException
202
     */
203 43
    protected function populate($name, $value)
204
    {
205
        switch ($name) {
206 43
            case 'port':
207 21
                if (!is_int($value)) {
208 1
                    throw new \InvalidArgumentException('Value given to set "'.$name.'" must be a valid int!!');
209
                }
210 20
                if (isset(self::$defaultPorts[$this->scheme]) && $value == self::$defaultPorts[$this->scheme]) {
211 1
                    break;
212
                }
213 20
                $this->$name = $value;
214 20
                break;
215 43
            case 'host':
216 42
                if (strpos($value, ':') !== false) {
217 1
                    $tmp = explode(':', $value);
218 1
                    $this->port((int)$tmp[1]);
219 1
                    $value = $tmp[0];
220 1
                }
221
                //Just extract port then go to the default case
222 43
            default:
223 43
                if (!is_string($value)) {
224 7
                    throw new \InvalidArgumentException('Value given to set "'.$name.'" must be a valid string!!');
225
                }
226 43
                $this->$name = $value;
227 43
                break;
228 43
        }
229 43
    }
230
231
    /**
232
     * Property name to retrieve from the current object
233
     * This function is defined only for the special port case
234
     * @param string $name
235
     * @return mixed
236
     */
237 15
    protected function retrieve($name)
238
    {
239
        switch ($name) {
240 15
            case 'port':
241 2
                if (is_null($this->$name) && isset(self::$defaultPorts[$this->scheme])) {
242 1
                    return self::$defaultPorts[$this->scheme];
243
                }
244
                //Just handle port then go to the default case
245 15
            default:
246 15
                return $this->$name;
247 15
        }
248
    }
249
}
250