Completed
Push — master ( 65c5e3...678926 )
by Bence
02:42
created

Requester   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 68.12%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 98
ccs 47
cts 69
cp 0.6812
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDomain() 0 3 1
get() 0 1 ?
post() 0 1 ?
D request() 0 85 16
1
<?php
2
3
namespace WrapIt\Http;
4
5
use WrapIt\Exceptions\WrapItParameterException;
6
//use WrapIt\Exceptions\WrapItHTTPException;
7
8
/**
9
 * Class Requester
10
 *
11
 * @package WrapIt
12
 */
13
abstract class Requester {
14
15
    protected $domain = null;
16
17 4
    protected function request($data) {
18 4
        $data = array_merge(array(
19 4
            "url" => null,
20 4
            "get" => array(),
21 4
            "post" => array(),
22 4
            "body_type" => "application/x-www-form-urlencoded",
23 4
            "body_charset" => "UTF-8",
24 4
            "headers" => array(),
25 4
            "response_type" => "json",
26 4
            "useragent" => "WrapIt-HTTP/1.0",
27
            "method" => null
28 4
        ), $data);
29
30 4
        if ($data["url"] == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $data['url'] of type null|array|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
31
            throw new WrapItParameterException("Missing data: url");
32
        }
33
34 4
        $url = $data["url"];
35 4
        if ($data["get"] != null && count($data["get"]) > 0) {
36 1
            $url .= "?" . http_build_query($data["get"]);
37 1
        }
38
39 4
        $ch = curl_init();
40 4
        curl_setopt($ch, CURLOPT_URL, $url);
41 4
        curl_setopt($ch, CURLOPT_USERAGENT, $data["useragent"]);
42 4
        if ($data["post"] != null) {
43 2
            $data["headers"][] = "Content-type: " . $data["body_type"];
44 2
            $data["headers"][] = "Charset: " . $data["body_charset"];
45
46 2
            switch ($data["body_type"]) {
47 2
                case "application/x-www-form-urlencoded":
48 2
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data["post"]));
49 2
                    break;
50
                case "application/json":
51
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data["post"]));
52
                    break;
53
                case "text/plain":
54
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]);
55
                    break;
56
                case "text/xml":
57
                    if (is_string($data["post"])) {
58
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $data["post"]);
59
                    } else {
60
                        throw new WrapItParameterException("XML data must be pre-processed!");
61
                    }
62
                    break;
63
                default:
64
                    throw new WrapItParameterException("Invalid body type");
65
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
66 2
            }
67
68 2
            if ($data["method"] != null) {
69
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]);
70
            } else {
71 2
                curl_setopt($ch, CURLOPT_POST, true);
72
            }
73 2
        }
74 4
        if ($data["headers"] != null && count($data["headers"]) > 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $data['headers'] of type null|array|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
75 4
            curl_setopt($ch, CURLOPT_HTTPHEADER, $data["headers"]);
76 4
        }
77 4
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78 4
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
79 4
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
80 4
        $result = curl_exec($ch);
81 4
        $response_header = curl_getinfo($ch);
82 4
        curl_close($ch);
83
84 4
        if ($response_header["http_code"] == 0) {
85
            throw new WrapItHTTPException("HTTP Connection error");
86
        }
87
88 4
        switch ($data["response_type"]) {
89 4
            case "json":
90 4
                $json = json_decode($result, true);
91 4
                if ($json !== null) {
92 4
                    return $json;
93
                } else {
94
                    return $result;
95
                }
96
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
97
            default:
98
                return $result;
99
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
100
        }
101
    }
102
103 1
    public function getDomain() {
104 1
        return $this->domain;
105
    }
106
107
    abstract public function get($api, $data);
108
    abstract public function post($api, $data);
109
110
}
111