Subversion-Projekte lars-tiefland.laravel_shop

Revision

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