Passed
Push — psr-implementation ( 3cd67c...1cb923 )
by Yasin
02:23
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);
0 ignored issues
show
Bug introduced by
$port of type array is incompatible with the type integer expected by parameter $port of One\FactoryUri::createUri(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        return self::createUri($scheme, $host, /** @scrutinizer ignore-type */ $port, $user, $pass, $path, $query, $fragment);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$port of type array is incompatible with the type integer expected by parameter $port of One\FactoryUri::createUri(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        return self::createUri($scheme, $host, /** @scrutinizer ignore-type */ $port, $user, $pass, $path, $query, $fragment);
Loading history...
80
    }
81
82
    /**
83
     * Create Uri Object
84
     *
85
     * @param String $string
86
     * @param string $scheme
87
     * @param string $host
88
     * @param int $port
89
     * @param string $user
90
     * @param string $password
91
     * @param string $path
92
     * @param string $query
93
     * @param string $fragment
94
     * @return Uri Object
95
     */
96
    private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
97
    {
98
        return new Uri(
99
            $scheme,
100
            $host,
101
            $port,
102
            $path,
103
            $query,
104
            $fragment,
105
            $user,
106
            $password
107
        );
108
    }
109
110
    /**
111
     * Make Sure Url in string with correct url format
112
     *
113
     * @param String $string
114
     * @return string
115
     */
116
    private static function validateUrl($url)
0 ignored issues
show
Unused Code introduced by
The method validateUrl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
117
    {
118
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
119
            throw new \Exception("Invalid url : $url");
120
        }
121
        return $url;
122
    }
123
124
    /**
125
     * functionality validity for string variables
126
     *
127
     * @param String $var
128
     * @return String
129
     */
130
    private static function validateString($var)
131
    {
132
        if (gettype($var) === "string") {
133
            return $var;
134
        }
135
        throw new \Exception("The variable type must String :" . $var);
136
    }
137
}
138