Passed
Push — master ( 386b91...818dbd )
by Anton
06:54
created

Httpie::setopt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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
    public function body(array $data): Httpie
59
    {
60
        $http = clone $this;
61
        $http->body = json_encode($data, JSON_PRETTY_PRINT);
62
        $http->headers += [
63
            'Content-Type: application/json',
64
            'Content-Length: ' . strlen($http->body)
65
        ];
66
        return $http;
67
    }
68
69
    public function form(array $data): Httpie
70
    {
71
        $http = clone $this;
72
        $http->body = http_build_query($data);
73
        $http->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