17
17
use GuzzleHttp \Psr7 \Uri ;
18
18
use function GuzzleHttp \Psr7 \stream_for ;
19
19
20
+ /**
21
+ * Guzzle Client.
22
+ *
23
+ * @package PhpFirebase
24
+ * @subpackage Clients
25
+ * @since 0.1.0
26
+ */
20
27
class GuzzleClient implements ClientInterface
21
28
{
29
+ /**
30
+ * Guzzle client
31
+ *
32
+ * @var \GuzzleHttp\Client
33
+ */
22
34
protected $ guzzle ;
23
35
36
+ /**
37
+ * Base endpoint
38
+ *
39
+ * @var string
40
+ */
24
41
protected $ base ;
25
42
43
+ /**
44
+ * Token
45
+ *
46
+ * @var string
47
+ */
26
48
protected $ token ;
27
49
50
+ /**
51
+ * Set the base path for Firebase endpont
52
+ * the token to authenticate and the guzzle client
53
+ *
54
+ * @param string $base The base endpoint
55
+ * @param string $token The token
56
+ * @param \PhpFirebase\Interfaces\ClientInterface|null $client Client to make the request
57
+ */
28
58
public function __construct (array $ options = [])
29
59
{
30
60
if (!isset ($ options ['base ' ])) {
@@ -41,6 +71,14 @@ public function __construct(array $options = [])
41
71
$ this ->guzzle = new HttpClient ($ options );
42
72
}
43
73
74
+ /**
75
+ * Create a new GET reuest
76
+ *
77
+ * @param string $endpoint The sub endpoint
78
+ * @param array $query Query parameters
79
+ *
80
+ * @return array
81
+ */
44
82
public function get ($ endpoint , $ query = [])
45
83
{
46
84
$ request = new Request ('GET ' ,$ this ->buildUri ($ endpoint , $ query ), $ this ->buildHeaders ());
@@ -50,6 +88,15 @@ public function get($endpoint, $query = [])
50
88
return $ this ->handle ($ response );
51
89
}
52
90
91
+ /**
92
+ * Create a new POST reuest
93
+ *
94
+ * @param string $endpoint The sub endpoint
95
+ * @param string|array $data The data to be submited
96
+ * @param array $query Query parameters
97
+ *
98
+ * @return array
99
+ */
53
100
public function post ($ endpoint , $ data , $ query = [])
54
101
{
55
102
$ data = $ this ->prepareData ($ data );
@@ -61,6 +108,15 @@ public function post($endpoint, $data, $query = [])
61
108
return $ this ->handle ($ response );
62
109
}
63
110
111
+ /**
112
+ * Create a new PUT reuest
113
+ *
114
+ * @param string $endpoint The sub endpoint
115
+ * @param string|array $data The data to be submited
116
+ * @param array $query Query parameters
117
+ *
118
+ * @return array
119
+ */
64
120
public function put ($ endpoint , $ data , $ query = [])
65
121
{
66
122
$ data = $ this ->prepareData ($ data );
@@ -72,6 +128,15 @@ public function put($endpoint, $data, $query = [])
72
128
return $ this ->handle ($ response );
73
129
}
74
130
131
+ /**
132
+ * Create a new PATCH reuest
133
+ *
134
+ * @param string $endpoint The sub endpoint
135
+ * @param string|array $data The data to be submited
136
+ * @param array $query Query parameters
137
+ *
138
+ * @return array
139
+ */
75
140
public function patch ($ endpoint , $ data , $ query = [])
76
141
{
77
142
$ data = $ this ->prepareData ($ data );
@@ -83,6 +148,14 @@ public function patch($endpoint, $data, $query = [])
83
148
return $ this ->handle ($ response );
84
149
}
85
150
151
+ /**
152
+ * Create a new DELETE reuest
153
+ *
154
+ * @param string $endpoint The sub endpoint
155
+ * @param array $query Query parameters
156
+ *
157
+ * @return array
158
+ */
86
159
public function delete ($ endpoint , $ query = [])
87
160
{
88
161
$ request = new Request ('DELETE ' ,$ this ->buildUri ($ endpoint , $ query ), $ this ->buildHeaders ());
@@ -92,11 +165,27 @@ public function delete($endpoint, $query = [])
92
165
return $ this ->handle ($ response );
93
166
}
94
167
168
+ /**
169
+ * Convert array|string to json
170
+ *
171
+ * @param array $data Data to be converted
172
+ *
173
+ * @return array
174
+ */
95
175
protected function prepareData ($ data = [])
96
176
{
97
177
return json_encode ($ data );
98
178
}
99
179
180
+ /**
181
+ * Create a standard uri based on the end point
182
+ * and add the auth token
183
+ *
184
+ * @param string $endpoint The sub endpoint
185
+ * @param array $options Extra options to be added
186
+ *
187
+ * @return string
188
+ */
100
189
protected function buildUri ($ endpoint , $ options = [])
101
190
{
102
191
if ($ this ->token !== '' ) {
@@ -106,6 +195,13 @@ protected function buildUri($endpoint, $options = [])
106
195
return $ this ->base . '/ ' . ltrim ($ endpoint , '/ ' ) . '.json? ' . http_build_query ($ options , '' , '& ' );
107
196
}
108
197
198
+ /**
199
+ * Build all headers
200
+ *
201
+ * @param array $extraHeaders Extra headers to be added
202
+ *
203
+ * @return array
204
+ */
109
205
protected function buildHeaders ($ extraHeaders = [])
110
206
{
111
207
$ headers = [
@@ -116,7 +212,14 @@ protected function buildHeaders($extraHeaders = [])
116
212
return array_merge ($ headers , $ extraHeaders );
117
213
}
118
214
119
- private function handle (Response $ response , $ default = null )
215
+ /**
216
+ * Handle the response
217
+ *
218
+ * @param \GuzzleHttp\Psr7\Response $response The response
219
+ *
220
+ * @return array
221
+ */
222
+ private function handle (Response $ response )
120
223
{
121
224
$ stream = stream_for ($ response ->getBody ());
122
225
0 commit comments