FactoryUri::createUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 11
rs 9.9666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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