Client   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 201
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 3
A get() 0 4 1
A post() 0 4 1
A patch() 0 4 1
A put() 0 4 1
A delete() 0 4 1
A makeRequest() 0 29 5
B makeCurlRequest() 0 35 6
A attachRequestPayload() 0 7 1
A getCurlHandle() 0 21 1
1
<?php
2
3
namespace Facade\FlareClient\Http;
4
5
use Facade\FlareClient\Http\Exceptions\BadResponseCode;
6
use Facade\FlareClient\Http\Exceptions\InvalidData;
7
use Facade\FlareClient\Http\Exceptions\MissingParameter;
8
use Facade\FlareClient\Http\Exceptions\NotFound;
9
10
class Client
11
{
12
    /** @var null|string */
13
    private $apiToken;
14
15
    /** @var null|string */
16
    private $apiSecret;
17
18
    /** @var string */
19
    private $baseUrl;
20
21
    /** @var int */
22
    private $timeout;
23
24
    public function __construct(
25
        ?string $apiToken,
26
        ?string $apiSecret,
27
        string $baseUrl = 'https://flareapp.io/api',
28
        int $timeout = 10
29
    ) {
30
        $this->apiToken = $apiToken;
31
32
        $this->apiSecret = $apiSecret;
33
34
        if (! $baseUrl) {
35
            throw MissingParameter::create('baseUrl');
36
        }
37
38
        $this->baseUrl = $baseUrl;
39
40
        if (! $timeout) {
41
            throw MissingParameter::create('timeout');
42
        }
43
44
        $this->timeout = $timeout;
45
    }
46
47
    /**
48
     * @param string $url
49
     * @param array  $arguments
50
     *
51
     * @return array|false
52
     */
53
    public function get(string $url, array $arguments = [])
54
    {
55
        return $this->makeRequest('get', $url, $arguments);
56
    }
57
58
    /**
59
     * @param string $url
60
     * @param array  $arguments
61
     *
62
     * @return array|false
63
     */
64
    public function post(string $url, array $arguments = [])
65
    {
66
        return $this->makeRequest('post', $url, $arguments);
67
    }
68
69
    /**
70
     * @param string $url
71
     * @param array  $arguments
72
     *
73
     * @return array|false
74
     */
75
    public function patch(string $url, array $arguments = [])
76
    {
77
        return $this->makeRequest('patch', $url, $arguments);
78
    }
79
80
    /**
81
     * @param string $url
82
     * @param array  $arguments
83
     *
84
     * @return array|false
85
     */
86
    public function put(string $url, array $arguments = [])
87
    {
88
        return $this->makeRequest('put', $url, $arguments);
89
    }
90
91
    /**
92
     * @param string $method
93
     * @param array  $arguments
94
     *
95
     * @return array|false
96
     */
97
    public function delete(string $method, array $arguments = [])
98
    {
99
        return $this->makeRequest('delete', $method, $arguments);
100
    }
101
102
    /**
103
     * @param string $httpVerb
104
     * @param string $url
105
     * @param array $arguments
106
     *
107
     * @return array
108
     */
109
    private function makeRequest(string $httpVerb, string $url, array $arguments = [])
110
    {
111
        $queryString = http_build_query([
112
            'key' => $this->apiToken,
113
            'secret' => $this->apiSecret,
114
        ]);
115
116
        $fullUrl = "{$this->baseUrl}/{$url}?{$queryString}";
117
118
        $headers = [
119
            'x-api-token: '.$this->apiToken,
120
        ];
121
122
        $response = $this->makeCurlRequest($httpVerb, $fullUrl, $headers, $arguments);
123
124
        if ($response->getHttpResponseCode() === 422) {
125
            throw InvalidData::createForResponse($response);
126
        }
127
128
        if ($response->getHttpResponseCode() === 404) {
129
            throw NotFound::createForResponse($response);
130
        }
131
132
        if ($response->getHttpResponseCode() !== 200 && $response->getHttpResponseCode() !== 204) {
133
            throw BadResponseCode::createForResponse($response);
134
        }
135
136
        return $response->getBody();
137
    }
138
139
    public function makeCurlRequest(string $httpVerb, string $fullUrl, array $headers = [], array $arguments = []): Response
140
    {
141
        $curlHandle = $this->getCurlHandle($fullUrl, $headers);
142
143
        switch ($httpVerb) {
144
            case 'post':
145
                curl_setopt($curlHandle, CURLOPT_POST, true);
146
                $this->attachRequestPayload($curlHandle, $arguments);
147
                break;
148
149
            case 'get':
150
                curl_setopt($curlHandle, CURLOPT_URL, $fullUrl.'?'.http_build_query($arguments));
151
                break;
152
153
            case 'delete':
154
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
155
                break;
156
157
            case 'patch':
158
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PATCH');
159
                $this->attachRequestPayload($curlHandle, $arguments);
160
                break;
161
162
            case 'put':
163
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT');
164
                $this->attachRequestPayload($curlHandle, $arguments);
165
                break;
166
        }
167
168
        $body = json_decode(curl_exec($curlHandle), true);
169
        $headers = curl_getinfo($curlHandle);
170
        $error = curl_error($curlHandle);
171
172
        return new Response($headers, $body, $error);
173
    }
174
175
    private function attachRequestPayload(&$curlHandle, array $data)
176
    {
177
        $encoded = json_encode($data);
178
179
        $this->lastRequest['body'] = $encoded;
0 ignored issues
show
Bug introduced by
The property lastRequest does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
180
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $encoded);
181
    }
182
183
    /**
184
     * @param string $fullUrl
185
     * @param array $headers
186
     *
187
     * @return resource
188
     */
189
    private function getCurlHandle(string $fullUrl, array $headers = [])
190
    {
191
        $curlHandle = curl_init();
192
193
        curl_setopt($curlHandle, CURLOPT_URL, $fullUrl);
194
195
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
196
            'Accept: application/json',
197
            'Content-Type: application/json',
198
        ], $headers));
199
200
        curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Laravel/Flare API 1.0');
201
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
202
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout);
203
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
204
        curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
205
        curl_setopt($curlHandle, CURLOPT_ENCODING, '');
206
        curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
207
208
        return $curlHandle;
209
    }
210
}
211