Utils   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 0
cbo 1
dl 0
loc 38
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C parseUrl() 0 25 8
1
<?php
2
/**
3
 * For the full copyright and license information, please view the LICENSE
4
 * file that was distributed with this source code.
5
 *
6
 * @author Nikita Vershinin <[email protected]>
7
 * @license MIT
8
 */
9
namespace OpenStackStorage;
10
11
use OpenStackStorage\Exceptions\InvalidUrl;
12
13
/**
14
 * Common helper functions.
15
 */
16
class Utils
17
{
18
19
    /**
20
     * Return an array of 4 elements containing the scheme, hostname, port
21
     * and a boolean representing whether the connection should use SSL or not.
22
     *
23
     * @static
24
     * @param  string                                  $url
25
     * @return array
26
     * @throws \OpenStackStorage\Exceptions\InvalidUrl
27
     */
28
    public static function parseUrl($url)
29
    {
30
        $info = parse_url($url);
31
        if (!$info || empty($info['scheme'])) {
32
            throw new InvalidUrl('The string must be a valid URL');
33
        }
34
35
        if (empty($info['scheme']) || !in_array($info['scheme'], array('http', 'https'))) {
36
            throw new InvalidUrl('Scheme must be one of http or https');
37
        }
38
39
        if (empty($info['port'])) {
40
            // Порт не указан
41
            $port = $info['scheme'] == 'https' ? 443 : 80;
42
        } else {
43
            $port = intval($info['port']);
44
        }
45
46
        return array(
47
            'scheme' => $info['scheme'],
48
            'host'   => $info['host'],
49
            'port'   => $port,
50
            'path'   => !empty($info['path']) ? trim($info['path'], '/') : '',
51
        );
52
    }
53
}
54