FactoryUri   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 44
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkData() 0 3 1
A create() 0 6 2
A createFromServer() 0 24 4
A createUri() 0 11 1
A validateString() 0 6 2
A createFromString() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace One;
4
5
/**
6
 * FactoryUri Class
7
 *
8
 * @method create
9
 * @method createArticle
10
 * @method validateArray
11
 * @method validateUrl
12
 * @method validateInteger
13
 * @method validateString
14
 * @method checkData
15
 */
16
class FactoryUri
17
{
18
    /**
19
     * function Create Uri
20
     */
21
    public static function create(string $string = ''): \One\Uri
22
    {
23
        if (empty($string)) {
24
            $string = '/';
25
        }
26
        return self::createFromString($string);
27
    }
28
29
    /**
30
     * function for Create Uri From Server
31
     */
32
    public static function createFromString(string $uri): \One\Uri
33
    {
34
        $data = parse_url($uri);
35
        $scheme = self::validateString((string) self::checkData($data, 'scheme', ''));
36
        $user = self::validateString((string) self::checkData($data, 'user', ''));
37
        $pass = self::validateString((string) self::checkData($data, 'pass', ''));
38
        $host = self::validateString((string) self::checkData($data, 'host', ''));
39
        $port = self::checkData($data, 'port', null);
40
        $path = self::validateString((string) self::checkData($data, 'path', ''));
41
        $query = self::validateString((string) self::checkData($data, 'query', ''));
42
        $fragment = self::validateString((string) self::checkData($data, 'fragment', ''));
43
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
44
    }
45
46
    /**
47
     * function for Create Uri From Server
48
     */
49
    public static function createFromServer(): \One\Uri
50
    {
51
        $scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://'));
52
        $host = self::validateString((string) self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
53
        $port = self::checkData($_SERVER, 'SERVER_PORT', null);
54
        $user = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
55
        $pass = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
56
        $path = (string) self::checkData($_SERVER, 'REQUEST_URI', '');
57
        $path = self::validateString(parse_url('http://www.foobar.com/' . $path, PHP_URL_PATH));
58
        $query = self::validateString((string) self::checkData($_SERVER, 'QUERY_STRING', ''));
59
        $fragment = '';
60
        if (empty($user) && empty($pass) && ! empty($_SERVER['HTTP_AUTHORIZATION'])) {
61
            [$user] = explode(
62
                ':',
63
                base64_decode(
64
                    substr(
65
                        $_SERVER['HTTP_AUTHORIZATION'],
66
                        6
67
                    ),
68
                    true
69
                )
70
            );
71
        }
72
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
73
    }
74
75
    /**
76
     * functionality to check whether a variable is set or not.
77
     * @param  mixed $key
78
     * @param  string $default
79
     * @return mixed
80
     */
81
    private static function checkData(array $data, $key, $default = '')
82
    {
83
        return $data[$key] ?? $default;
84
    }
85
86
    /**
87
     * Create Uri Object
88
     */
89
    private static function createUri(string $scheme, string $host, ?int $port, string $user, string $password, string $path, string $query, string $fragment): \One\Uri
90
    {
91
        return new Uri(
92
            $scheme,
93
            $host,
94
            $port,
95
            $path,
96
            $query,
97
            $fragment,
98
            $user,
99
            $password
100
        );
101
    }
102
103
    /**
104
     * functionality validity for string variables
105
     * @param mixed $var
106
     * @throws \Exception
107
     */
108
    private static function validateString($var): string
109
    {
110
        if (gettype($var) === 'string') {
111
            return $var;
112
        }
113
        throw new \Exception('The variable type must String :' . $var);
114
    }
115
}
116