|
| 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 | +} |
0 commit comments