Passed
Push — master ( c4128f...b4201c )
by Charis
01:36
created

FactoryUri::createUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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

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
2
namespace One;
3
4
/**
5
 * FactoryUri Class
6
 *
7
 * @method create
8
 * @method createArticle
9
 * @method validateArray
10
 * @method validateUrl
11
 * @method validateInteger
12
 * @method validateString
13
 * @method checkData
14
 *
15
 */
16
class FactoryUri
17
{
18
19
    /**
20
     * function Create Uri
21
     *
22
     * @param String $string
23
     * @return object Uri
24
     */
25
    public static function create($string = null)
26
    {
27
        if (!empty($string)) {
28
            return self::createFromString($string);
29
        }
30
        return self::createFromServer();
31
    }
32
33
    /**
34
     * function for Create Uri From Server
35
     *
36
     */
37
    public static function createFromString($uri)
38
    {
39
        $data = parse_url(self::validateUrl($uri));
40
41
        $scheme = self::validateString((string) self::checkData($data, 'scheme', ''));
42
        $user = self::validateString((string) self::checkData($data, 'user', ''));
43
        $pass = self::validateString((string) self::checkData($data, 'pass', ''));
44
        $host = self::validateString((string) self::checkData($data, 'host', ''));
45
        $port = self::validateInteger((int) self::checkData($data, 'port', null));
46
        $path = self::validateString((string) self::checkData($data, 'path', ''));
47
        $query = self::validateString((string) self::checkData($data, 'query', ''));
48
        $fragment = self::validateString((string) self::checkData($data, 'fragment', ''));
49
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
50
    }
51
52
    /**
53
     * functionality to check whether a variable is set or not.
54
     *
55
     * @return array
56
     */
57
    private static function checkData($data, $key, $default = '')
58
    {
59
        return isset($data[$key]) ? $data[$key] : $default;
60
    }
61
62
    /**
63
     * function for Create Uri From Server
64
     *
65
     */
66
    public static function createFromServer()
67
    {
68
        $scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://'));
69
        $host = self::validateString((string) self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
70
        $port = self::validateInteger((int) self::checkData($_SERVER, 'SERVER_PORT', null));
71
        $user = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
72
        $pass = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
73
        $path = (string) self::checkData($_SERVER, 'REQUEST_URI', '');
74
        $path = self::validateString(parse_url('http://www.foobar.com/' . $path, PHP_URL_PATH));
75
        $query = self::validateString((string) self::checkData($_SERVER, 'QUERY_STRING', ''));
76
        $fragment = '';
77
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
78
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
79
        }
80
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
81
    }
82
83
    /**
84
     * Create Uri Object
85
     *
86
     * @param String $string
87
     * @param string $scheme
88
     * @param string $host
89
     * @param int $port
90
     * @param string $user
91
     * @param string $password
92
     * @param string $path
93
     * @param string $query
94
     * @param string $fragment
95
     * @return Uri Object
96
     */
97
    private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
98
    {
99
        return new Uri(
100
            $scheme,
101
            $host,
102
            $port,
103
            $path,
104
            $query,
105
            $fragment,
106
            $user,
107
            $password
108
        );
109
    }
110
111
    /**
112
     * Make Sure Url in string with correct url format
113
     *
114
     * @param String $string
115
     * @return string
116
     */
117
    private static function validateUrl($url)
118
    {
119
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
120
            throw new \Exception("Invalid url : $url");
121
        }
122
        return $url;
123
    }
124
125
    /**
126
     * functionality validity for int variables
127
     *
128
     * @param int $var
129
     * @return int
130
     */
131
    private static function validateInteger($var)
132
    {
133
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
134
            throw new \Exception("The variable must be a integer :" . $var);
135
        }
136
        return $var;
137
    }
138
139
    /**
140
     * functionality validity for string variables
141
     *
142
     * @param String $var
143
     * @return String
144
     */
145
    private static function validateString($var)
146
    {
147
        if (gettype($var) === "string") {
148
            return $var;
149
        }
150
        throw new \Exception("The variable type must String :" . $var);
151
    }
152
}
153