Passed
Push — master ( 0f048f...a1e798 )
by Bence
07:37 queued 03:39
created

Requester::request()   D

Complexity

Conditions 16
Paths 149

Size

Total Lines 82
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 25.0667

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 82
ccs 45
cts 67
cp 0.6716
rs 4.6236
cc 16
eloc 64
nc 149
nop 1
crap 25.0667

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
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 20
    protected function request($data) {
18 20
        $data = array_merge(array(
19 20
            "url" => null,
20 12
            "get" => array(),
21 12
            "post" => array(),
22 12
            "body_type" => "application/x-www-form-urlencoded",
23 12
            "body_charset" => "UTF-8",
24 12
            "headers" => array(),
25 12
            "response_type" => "json",
26 12
            "useragent" => "WrapIt-HTTP/1.0",
27
            "method" => null
28 20
        ), $data);
29
30 20
        if ($data["url"] === null) {
31
            throw new WrapItParameterException("Missing data: url");
32
        }
33
34 20
        $url = $data["url"];
35 20
        if ($data["get"] !== null && count($data["get"]) > 0) {
36 5
            $url .= "?" . http_build_query($data["get"]);
37 3
        }
38
39 20
        $ch = curl_init();
40 20
        curl_setopt($ch, CURLOPT_URL, $url);
41 20
        curl_setopt($ch, CURLOPT_USERAGENT, $data["useragent"]);
42 20
        if ($data["post"] != null) {
43 10
            $data["headers"][] = "Content-type: " . $data["body_type"];
44 10
            $data["headers"][] = "Charset: " . $data["body_charset"];
45
46 10
            switch ($data["body_type"]) {
47 10
                case "application/x-www-form-urlencoded":
48 10
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data["post"]));
49 10
                    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 6
            }
66
67 10
            if ($data["method"] !== null) {
68
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $data["method"]);
69
            } else {
70 10
                curl_setopt($ch, CURLOPT_POST, true);
71
            }
72 6
        }
73 20
        if ($data["headers"] !== null && count($data["headers"]) > 0) {
74 20
            curl_setopt($ch, CURLOPT_HTTPHEADER, $data["headers"]);
75 12
        }
76 20
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
77 20
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
78 20
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
79 20
        $result = curl_exec($ch);
80 20
        $response_header = curl_getinfo($ch);
81 20
        curl_close($ch);
82
83 20
        if ($response_header["http_code"] == 0) {
84
            throw new WrapItHTTPException("HTTP Connection error");
85
        }
86
87 20
        switch ($data["response_type"]) {
88 20
            case "json":
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
89 20
                $json = json_decode($result, true);
90 20
                if ($json !== null) {
91 20
                    return $json;
92
                } else {
93
                    return $result;
94
                }
95
            default:
96
                return $result;
97
        }
98
    }
99
100 5
    public function getDomain() {
101 5
        return $this->domain;
102
    }
103
104
    abstract public function get($api, $data);
105
    abstract public function post($api, $data);
106
107
}
108