Httpie   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 18.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 20
loc 106
ccs 0
cts 88
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A get() 0 7 1
A post() 0 7 1
A query() 0 6 1
A header() 0 6 1
A body() 10 10 1
A form() 10 10 1
A setopt() 0 6 1
A send() 0 18 2
A getJson() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Utility;
9
10
class Httpie
11
{
12
    private $method = 'GET';
13
    private $url = '';
14
    private $headers = [];
15
    private $body = '';
16
    private $curlopts = [];
17
18
    public function __construct()
19
    {
20
        if (!extension_loaded('curl')) {
21
            throw new \Exception(
22
                "Please, install curl extension.\n" .
23
                "https://goo.gl/yTAeZh"
24
            );
25
        }
26
    }
27
28
    public static function get(string $url): Httpie
29
    {
30
        $http = new self;
31
        $http->method = 'GET';
32
        $http->url = $url;
33
        return $http;
34
    }
35
36
    public static function post(string $url): Httpie
37
    {
38
        $http = new self;
39
        $http->method = 'POST';
40
        $http->url = $url;
41
        return $http;
42
    }
43
44
    public function query(array $params): Httpie
45
    {
46
        $http = clone $this;
47
        $http->url .= '?' . http_build_query($params);
48
        return $http;
49
    }
50
51
    public function header(string $header): Httpie
52
    {
53
        $http = clone $this;
54
        $http->headers[] = $header;
55
        return $http;
56
    }
57
58 View Code Duplication
    public function body(array $data): Httpie
59
    {
60
        $http = clone $this;
61
        $http->body = json_encode($data, JSON_PRETTY_PRINT);
62
        $http->headers = array_merge($http->headers, [
63
            'Content-Type: application/json',
64
            'Content-Length: ' . strlen($http->body)
65
        ]);
66
        return $http;
67
    }
68
69 View Code Duplication
    public function form(array $data): Httpie
70
    {
71
        $http = clone $this;
72
        $http->body = http_build_query($data);
73
        $http->headers = array_merge($this->headers, [
74
            'Content-type: application/x-www-form-urlencoded',
75
            'Content-Length: ' . strlen($http->body)
76
        ]);
77
        return $http;
78
    }
79
80
    public function setopt($key, $value)
81
    {
82
        $http = clone $this;
83
        $http->curlopts[$key] = $value;
84
        return $http;
85
    }
86
87
    public function send()
88
    {
89
        $ch = curl_init($this->url);
90
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
91
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
92
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
93
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
95
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
96
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
97
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
98
        foreach ($this->curlopts as $key => $value) {
99
            curl_setopt($ch, $key, $value);
100
        }
101
        $result = curl_exec($ch);
102
        curl_close($ch);
103
        return $result;
104
    }
105
106
    public function getJson()
107
    {
108
        $result = $this->send();
109
        $response = json_decode($result, true);
110
        if (json_last_error() !== JSON_ERROR_NONE) {
111
            throw new \Exception('JSON Error: ' . json_last_error_msg());
112
        }
113
        return $response;
114
    }
115
}
116