Skip to content

Commit 9aa1d69

Browse files
committed
Carpe Diem
0 parents  commit 9aa1d69

File tree

13 files changed

+564
-0
lines changed

13 files changed

+564
-0
lines changed

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Enforce Unix newlines
2+
* text=lf
3+
4+
# Exclude unused files
5+
# see: https://redd.it/2jzp6k
6+
/example export-ignore
7+
/tests export-ignore
8+
/.codeclimate.yml export-ignore
9+
/.coveralls.yml export-ignore
10+
/.editorconfig export-ignore
11+
/.gitattributes export-ignore
12+
/.gitignore export-ignore
13+
/.travis.yml export-ignore
14+
/CONTRIBUTING.md export-ignore
15+
/README.md export-ignore
16+
/phpcs.xml export-ignore
17+
/phpunit.xml.dist export-ignore

.gitignore

Whitespace-only changes.

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
7+
- hhvm
8+
9+
sudo: required
10+
11+
install: composer install
12+
13+
before_script:
14+
# navigate out of module directory to prevent blown stack by recursive module lookup
15+
- wget https://phar.phpunit.de/phpunit.phar
16+
- chmod +x phpunit.phar
17+
- sudo mv phpunit.phar /usr/local/bin/phpunit
18+
- phpunit --version
19+
20+
- wget http://getcomposer.org/composer.phar
21+
- php composer.phar install
22+
23+
script: phpunit --bootstrap tests/bootstrap.php tests

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2016, Alejandro Morelos.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[PHP-Firebase](https://github.com/adrorocker/php-firebase)
2+
===================================
3+
4+
A PHP SDK for Firebase REST API.
5+
6+
-----------------------------------
7+
8+
Authors:
9+
10+
[Alejandro Morelos](https://github.com/adrorocker).

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "adrorocker/php-firebase",
3+
"description": "A PHP SDK for Google Firebase",
4+
"license": "MIT",
5+
"keywords": ["sdk", "php", "firebase", "google", "adrorocker"],
6+
"homepage": "https://github.com/adrorocker/php-firebase",
7+
"type": "library",
8+
"authors": [
9+
{
10+
"name": "Adro Rocker",
11+
"email": "alejandro.morelos@jarwebdev.com",
12+
"homepage": "https://github.com/adrorocker"
13+
}
14+
],
15+
"require": {
16+
"php": ">= 5.6",
17+
"guzzlehttp/guzzle": "~6.0"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "5.6.*"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"PhpFirebase\\": ["src/","tests/"]
25+
},
26+
"files": [
27+
]
28+
},
29+
"extra": {
30+
"branch-alias": {
31+
"dev-master": "0.4-dev"
32+
}
33+
}
34+
}

src/Clients/GuzzleClient.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* PHP-Firebase
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
* @copyright Copyright (c) 2016 Adro Rocker
7+
* @author Adro Rocker <alejandro.morelos@jarwebdev.com>
8+
*/
9+
namespace PhpFirebase\Clients;
10+
11+
use InvalidArgumentException;
12+
use PhpFirebase\Interfaces\ClientInterface;
13+
use GuzzleHttp\Client as HttpClient;
14+
use GuzzleHttp\Exception\ClientException;
15+
use GuzzleHttp\Psr7\Request;
16+
use GuzzleHttp\Psr7\Response;
17+
use GuzzleHttp\Psr7\Uri;
18+
use function GuzzleHttp\Psr7\stream_for;
19+
20+
class GuzzleClient implements ClientInterface
21+
{
22+
protected $guzzle;
23+
24+
protected $base;
25+
26+
protected $token;
27+
28+
public function __construct(array $options = [])
29+
{
30+
if (!isset($options['base'])) {
31+
throw new InvalidArgumentException("Missign base path");
32+
}
33+
34+
if (!isset($options['token'])) {
35+
throw new InvalidArgumentException("Missign token");
36+
}
37+
38+
$this->base = $options['base'];
39+
$this->token = $options['token'];
40+
41+
$this->guzzle = new HttpClient($options);
42+
}
43+
44+
public function get($endpoint)
45+
{
46+
$request = new Request('GET',$this->buildUri($endpoint), $this->buildHeaders());
47+
48+
$response = $this->guzzle->send($request);
49+
50+
return $this->handle($response);
51+
}
52+
53+
public function post($endpoint, $data)
54+
{
55+
$data = $this->prepareData($data);
56+
57+
$request = new Request('POST',$this->buildUri($endpoint),$this->buildHeaders(),$data);
58+
59+
$response = $this->guzzle->send($request);
60+
61+
return $this->handle($response);
62+
}
63+
64+
public function put($endpoint, $data)
65+
{
66+
$data = $this->prepareData($data);
67+
68+
$request = new Request('PUT',$this->buildUri($endpoint),$this->buildHeaders(),$data);
69+
70+
$response = $this->guzzle->send($request);
71+
72+
return $this->handle($response);
73+
}
74+
75+
public function patch($endpoint, $data)
76+
{
77+
$data = $this->prepareData($data);
78+
79+
$request = new Request('PATCH',$this->buildUri($endpoint),$this->buildHeaders(),$data);
80+
81+
$response = $this->guzzle->send($request);
82+
83+
return $this->handle($response);
84+
}
85+
86+
public function delete($endpoint)
87+
{
88+
$request = new Request('DELETE',$this->buildUri($endpoint), $this->buildHeaders());
89+
90+
$response = $this->guzzle->send($request);
91+
92+
return $this->handle($response);
93+
}
94+
95+
protected function prepareData($data = [])
96+
{
97+
return json_encode($data);
98+
}
99+
100+
protected function buildUri($endpoint, $options = [])
101+
{
102+
if ($this->token !== '') {
103+
$options['auth'] = $this->token;
104+
}
105+
106+
return $this->base . '/' . ltrim($endpoint, '/') . '.json?' . http_build_query($options, '', '&');
107+
}
108+
109+
protected function buildHeaders($extraHeaders = [])
110+
{
111+
$headers = [
112+
'Accept' => 'application/json',
113+
'Content-Type: application/json',
114+
];
115+
116+
return array_merge($headers, $extraHeaders);
117+
}
118+
119+
private function handle(Response $response, $default = null)
120+
{
121+
$stream = stream_for($response->getBody());
122+
123+
$data = json_decode($stream->getContents());
124+
125+
return $data;
126+
}
127+
128+
}

src/Firebase.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* PHP-Firebase
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
* @copyright Copyright (c) 2016 Adro Rocker
7+
* @author Adro Rocker <alejandro.morelos@jarwebdev.com>
8+
*/
9+
namespace PhpFirebase;
10+
11+
use InvalidArgumentException;
12+
use PhpFirebase\Clients\GuzzleClient;
13+
use PhpFirebase\Interfaces\FirebaseInterface;
14+
use PhpFirebase\Interfaces\ClientInterface;
15+
16+
class Firebase implements FirebaseInterface
17+
{
18+
protected $base;
19+
20+
protected $token;
21+
22+
protected $client = null;
23+
24+
protected $response = null;
25+
26+
public function __construct($base, $token, ClientInterface $client = null)
27+
{
28+
if (!is_string($base)) {
29+
throw new InvalidArgumentException("Base parameter needs to be string", 1);
30+
}
31+
if (!is_string($token)) {
32+
throw new InvalidArgumentException("Token parameter needs to be string", 1);
33+
}
34+
35+
$parts = parse_url($base);
36+
37+
if (!isset($parts['scheme']) || !isset($parts['host'])) {
38+
throw new InvalidArgumentException("The base URL $base is not valid", 1);
39+
}
40+
41+
$this->base = rtrim($base,'/');
42+
$this->token = (string) $token;
43+
44+
if (!$client) {
45+
$client = new GuzzleClient([
46+
'base' => $this->base,
47+
'token' => $this->token,
48+
]);
49+
}
50+
51+
$this->setClient($client);
52+
}
53+
54+
public function get($endpoint)
55+
{
56+
$this->response = $this->client->get($endpoint);
57+
58+
return $this;
59+
}
60+
61+
public function post($endpoint, $data)
62+
{
63+
$this->response = $this->client->post($endpoint,$data);
64+
65+
return $this;
66+
}
67+
68+
public function put($endpoint, $data)
69+
{
70+
$this->response = $this->client->put($endpoint,$data);
71+
72+
return $this;
73+
}
74+
75+
public function patch($endpoint, $data)
76+
{
77+
$this->response = $this->client->patch($endpoint,$data);
78+
79+
return $this;
80+
}
81+
82+
public function delete($endpoint)
83+
{
84+
$this->response = $this->client->delete($endpoint);
85+
86+
return $this;
87+
}
88+
89+
public function getResponse()
90+
{
91+
return $this->response;
92+
}
93+
94+
public function getClient()
95+
{
96+
return $this->client;
97+
}
98+
99+
public function getBase()
100+
{
101+
return $this->base;
102+
}
103+
104+
protected function setClient(ClientInterface $client)
105+
{
106+
$this->client = $client;
107+
}
108+
109+
}

src/Interfaces/ClientInterface.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* PHP-Firebase
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
* @copyright Copyright (c) 2016 Adro Rocker
7+
* @author Adro Rocker <alejandro.morelos@jarwebdev.com>
8+
*/
9+
namespace PhpFirebase\Interfaces;
10+
11+
interface ClientInterface
12+
{
13+
public function get($endpoint);
14+
15+
public function post($endpoint, $data);
16+
17+
public function put($endpoint, $data);
18+
19+
public function patch($endpoint, $data);
20+
21+
public function delete($endpoint);
22+
}

src/Interfaces/FirebaseInterface.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* PHP-Firebase
4+
*
5+
* @link https://github.com/adrorocker/php-firebase
6+
* @copyright Copyright (c) 2016 Adro Rocker
7+
* @author Adro Rocker <alejandro.morelos@jarwebdev.com>
8+
*/
9+
namespace PhpFirebase\Interfaces;
10+
11+
use PhpFirebase\Interfaces\ClientInterface;
12+
13+
interface FirebaseInterface
14+
{
15+
public function __construct($base, $token);
16+
17+
public function get($endpoint);
18+
19+
public function post($endpoint, $data);
20+
21+
public function put($endpoint, $data);
22+
23+
public function patch($endpoint, $data);
24+
25+
public function delete($endpoint);
26+
}

0 commit comments

Comments
 (0)