AbstractApiService   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 48
c 1
b 0
f 0
dl 0
loc 188
ccs 0
cts 46
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A put() 0 18 4
A get() 0 18 4
A headers() 0 5 1
A post() 0 18 3
A delete() 0 17 4
1
<?php
2
/**
3
 * Abstract API Service.
4
 *
5
 * @package App\Services\Api
6
 *
7
 * @author    Nick Menke <[email protected]>
8
 * @copyright 2018-2020 Nick Menke
9
 *
10
 * @link https://github.com/nlmenke/vertebrae
11
 */
12
13
declare(strict_types=1);
14
15
namespace App\Services\Api;
16
17
use Closure;
18
use GuzzleHttp\Client;
19
use GuzzleHttp\Exception\ClientException;
20
21
/**
22
 * The base API service class.
23
 *
24
 * This class contains any functionality that would otherwise be duplicated in
25
 * other API services. All other API services should extend this class.
26
 *
27
 * @since x.x.x introduced
28
 */
29
abstract class AbstractApiService
30
{
31
    /**
32
     * The base URI for the API call.
33
     *
34
     * @var string
35
     */
36
    protected $baseUri;
37
38
    /**
39
     * The client instance.
40
     *
41
     * @var Client
42
     */
43
    protected $client;
44
45
    /**
46
     * Headers being sent with the API call.
47
     *
48
     * @var array
49
     */
50
    protected $headerList = [
51
        'Accept' => 'application/json',
52
    ];
53
54
    /**
55
     * Create a new API service class.
56
     *
57
     * @return void
58
     */
59
    public function __construct()
60
    {
61
        $this->client = new Client([
62
            'base_uri' => $this->baseUri,
63
        ]);
64
    }
65
66
    /**
67
     * Submits a DELETE API request.
68
     *
69
     * This method is used to send a DELETE request to an external application.
70
     * A DELETE request will typically returns a 200 (OK) HTTP response code
71
     * along with a response body, a 204 (No Content) with no response body, or
72
     * a 404 (Not Found) if the record or route does not exist.
73
     *
74
     * @param string       $uri
75
     * @param int|null     $id
76
     * @param Closure|null $callback
77
     *
78
     * @return array
79
     */
80
    public function delete(string $uri, int $id = null, Closure $callback = null): array
81
    {
82
        try {
83
            $result = $this->client->delete($uri . ($id !== null ? '/' . $id : ''), [
84
                'headers' => $this->headerList,
85
            ]);
86
87
            $contents = $result->getBody()->getContents();
88
89
            if (is_callable($callback)) {
90
                $callback($result);
91
            }
92
        } catch (ClientException $e) {
93
            $contents = $e->getResponse()->getBody()->getContents();
94
        }
95
96
        return json_decode($contents, true);
97
    }
98
99
    /**
100
     * Submits a GET API request.
101
     *
102
     * This method is used to send a GET request to an external application. A
103
     * GET request may return a 200 (OK) HTTP response code, which may or may
104
     * not include a response body, depending on the application, or a 404 (Not
105
     * Found) if the collection, record, or route does not exist.
106
     *
107
     * @param string       $uri
108
     * @param int|null     $id
109
     * @param array        $query
110
     * @param Closure|null $callback
111
     *
112
     * @return array
113
     */
114
    public function get(string $uri, int $id = null, array $query = [], Closure $callback = null): array
115
    {
116
        try {
117
            $result = $this->client->get($uri . ($id !== null ? '/' . $id : ''), [
118
                'headers' => $this->headerList,
119
                'query' => $query,
120
            ]);
121
122
            $contents = $result->getBody()->getContents();
123
124
            if (is_callable($callback)) {
125
                $callback($result);
126
            }
127
        } catch (ClientException $e) {
128
            $contents = $e->getResponse()->getBody()->getContents();
129
        }
130
131
        return json_decode($contents, true);
132
    }
133
134
    /**
135
     * Submits a POST API request.
136
     *
137
     * This method is used to send a POST request to an external application.
138
     * POST requests typically return a 201 (Created) along with a location
139
     * header with a link to the newly-created resource, a 404 (Not Found) if
140
     * the route does not exist, or a 409 (Conflict) if the resource already
141
     * exists.
142
     *
143
     * @param string       $uri
144
     * @param array        $formParams
145
     * @param Closure|null $callback
146
     *
147
     * @return array
148
     */
149
    public function post(string $uri, array $formParams = [], Closure $callback = null): array
150
    {
151
        try {
152
            $result = $this->client->post($uri, [
153
                'headers' => $this->headerList,
154
                'form_params' => $formParams,
155
            ]);
156
157
            $contents = $result->getBody()->getContents();
158
159
            if (is_callable($callback)) {
160
                $callback($result);
161
            }
162
        } catch (ClientException $e) {
163
            $contents = $e->getResponse()->getBody()->getContents();
164
        }
165
166
        return json_decode($contents, true);
167
    }
168
169
    /**
170
     * Submits a PUT API request.
171
     *
172
     * This method is used to send a PUT request to an external application.
173
     * PUT requests can return a 200 (OK) along with a body or a 204 (No
174
     * Content) if no body is included in the response, either of which may
175
     * include a location header with a link to the resource. You may also
176
     * receive a 404 (Not Found) if the record or route does not exist.
177
     *
178
     * @param string       $uri
179
     * @param int|null     $id
180
     * @param array        $formParams
181
     * @param Closure|null $callback
182
     *
183
     * @return array
184
     */
185
    public function put(string $uri, int $id = null, array $formParams = [], Closure $callback = null): array
186
    {
187
        try {
188
            $result = $this->client->put($uri . ($id !== null ? '/' . $id : ''), [
189
                'headers' => $this->headerList,
190
                'form_params' => $formParams,
191
            ]);
192
193
            $contents = $result->getBody()->getContents();
194
195
            if (is_callable($callback)) {
196
                $callback($result);
197
            }
198
        } catch (ClientException $e) {
199
            $contents = $e->getResponse()->getBody()->getContents();
200
        }
201
202
        return json_decode($contents, true);
203
    }
204
205
    /**
206
     * Adds additional headers to the headerList array.
207
     *
208
     * @param array $headerList
209
     *
210
     * @return self
211
     */
212
    protected function headers(array $headerList): self
213
    {
214
        $this->headerList = array_merge($this->headerList, $headerList);
215
216
        return $this;
217
    }
218
}
219