HttpHeader::processRules()   D
last analyzed

Complexity

Conditions 25
Paths 15

Size

Total Lines 94
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 25
eloc 58
c 4
b 0
f 0
nc 15
nop 2
dl 0
loc 94
rs 4.1666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace GJClasses;
3
4
class HttpHeader
5
{
6
    public function process($domain)
7
    {
8
        $headers = $this->retrieve($domain);
9
        list($status, $data, $final_destination, $final_destination_apex) = $this->processRules($domain, $headers);
10
        return array($status, $data, $final_destination, $final_destination_apex);
11
    }
12
13
    public function retrieve($domain)
14
    {
15
        stream_context_set_default(
16
            array(
17
                'http' => array(
18
                    'timeout' => 8,
19
                    'method' => "GET",
20
                    'header' => "Accept-language: en\r\n" .
21
                        "Cookie: LANGUAGE=en;DEFLANG=en;"
22
                )
23
            )
24
        );
25
26
        return @get_headers('http://' . $domain);
27
    }
28
29
    public function processRules($domain, $headers)
30
    {
31
        if ($headers['0'] == 'HTTP/1.0 200 OK' || $headers['0'] == 'HTTP/1.1 200 OK') {
32
33
            $final_header_status = 'Live Site (200)';
34
            $header_data = 'n/a';
35
            $final_destination = 'http://' . $domain;
36
            $final_destination_apex = $domain;
37
38
        } elseif ($headers['0'] == 'HTTP/1.0 301 Moved Permanently' || $headers['0'] == 'HTTP/1.1 301 Moved Permanently') {
39
40
            list($header_data, $final_destination, $final_destination_apex, $count) = $this->createData($domain, $headers);
41
42
            if ($count === 1) {
43
                $header_status = 'Redirect, Permanent (301)';
44
            } elseif ($count > 1) {
45
                $header_status = 'Redirect, Permanent (301) [Multiple Redirects]';
46
            }
47
48
            $final_header_status = $this->checkSameRedirects($domain, $header_status, $header_data);
49
50
        } elseif ($headers['0'] == 'HTTP/1.0 302 Found' || $headers['0'] == 'HTTP/1.1 302 Found' || $headers['0'] == 'HTTP/1.1 302 Moved Temporarily') {
51
52
            list($header_data, $final_destination, $final_destination_apex, $count) = $this->createData($domain, $headers);
53
54
            if ($count === 1) {
55
                $header_status = 'Redirect, Temporary (302)';
56
            } elseif ($count > 1) {
57
                $header_status = 'Redirect, Temporary (302) [Multiple Redirects]';
58
            }
59
60
            $final_header_status = $this->checkSameRedirects($domain, $header_status, $header_data);
61
62
        } elseif ($headers['0'] == 'HTTP/1.0 400 Bad Request') {
63
64
            $final_header_status = 'Bad Request (400)';
65
            $header_data = 'n/a';
66
            $final_destination = 'n/a';
67
            $final_destination_apex = 'n/a';
68
69
        } elseif ($headers['0'] == 'HTTP/1.1 403 Forbidden') {
70
71
            $final_header_status = 'Forbidden (403)';
72
            $header_data = 'n/a';
73
            $final_destination = 'n/a';
74
            $final_destination_apex = 'n/a';
75
76
        } elseif ($headers['0'] == 'HTTP/1.0 404 Not Found' || $headers['0'] == 'HTTP/1.1 404 Not Found') {
77
78
            $final_header_status = 'Not Found (404)';
79
            $header_data = 'n/a';
80
            $final_destination = 'n/a';
81
            $final_destination_apex = 'n/a';
82
83
        } elseif ($headers['0'] == 'HTTP/1.1 463' || $headers['0'] == 'HTTP/1.1 463 ') {
84
85
            $final_header_status = 'Unspecified Error (463)';
86
            $header_data = 'n/a';
87
            $final_destination = 'n/a';
88
            $final_destination_apex = 'n/a';
89
90
        } elseif ($headers['0'] == 'HTTP/1.0 500 Internal Server Error' || $headers['0'] == 'HTTP/1.1 500 Internal Server Error') {
91
92
            $final_header_status = 'Internal Server Error (500)';
93
            $header_data = 'n/a';
94
            $final_destination = 'n/a';
95
            $final_destination_apex = 'n/a';
96
97
        } elseif ($headers['0'] == 'HTTP/1.0 503 Service Unavailable' || $headers['0'] == 'HTTP/1.1 503 Service Unavailable' || $headers['0'] == 'HTTP/1.1 503 Service Temporarily Unavailable') {
98
99
            $final_header_status = 'Service Temporarily Unavailable (503)';
100
            $header_data = 'n/a';
101
            $final_destination = 'n/a';
102
            $final_destination_apex = 'n/a';
103
104
        } else {
105
106
            if (!$headers['0'] || $headers['0'] == '') {
107
108
                $final_header_status = 'Error Retrieving Headers';
109
110
            } else {
111
112
                $final_header_status = $headers['0'];
113
114
            }
115
116
            $header_data = 'n/a';
117
            $final_destination = 'n/a';
118
            $final_destination_apex = 'n/a';
119
120
        }
121
122
        return array($final_header_status, $header_data, $final_destination, $final_destination_apex);
123
    }
124
125
    public function createData($domain, $headers)
126
    {
127
        $count = 0;
128
129
        $header_data = 'http://' . $domain . ' -> ';
130
131
        foreach ($headers as $value) {
132
            if (preg_match("/^Location:/", $value)) {
133
                $no_slash = rtrim($value, '/');
134
                $header_data .= substr($no_slash, 10) . " -> ";
135
                $count++;
136
            }
137
        }
138
        $header_data = substr($header_data, 0, -4);
139
        $final_destination = substr($no_slash, 10);
140
        $final_destination_apex = $this->getDomainApex($final_destination);
141
142
        return array($header_data, $final_destination, $final_destination_apex, $count);
143
144
    }
145
146
    public function getDomainApex($domain)
147
    {
148
        return ltrim(parse_url($domain, PHP_URL_HOST), 'www.');
149
    }
150
151
    public function checkSameRedirects($domain, $http_header_status, $http_header_data)
152
    {
153
        // check to see if the redirect chain is all for the same domain, and if so update it to 'Live Site (200)'
154
        $final_http_header_status = $http_header_status;
155
156
        if ($http_header_status === 'Redirect, Permanent (301)' || $http_header_status ===
157
            'Redirect, Permanent (301) [Multiple Redirects]' || $http_header_status === 'Redirect, Temporary (302)' ||
158
            $http_header_status === 'Redirect, Temporary (302) [Multiple Redirects]') {
159
160
            $header_data_array = explode(" -> ", $http_header_data);
161
162
            // standard port
163
            $url_variations = array();
164
            $url_variations[0] = 'http://' . $domain; // insecure, no-www
165
            $url_variations[1] = 'http://www.' . $domain; // insecure, www
166
            $url_variations[2] = 'https://' . $domain; // secure, no-www
167
            $url_variations[3] = 'https://www.' . $domain; // secure, www
168
            // port 443
169
            $url_variations[4] = 'http://' . $domain . ':443'; // insecure, no-www
170
            $url_variations[5] = 'http://www.' . $domain . ':443'; // insecure, www
171
            $url_variations[6] = 'https://' . $domain . ':443'; // secure, no-www
172
            $url_variations[7] = 'https://www.' . $domain . ':443'; // secure, www
173
174
            $all_same_domain = !array_diff($header_data_array, $url_variations);
175
176
            if ($all_same_domain === true) {
177
178
                $final_http_header_status = 'Live Site (200) [With Redirects]';
179
180
            }
181
182
        }
183
184
        return $final_http_header_status;
185
186
    }
187
188
}
189