Passed
Pull Request — master (#56)
by Yasin
04:06 queued 01:57
created

FactoryUri::validateInteger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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($uri);
40
        $scheme = self::validateString((string) self::checkData($data, 'scheme', ''));
41
        $user = self::validateString((string) self::checkData($data, 'user', ''));
42
        $pass = self::validateString((string) self::checkData($data, 'pass', ''));
43
        $host = self::validateString((string) self::checkData($data, 'host', ''));
44
        $port = self::checkData($data, 'port', null);
45
        $path = self::validateString((string) self::checkData($data, 'path', ''));
46
        $query = self::validateString((string) self::checkData($data, 'query', ''));
47
        $fragment = self::validateString((string) self::checkData($data, 'fragment', ''));
48
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
49
    }
50
51
    /**
52
     * functionality to check whether a variable is set or not.
53
     *
54
     * @return array
55
     */
56
    private static function checkData($data, $key, $default = '')
57
    {
58
        return isset($data[$key]) ? $data[$key] : $default;
59
    }
60
61
    /**
62
     * function for Create Uri From Server
63
     *
64
     */
65
    public static function createFromServer()
66
    {
67
        $scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://'));
68
        $host = self::validateString((string) self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
69
        $port = self::checkData($_SERVER, 'SERVER_PORT', null);
70
        $user = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
71
        $pass = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
72
        $path = (string) self::checkData($_SERVER, 'REQUEST_URI', '');
73
        $path = self::validateString(parse_url('http://www.foobar.com/' . $path, PHP_URL_PATH));
74
        $query = self::validateString((string) self::checkData($_SERVER, 'QUERY_STRING', ''));
75
        $fragment = '';
76
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
77
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
78
        }
79
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
80
    }
81
82
    /**
83
     * Create Uri Object
84
     *
85
     * @param String $string
86
     * @param string $scheme
87
     * @param string $host
88
     * @param string $user
89
     * @param string $password
90
     * @param string $path
91
     * @param string $query
92
     * @param string $fragment
93
     * @return Uri Object
94
     */
95
    private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
96
    {
97
        return new Uri(
98
            $scheme,
99
            $host,
100
            $port,
101
            $path,
102
            $query,
103
            $fragment,
104
            $user,
105
            $password
106
        );
107
    }
108
109
    /**
110
     * functionality validity for string variables
111
     *
112
     * @param String $var
113
     * @return String
114
     */
115
    private static function validateString($var)
116
    {
117
        if (gettype($var) === "string") {
118
            return $var;
119
        }
120
        throw new \Exception("The variable type must String :" . $var);
121
    }
122
}
123