Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Exclude unused files
# see: https://redd.it/2jzp6k
/example export-ignore
/docs export-ignore
/tests export-ignore
/.codeclimate.yml export-ignore
/.coveralls.yml export-ignore
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ script:
- phpunit -c phpunit.xml.dist

after_script:
- travis_retry php vendor/bin/coveralls -v
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then travis_retry php vendor/bin/coveralls -v; fi
- if [[ "$TRAVIS_PHP_VERSION" == '7.0' ]]; then travis_retry php vendor/bin/coveralls -v; fi
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ A PHP SDK for Firebase REST API.
```
composer require adrorocker/php-firebase
```
-----------------------------------

## Usage

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

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

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

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

## Authors:

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

[Master]: https://travis-ci.org/adrorocker/php-firebase/
[Master image]: https://travis-ci.org/adrorocker/php-firebase.svg?branch=master&style=flat-square
[Master covarage]: https://coveralls.io/github/adrorocker/php-firebase?branch=master
[Master covarage image]: https://coveralls.io/repos/github/adrorocker/php-firebase/badge.svg?branch=master&style=flat-square
[Master image]: https://travis-ci.org/adrorocker/php-firebase.svg?branch=master
[Master covarage]: https://coveralls.io/github/adrorocker/php-firebase
[Master covarage image]: https://coveralls.io/repos/github/adrorocker/php-firebase/badge.svg?branch=master
Empty file added docs/.gitkeep
Empty file.
120 changes: 23 additions & 97 deletions src/Clients/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,31 @@ class GuzzleClient implements ClientInterface
protected $guzzle;

/**
* Base endpoint
* Set the the guzzle client
*
* @var string
* @param array $options The options to set the defaul
* @param Object|null $client Client to make the requests
*/
protected $base;

/**
* Token
*
* @var string
*/
protected $token;

/**
* Set the base path for Firebase endpont
* the token to authenticate and the guzzle client
*
* @param string $base The base endpoint
* @param string $token The token
* @param \PhpFirebase\Interfaces\ClientInterface|null $client Client to make the request
*/
public function __construct(array $options = [])
public function __construct(array $options = [], $client = null)
{
if (!isset($options['base'])) {
throw new InvalidArgumentException("Missign base path");
if (!$client) {
$client = new HttpClient($options);
}

if (!isset($options['token'])) {
throw new InvalidArgumentException("Missign token");
}

$this->base = $options['base'];
$this->token = $options['token'];

$this->guzzle = new HttpClient($options);
$this->guzzle = $client;
}

/**
* Create a new GET reuest
*
* @param string $endpoint The sub endpoint
* @param array $query Query parameters
* @param array $headers Request headers
*
* @return array
*/
public function get($endpoint, $query = [])
public function get($endpoint, $headers = [])
{
$request = new Request('GET',$this->buildUri($endpoint, $query), $this->buildHeaders());
$request = new Request('GET',$endpoint, $headers);

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

Expand All @@ -93,15 +70,13 @@ public function get($endpoint, $query = [])
*
* @param string $endpoint The sub endpoint
* @param string|array $data The data to be submited
* @param array $query Query parameters
* @param array $headers Request headers
*
* @return array
*/
public function post($endpoint, $data, $query = [])
public function post($endpoint, $data, $headers = [])
{
$data = $this->prepareData($data);

$request = new Request('POST',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
$request = new Request('POST',$endpoint, $headers, $data);

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

Expand All @@ -113,15 +88,13 @@ public function post($endpoint, $data, $query = [])
*
* @param string $endpoint The sub endpoint
* @param string|array $data The data to be submited
* @param array $query Query parameters
* @param array $headers Request headers
*
* @return array
*/
public function put($endpoint, $data, $query = [])
public function put($endpoint, $data, $headers = [])
{
$data = $this->prepareData($data);

$request = new Request('PUT',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
$request = new Request('PUT',$endpoint, $headers, $data);

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

Expand All @@ -133,15 +106,13 @@ public function put($endpoint, $data, $query = [])
*
* @param string $endpoint The sub endpoint
* @param string|array $data The data to be submited
* @param array $query Query parameters
* @param array $headers Request headers
*
* @return array
*/
public function patch($endpoint, $data, $query = [])
public function patch($endpoint, $data, $headers = [])
{
$data = $this->prepareData($data);

$request = new Request('PATCH',$this->buildUri($endpoint, $query),$this->buildHeaders(),$data);
$request = new Request('PATCH',$endpoint, $headers, $data);

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

Expand All @@ -152,65 +123,20 @@ public function patch($endpoint, $data, $query = [])
* Create a new DELETE reuest
*
* @param string $endpoint The sub endpoint
* @param array $query Query parameters
* @param array $headers Request headers
*
* @return array
*/
public function delete($endpoint, $query = [])
public function delete($endpoint, $headers = [])
{
$request = new Request('DELETE',$this->buildUri($endpoint, $query), $this->buildHeaders());
$request = new Request('DELETE',$endpoint, $headers);

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

return $this->handle($response);
}

/**
* Convert array|string to json
*
* @param array $data Data to be converted
*
* @return array
*/
protected function prepareData($data = [])
{
return json_encode($data);
}

/**
* Create a standard uri based on the end point
* and add the auth token
*
* @param string $endpoint The sub endpoint
* @param array $options Extra options to be added
*
* @return string
*/
protected function buildUri($endpoint, $options = [])
{
if ($this->token !== '') {
$options['auth'] = $this->token;
}

return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
}

/**
* Build all headers
*
* @param array $extraHeaders Extra headers to be added
*
* @return array
*/
protected function buildHeaders($extraHeaders = [])
{
$headers = [
'Accept' => 'application/json',
'Content-Type: application/json',
];

return array_merge($headers, $extraHeaders);
}


/**
* Handle the response
Expand Down
85 changes: 75 additions & 10 deletions src/Firebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ public function __construct($base, $token, ClientInterface $client = null)
*/
public function get($endpoint, $query = [])
{
$this->response = $this->client->get($endpoint,$query);
$endpoint = $this->buildUri($endpoint, $query);
$headers = $this->buildHeaders();

return $this;
$this->response = $this->client->get($endpoint,$headers);

return $this->response;
}

/**
Expand All @@ -111,9 +114,13 @@ public function get($endpoint, $query = [])
*/
public function post($endpoint, $data, $query = [])
{
$this->response = $this->client->post($endpoint,$data);
$endpoint = $this->buildUri($endpoint, $query);
$headers = $this->buildHeaders();
$data = $this->prepareData($data);

$this->response = $this->client->post($endpoint,$data,$headers);

return $this;
return $this->response;
}

/**
Expand All @@ -127,9 +134,13 @@ public function post($endpoint, $data, $query = [])
*/
public function put($endpoint, $data, $query = [])
{
$this->response = $this->client->put($endpoint,$data);
$endpoint = $this->buildUri($endpoint, $query);
$headers = $this->buildHeaders();
$data = $this->prepareData($data);

$this->response = $this->client->put($endpoint,$data,$headers);

return $this;
return $this->response;
}

/**
Expand All @@ -143,9 +154,13 @@ public function put($endpoint, $data, $query = [])
*/
public function patch($endpoint, $data, $query = [])
{
$this->response = $this->client->patch($endpoint,$data);
$endpoint = $this->buildUri($endpoint, $query);
$headers = $this->buildHeaders();
$data = $this->prepareData($data);

$this->response = $this->client->patch($endpoint,$data,$headers);

return $this;
return $this->response;
}

/**
Expand All @@ -158,9 +173,12 @@ public function patch($endpoint, $data, $query = [])
*/
public function delete($endpoint, $query = [])
{
$this->response = $this->client->delete($endpoint);
$endpoint = $this->buildUri($endpoint, $query);
$headers = $this->buildHeaders();

$this->response = $this->client->delete($endpoint,$headers);

return $this;
return $this->response;
}

/**
Expand Down Expand Up @@ -202,4 +220,51 @@ protected function setClient(ClientInterface $client)
{
$this->client = $client;
}

/**
* Convert array|string to json
*
* @param array $data Data to be converted
*
* @return array
*/
protected function prepareData($data = [])
{
return json_encode($data);
}

/**
* Create a standard uri based on the end point
* and add the auth token
*
* @param string $endpoint The sub endpoint
* @param array $options Extra options to be added
*
* @return string
*/
protected function buildUri($endpoint, $options = [])
{
if ($this->token !== '') {
$options['auth'] = $this->token;
}

return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
}

/**
* Build all headers
*
* @param array $extraHeaders Extra headers to be added
*
* @return array
*/
protected function buildHeaders($extraHeaders = [])
{
$headers = [
'Accept' => 'application/json',
'Content-Type: application/json',
];

return array_merge($headers, $extraHeaders);
}
}
Loading