Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\FlareClient\Http;
4
 
5
use Spatie\FlareClient\Http\Exceptions\BadResponseCode;
6
use Spatie\FlareClient\Http\Exceptions\InvalidData;
7
use Spatie\FlareClient\Http\Exceptions\MissingParameter;
8
use Spatie\FlareClient\Http\Exceptions\NotFound;
9
 
10
class Client
11
{
12
    protected ?string $apiToken;
13
 
14
    protected ?string $baseUrl;
15
 
16
    protected int $timeout;
17
 
18
    public function __construct(
19
        ?string $apiToken = null,
20
        string $baseUrl = 'https://reporting.flareapp.io/api',
21
        int $timeout = 10
22
    ) {
23
        $this->apiToken = $apiToken;
24
 
25
        if (! $baseUrl) {
26
            throw MissingParameter::create('baseUrl');
27
        }
28
 
29
        $this->baseUrl = $baseUrl;
30
 
31
        if (! $timeout) {
32
            throw MissingParameter::create('timeout');
33
        }
34
 
35
        $this->timeout = $timeout;
36
    }
37
 
38
    public function setApiToken(string $apiToken): self
39
    {
40
        $this->apiToken = $apiToken;
41
 
42
        return $this;
43
    }
44
 
45
    public function apiTokenSet(): bool
46
    {
47
        return ! empty($this->apiToken);
48
    }
49
 
50
    public function setBaseUrl(string $baseUrl): self
51
    {
52
        $this->baseUrl = $baseUrl;
53
 
54
        return $this;
55
    }
56
 
57
    /**
58
     * @param string $url
59
     * @param array  $arguments
60
     *
61
     * @return array|false
62
     */
63
    public function get(string $url, array $arguments = [])
64
    {
65
        return $this->makeRequest('get', $url, $arguments);
66
    }
67
 
68
    /**
69
     * @param string $url
70
     * @param array  $arguments
71
     *
72
     * @return array|false
73
     */
74
    public function post(string $url, array $arguments = [])
75
    {
76
        return $this->makeRequest('post', $url, $arguments);
77
    }
78
 
79
    /**
80
     * @param string $url
81
     * @param array  $arguments
82
     *
83
     * @return array|false
84
     */
85
    public function patch(string $url, array $arguments = [])
86
    {
87
        return $this->makeRequest('patch', $url, $arguments);
88
    }
89
 
90
    /**
91
     * @param string $url
92
     * @param array  $arguments
93
     *
94
     * @return array|false
95
     */
96
    public function put(string $url, array $arguments = [])
97
    {
98
        return $this->makeRequest('put', $url, $arguments);
99
    }
100
 
101
    /**
102
     * @param string $method
103
     * @param array  $arguments
104
     *
105
     * @return array|false
106
     */
107
    public function delete(string $method, array $arguments = [])
108
    {
109
        return $this->makeRequest('delete', $method, $arguments);
110
    }
111
 
112
    /**
113
     * @param string $httpVerb
114
     * @param string $url
115
     * @param array $arguments
116
     *
117
     * @return array
118
     */
119
    protected function makeRequest(string $httpVerb, string $url, array $arguments = [])
120
    {
121
        $queryString = http_build_query([
122
            'key' => $this->apiToken,
123
        ]);
124
 
125
        $fullUrl = "{$this->baseUrl}/{$url}?{$queryString}";
126
 
127
        $headers = [
128
            'x-api-token: '.$this->apiToken,
129
        ];
130
 
131
        $response = $this->makeCurlRequest($httpVerb, $fullUrl, $headers, $arguments);
132
 
133
        if ($response->getHttpResponseCode() === 422) {
134
            throw InvalidData::createForResponse($response);
135
        }
136
 
137
        if ($response->getHttpResponseCode() === 404) {
138
            throw NotFound::createForResponse($response);
139
        }
140
 
141
        if ($response->getHttpResponseCode() !== 200 && $response->getHttpResponseCode() !== 204) {
142
            throw BadResponseCode::createForResponse($response);
143
        }
144
 
145
        return $response->getBody();
146
    }
147
 
148
    public function makeCurlRequest(string $httpVerb, string $fullUrl, array $headers = [], array $arguments = []): Response
149
    {
150
        $curlHandle = $this->getCurlHandle($fullUrl, $headers);
151
 
152
        switch ($httpVerb) {
153
            case 'post':
154
                curl_setopt($curlHandle, CURLOPT_POST, true);
155
                $this->attachRequestPayload($curlHandle, $arguments);
156
 
157
                break;
158
 
159
            case 'get':
160
                curl_setopt($curlHandle, CURLOPT_URL, $fullUrl.'&'.http_build_query($arguments));
161
 
162
                break;
163
 
164
            case 'delete':
165
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'DELETE');
166
 
167
                break;
168
 
169
            case 'patch':
170
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PATCH');
171
                $this->attachRequestPayload($curlHandle, $arguments);
172
 
173
                break;
174
 
175
            case 'put':
176
                curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, 'PUT');
177
                $this->attachRequestPayload($curlHandle, $arguments);
178
 
179
                break;
180
        }
181
 
182
        $body = json_decode(curl_exec($curlHandle), true);
183
        $headers = curl_getinfo($curlHandle);
184
        $error = curl_error($curlHandle);
185
 
186
        return new Response($headers, $body, $error);
187
    }
188
 
189
    protected function attachRequestPayload(&$curlHandle, array $data)
190
    {
191
        $encoded = json_encode($data);
192
 
193
        $this->lastRequest['body'] = $encoded;
194
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $encoded);
195
    }
196
 
197
    /**
198
     * @param string $fullUrl
199
     * @param array $headers
200
     *
201
     * @return resource
202
     */
203
    protected function getCurlHandle(string $fullUrl, array $headers = [])
204
    {
205
        $curlHandle = curl_init();
206
 
207
        curl_setopt($curlHandle, CURLOPT_URL, $fullUrl);
208
 
209
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array_merge([
210
            'Accept: application/json',
211
            'Content-Type: application/json',
212
        ], $headers));
213
 
214
        curl_setopt($curlHandle, CURLOPT_USERAGENT, 'Laravel/Flare API 1.0');
215
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
216
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, $this->timeout);
217
        curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, true);
218
        curl_setopt($curlHandle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
219
        curl_setopt($curlHandle, CURLOPT_ENCODING, '');
220
        curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
221
        curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
222
        curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 1);
223
 
224
        return $curlHandle;
225
    }
226
}