Skip to content

Commit 3260f15

Browse files
authored
Merge pull request #3 from adrorocker/master
Master into develop
2 parents 5277ed3 + 9236a49 commit 3260f15

File tree

10 files changed

+211
-140
lines changed

10 files changed

+211
-140
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Exclude unused files
55
# see: https://redd.it/2jzp6k
66
/example export-ignore
7+
/docs export-ignore
78
/tests export-ignore
89
/.codeclimate.yml export-ignore
910
/.coveralls.yml export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ script:
2626
- phpunit -c phpunit.xml.dist
2727

2828
after_script:
29-
- travis_retry php vendor/bin/coveralls -v
29+
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then travis_retry php vendor/bin/coveralls -v; fi
30+
- if [[ "$TRAVIS_PHP_VERSION" == '7.0' ]]; then travis_retry php vendor/bin/coveralls -v; fi

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ A PHP SDK for Firebase REST API.
1313
```
1414
composer require adrorocker/php-firebase
1515
```
16-
-----------------------------------
1716

1817
## Usage
1918

@@ -33,21 +32,22 @@ $firebase = new Firebase($base,$token);
3332
// Unique ID
3433
$id = (new \DateTime())->getTimestamp();
3534

36-
$data = ['key' => 'value']; // Or even just a string
35+
// Set the data (body of the request).
36+
$data = ['key' => 'value']; // The data could be even just a string
3737

38-
// Make a PUT request and retive the response
39-
$put = $firebase->put('/logs/'.$id, $data)->getResponse();
38+
// Make a PUT request, the response is return
39+
$put = $firebase->put('/logs/'.$id, $data);
4040

41-
// Make a GET request and retive the response, you will see all the logs
42-
$get = $firebase->get('/logs')->getResponse();
41+
// Make a GET request, the response is return,
42+
// you will have all the logs in the $get variable
43+
$get = $firebase->get('/logs');
4344
```
44-
-----------------------------------
4545

4646
## Authors:
4747

4848
[Alejandro Morelos](https://github.com/adrorocker).
4949

5050
[Master]: https://travis-ci.org/adrorocker/php-firebase/
51-
[Master image]: https://travis-ci.org/adrorocker/php-firebase.svg?branch=master&style=flat-square
52-
[Master covarage]: https://coveralls.io/github/adrorocker/php-firebase?branch=master
53-
[Master covarage image]: https://coveralls.io/repos/github/adrorocker/php-firebase/badge.svg?branch=master&style=flat-square
51+
[Master image]: https://travis-ci.org/adrorocker/php-firebase.svg?branch=master
52+
[Master covarage]: https://coveralls.io/github/adrorocker/php-firebase
53+
[Master covarage image]: https://coveralls.io/repos/github/adrorocker/php-firebase/badge.svg?branch=master

docs/.gitkeep

Whitespace-only changes.

src/Clients/GuzzleClient.php

Lines changed: 23 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,31 @@ class GuzzleClient implements ClientInterface
3434
protected $guzzle;
3535

3636
/**
37-
* Base endpoint
37+
* Set the the guzzle client
3838
*
39-
* @var string
39+
* @param array $options The options to set the defaul
40+
* @param Object|null $client Client to make the requests
4041
*/
41-
protected $base;
42-
43-
/**
44-
* Token
45-
*
46-
* @var string
47-
*/
48-
protected $token;
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-
*/
58-
public function __construct(array $options = [])
42+
public function __construct(array $options = [], $client = null)
5943
{
60-
if (!isset($options['base'])) {
61-
throw new InvalidArgumentException("Missign base path");
44+
if (!$client) {
45+
$client = new HttpClient($options);
6246
}
63-
64-
if (!isset($options['token'])) {
65-
throw new InvalidArgumentException("Missign token");
66-
}
67-
68-
$this->base = $options['base'];
69-
$this->token = $options['token'];
7047

71-
$this->guzzle = new HttpClient($options);
48+
$this->guzzle = $client;
7249
}
7350

7451
/**
7552
* Create a new GET reuest
7653
*
7754
* @param string $endpoint The sub endpoint
78-
* @param array $query Query parameters
55+
* @param array $headers Request headers
7956
*
8057
* @return array
8158
*/
82-
public function get($endpoint, $query = [])
59+
public function get($endpoint, $headers = [])
8360
{
84-
$request = new Request('GET',$this->buildUri($endpoint, $query), $this->buildHeaders());
61+
$request = new Request('GET',$endpoint, $headers);
8562

8663
$response = $this->guzzle->send($request);
8764

@@ -93,15 +70,13 @@ public function get($endpoint, $query = [])
9370
*
9471
* @param string $endpoint The sub endpoint
9572
* @param string|array $data The data to be submited
96-
* @param array $query Query parameters
73+
* @param array $headers Request headers
9774
*
9875
* @return array
9976
*/
100-
public function post($endpoint, $data, $query = [])
77+
public function post($endpoint, $data, $headers = [])
10178
{
102-
$data = $this->prepareData($data);
103-
104-
$request = new Request('POST',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
79+
$request = new Request('POST',$endpoint, $headers, $data);
10580

10681
$response = $this->guzzle->send($request);
10782

@@ -113,15 +88,13 @@ public function post($endpoint, $data, $query = [])
11388
*
11489
* @param string $endpoint The sub endpoint
11590
* @param string|array $data The data to be submited
116-
* @param array $query Query parameters
91+
* @param array $headers Request headers
11792
*
11893
* @return array
11994
*/
120-
public function put($endpoint, $data, $query = [])
95+
public function put($endpoint, $data, $headers = [])
12196
{
122-
$data = $this->prepareData($data);
123-
124-
$request = new Request('PUT',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
97+
$request = new Request('PUT',$endpoint, $headers, $data);
12598

12699
$response = $this->guzzle->send($request);
127100

@@ -133,15 +106,13 @@ public function put($endpoint, $data, $query = [])
133106
*
134107
* @param string $endpoint The sub endpoint
135108
* @param string|array $data The data to be submited
136-
* @param array $query Query parameters
109+
* @param array $headers Request headers
137110
*
138111
* @return array
139112
*/
140-
public function patch($endpoint, $data, $query = [])
113+
public function patch($endpoint, $data, $headers = [])
141114
{
142-
$data = $this->prepareData($data);
143-
144-
$request = new Request('PATCH',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
115+
$request = new Request('PATCH',$endpoint, $headers, $data);
145116

146117
$response = $this->guzzle->send($request);
147118

@@ -152,65 +123,20 @@ public function patch($endpoint, $data, $query = [])
152123
* Create a new DELETE reuest
153124
*
154125
* @param string $endpoint The sub endpoint
155-
* @param array $query Query parameters
126+
* @param array $headers Request headers
156127
*
157128
* @return array
158129
*/
159-
public function delete($endpoint, $query = [])
130+
public function delete($endpoint, $headers = [])
160131
{
161-
$request = new Request('DELETE',$this->buildUri($endpoint, $query), $this->buildHeaders());
132+
$request = new Request('DELETE',$endpoint, $headers);
162133

163134
$response = $this->guzzle->send($request);
164135

165136
return $this->handle($response);
166137
}
167138

168-
/**
169-
* Convert array|string to json
170-
*
171-
* @param array $data Data to be converted
172-
*
173-
* @return array
174-
*/
175-
protected function prepareData($data = [])
176-
{
177-
return json_encode($data);
178-
}
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-
*/
189-
protected function buildUri($endpoint, $options = [])
190-
{
191-
if ($this->token !== '') {
192-
$options['auth'] = $this->token;
193-
}
194-
195-
return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
196-
}
197-
198-
/**
199-
* Build all headers
200-
*
201-
* @param array $extraHeaders Extra headers to be added
202-
*
203-
* @return array
204-
*/
205-
protected function buildHeaders($extraHeaders = [])
206-
{
207-
$headers = [
208-
'Accept' => 'application/json',
209-
'Content-Type: application/json',
210-
];
211-
212-
return array_merge($headers, $extraHeaders);
213-
}
139+
214140

215141
/**
216142
* Handle the response

src/Firebase.php

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,12 @@ public function __construct($base, $token, ClientInterface $client = null)
9595
*/
9696
public function get($endpoint, $query = [])
9797
{
98-
$this->response = $this->client->get($endpoint,$query);
98+
$endpoint = $this->buildUri($endpoint, $query);
99+
$headers = $this->buildHeaders();
99100

100-
return $this;
101+
$this->response = $this->client->get($endpoint,$headers);
102+
103+
return $this->response;
101104
}
102105

103106
/**
@@ -111,9 +114,13 @@ public function get($endpoint, $query = [])
111114
*/
112115
public function post($endpoint, $data, $query = [])
113116
{
114-
$this->response = $this->client->post($endpoint,$data);
117+
$endpoint = $this->buildUri($endpoint, $query);
118+
$headers = $this->buildHeaders();
119+
$data = $this->prepareData($data);
120+
121+
$this->response = $this->client->post($endpoint,$data,$headers);
115122

116-
return $this;
123+
return $this->response;
117124
}
118125

119126
/**
@@ -127,9 +134,13 @@ public function post($endpoint, $data, $query = [])
127134
*/
128135
public function put($endpoint, $data, $query = [])
129136
{
130-
$this->response = $this->client->put($endpoint,$data);
137+
$endpoint = $this->buildUri($endpoint, $query);
138+
$headers = $this->buildHeaders();
139+
$data = $this->prepareData($data);
140+
141+
$this->response = $this->client->put($endpoint,$data,$headers);
131142

132-
return $this;
143+
return $this->response;
133144
}
134145

135146
/**
@@ -143,9 +154,13 @@ public function put($endpoint, $data, $query = [])
143154
*/
144155
public function patch($endpoint, $data, $query = [])
145156
{
146-
$this->response = $this->client->patch($endpoint,$data);
157+
$endpoint = $this->buildUri($endpoint, $query);
158+
$headers = $this->buildHeaders();
159+
$data = $this->prepareData($data);
160+
161+
$this->response = $this->client->patch($endpoint,$data,$headers);
147162

148-
return $this;
163+
return $this->response;
149164
}
150165

151166
/**
@@ -158,9 +173,12 @@ public function patch($endpoint, $data, $query = [])
158173
*/
159174
public function delete($endpoint, $query = [])
160175
{
161-
$this->response = $this->client->delete($endpoint);
176+
$endpoint = $this->buildUri($endpoint, $query);
177+
$headers = $this->buildHeaders();
178+
179+
$this->response = $this->client->delete($endpoint,$headers);
162180

163-
return $this;
181+
return $this->response;
164182
}
165183

166184
/**
@@ -202,4 +220,51 @@ protected function setClient(ClientInterface $client)
202220
{
203221
$this->client = $client;
204222
}
223+
224+
/**
225+
* Convert array|string to json
226+
*
227+
* @param array $data Data to be converted
228+
*
229+
* @return array
230+
*/
231+
protected function prepareData($data = [])
232+
{
233+
return json_encode($data);
234+
}
235+
236+
/**
237+
* Create a standard uri based on the end point
238+
* and add the auth token
239+
*
240+
* @param string $endpoint The sub endpoint
241+
* @param array $options Extra options to be added
242+
*
243+
* @return string
244+
*/
245+
protected function buildUri($endpoint, $options = [])
246+
{
247+
if ($this->token !== '') {
248+
$options['auth'] = $this->token;
249+
}
250+
251+
return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
252+
}
253+
254+
/**
255+
* Build all headers
256+
*
257+
* @param array $extraHeaders Extra headers to be added
258+
*
259+
* @return array
260+
*/
261+
protected function buildHeaders($extraHeaders = [])
262+
{
263+
$headers = [
264+
'Accept' => 'application/json',
265+
'Content-Type: application/json',
266+
];
267+
268+
return array_merge($headers, $extraHeaders);
269+
}
205270
}

0 commit comments

Comments
 (0)