Completed
Push — master ( 57f229...1fda10 )
by Dev
02:31
created

Helper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 68
ccs 25
cts 30
cp 0.8333
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
B httpParseHeaders() 0 29 8
A checkContentType() 0 3 2
A checkStatusCode() 0 3 2
A getSchemeFrom() 0 9 2
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class Helper
6
{
7
    /**
8
     * Return scheme from proxy string and remove Scheme From proxy.
9
     *
10
     * @param string $proxy
11
     *
12
     * @return string
13
     */
14 6
    public static function getSchemeFrom(&$proxy)
15
    {
16 6
        if (!preg_match('@^([a-z0-9]*)://@', $proxy, $match)) {
17 6
            return 'http://';
18
        }
19 3
        $scheme = $match[1].'://';
20 3
        $proxy = substr($proxy, strlen($scheme));
21
22 3
        return $scheme;
23
    }
24
25
    /**
26
     * Parse HTTP headers (php HTTP functions but generally, this packet isn't installed).
27
     *
28
     * @source http://www.php.net/manual/en/function.http-parse-headers.php#112917
29
     *
30
     * @param string $raw_headers Contain HTTP headers
31
     *
32
     * @return bool|array an array on success or FALSE on failure
33
     */
34 9
    public static function httpParseHeaders($raw_headers)
35
    {
36 9
        if (function_exists('http_parse_headers')) {
37
            http_parse_headers($raw_headers);
38
        }
39 9
        $headers = [];
40 9
        $key = '';
41 9
        foreach (explode("\n", $raw_headers) as $i => $h) {
42 9
            $h = explode(':', $h, 2);
43 9
            if (isset($h[1])) {
44 6
                if (!isset($headers[$h[0]])) {
45 6
                    $headers[$h[0]] = trim($h[1]);
46
                } elseif (is_array($headers[$h[0]])) {
47
                    $headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
48
                } else {
49
                    $headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
50
                }
51 6
                $key = $h[0];
52
            } else {
53 9
                if ("\t" == substr($h[0], 0, 1)) {
54
                    $headers[$key] .= "\r\n\t".trim($h[0]);
55 9
                } elseif (!$key) {
56 9
                    $headers[0] = trim($h[0]);
57
                }
58 9
                trim($h[0]);
59
            }
60
        }
61
62 9
        return $headers;
63
    }
64
65 15
    public static function checkContentType($line, $expected = 'text/html')
66
    {
67 15
        return 0 === stripos(trim($line), 'content-type') && false !== stripos($line, $expected);
68
    }
69
70 9
    public static function checkStatusCode($line, $expected = 200)
71
    {
72 9
        return 0 === stripos(trim($line), 'http') && false !== stripos($line, ' '.$expected.' ');
73
    }
74
}
75