1717use GuzzleHttp \Psr7 \Uri ;
1818use function GuzzleHttp \Psr7 \stream_for ;
1919
20+ /**
21+ * Guzzle Client.
22+ *
23+ * @package PhpFirebase
24+ * @subpackage Clients
25+ * @since 0.1.0
26+ */
2027class GuzzleClient implements ClientInterface
2128{
29+ /**
30+ * Guzzle client
31+ *
32+ * @var \GuzzleHttp\Client
33+ */
2234 protected $ guzzle ;
2335
36+ /**
37+ * Base endpoint
38+ *
39+ * @var string
40+ */
2441 protected $ base ;
2542
43+ /**
44+ * Token
45+ *
46+ * @var string
47+ */
2648 protected $ token ;
2749
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+ */
2858 public function __construct (array $ options = [])
2959 {
3060 if (!isset ($ options ['base ' ])) {
@@ -41,6 +71,14 @@ public function __construct(array $options = [])
4171 $ this ->guzzle = new HttpClient ($ options );
4272 }
4373
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+ */
4482 public function get ($ endpoint , $ query = [])
4583 {
4684 $ request = new Request ('GET ' ,$ this ->buildUri ($ endpoint , $ query ), $ this ->buildHeaders ());
@@ -50,6 +88,15 @@ public function get($endpoint, $query = [])
5088 return $ this ->handle ($ response );
5189 }
5290
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+ */
53100 public function post ($ endpoint , $ data , $ query = [])
54101 {
55102 $ data = $ this ->prepareData ($ data );
@@ -61,6 +108,15 @@ public function post($endpoint, $data, $query = [])
61108 return $ this ->handle ($ response );
62109 }
63110
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+ */
64120 public function put ($ endpoint , $ data , $ query = [])
65121 {
66122 $ data = $ this ->prepareData ($ data );
@@ -72,6 +128,15 @@ public function put($endpoint, $data, $query = [])
72128 return $ this ->handle ($ response );
73129 }
74130
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+ */
75140 public function patch ($ endpoint , $ data , $ query = [])
76141 {
77142 $ data = $ this ->prepareData ($ data );
@@ -83,6 +148,14 @@ public function patch($endpoint, $data, $query = [])
83148 return $ this ->handle ($ response );
84149 }
85150
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+ */
86159 public function delete ($ endpoint , $ query = [])
87160 {
88161 $ request = new Request ('DELETE ' ,$ this ->buildUri ($ endpoint , $ query ), $ this ->buildHeaders ());
@@ -92,11 +165,27 @@ public function delete($endpoint, $query = [])
92165 return $ this ->handle ($ response );
93166 }
94167
168+ /**
169+ * Convert array|string to json
170+ *
171+ * @param array $data Data to be converted
172+ *
173+ * @return array
174+ */
95175 protected function prepareData ($ data = [])
96176 {
97177 return json_encode ($ data );
98178 }
99179
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+ */
100189 protected function buildUri ($ endpoint , $ options = [])
101190 {
102191 if ($ this ->token !== '' ) {
@@ -106,6 +195,13 @@ protected function buildUri($endpoint, $options = [])
106195 return $ this ->base . '/ ' . ltrim ($ endpoint , '/ ' ) . '.json? ' . http_build_query ($ options , '' , '& ' );
107196 }
108197
198+ /**
199+ * Build all headers
200+ *
201+ * @param array $extraHeaders Extra headers to be added
202+ *
203+ * @return array
204+ */
109205 protected function buildHeaders ($ extraHeaders = [])
110206 {
111207 $ headers = [
@@ -116,7 +212,14 @@ protected function buildHeaders($extraHeaders = [])
116212 return array_merge ($ headers , $ extraHeaders );
117213 }
118214
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 )
120223 {
121224 $ stream = stream_for ($ response ->getBody ());
122225
0 commit comments