Uri::createFromString()   C
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 6.4615
c 0
b 0
f 0
cc 9
eloc 11
nc 256
nop 1
1
<?php
2
3
namespace Resilient\Http\Factory;
4
5
use InvalidArgumentException;
6
use \Psr\Http\Message\UriInterface;
7
8
class Uri
9
{
10
    /**
11
     * Create uri Instance from header.
12
     *
13
     * @access public
14
     * @static
15
     * @return Resilient\Http\Uri
16
     */
17
    public static function createFromServer ($serv)
18
    {
19
        $scheme = isset($serv['HTTPS']) ? 'https://' : 'http://';
20
        $host = !empty($serv['HTTP_HOST']) ? $serv['HTTP_HOST'] : $serv['SERVER_NAME'];
21
        $port = empty($serv['SERVER_PORT']) ? $serv['SERVER_PORT'] : null;
22
23
        $path = (string) parse_url('http://www.example.com/' . $serv['REQUEST_URI'], PHP_URL_PATH);
24
25
        $query = empty($serv['QUERY_STRING']) ? parse_url('http://example.com' . $serv['REQUEST_URI'], PHP_URL_QUERY) : $serv['QUERY_STRING'];
26
27
        $fragment = '';
28
29
        $user = !empty($serv['PHP_AUTH_USER']) ? $serv['PHP_AUTH_USER'] : '';
30
        $password = !empty($serv['PHP_AUTH_PW']) ? $serv['PHP_AUTH_PW'] : '';
31
32
        if (empty($user) && empty($password) && !empty($serv['HTTP_AUTHORIZATION'])) {
33
            list($user, $password) = explode(':', base64_decode(substr($serv['HTTP_AUTHORIZATION'], 6)));
34
        }
35
36
        $uri = new \Resilient\Http\Uri($scheme, $host, $port, $path, $query, $fragment, $user, $password);
37
38
        return $uri;
39
    }
40
41
42
    /**
43
     * Create Uri Instance from string http://www.example.com/url/path.html
44
     *
45
     * @access public
46
     * @static
47
     * @param string $uri
48
     * @return Resilient\Http\Uri
49
     */
50
    public static function createFromString (string $uri)
51
    {
52
        $parts = parse_url($uri);
53
        $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
54
        $user = isset($parts['user']) ? $parts['user'] : '';
55
        $pass = isset($parts['pass']) ? $parts['pass'] : '';
56
        $host = isset($parts['host']) ? $parts['host'] : '';
57
        $port = isset($parts['port']) ? $parts['port'] : null;
58
        $path = isset($parts['path']) ? $parts['path'] : '';
59
        $query = isset($parts['query']) ? $parts['query'] : '';
60
        $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
61
62
        return new \Resilient\Http\Uri($scheme, $host, $port, $path, $query, $fragment, $user, $pass);
63
    }
64
}
65