|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the firebase-php package. |
| 4 | + * |
| 5 | + * For the full copyright and license information, please read the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Kreait\Firebase; |
| 10 | + |
| 11 | +use Ivory\HttpAdapter\CurlHttpAdapter; |
| 12 | +use Ivory\HttpAdapter\HttpAdapterException; |
| 13 | +use Ivory\HttpAdapter\HttpAdapterInterface; |
| 14 | +use Ivory\HttpAdapter\Message\RequestInterface; |
| 15 | + |
| 16 | +class Firebase implements FirebaseInterface |
| 17 | +{ |
| 18 | + use \Psr\Log\LoggerAwareTrait; |
| 19 | + |
| 20 | + /** |
| 21 | + * The HTTP adapter. |
| 22 | + * |
| 23 | + * @var HttpAdapterInterface |
| 24 | + */ |
| 25 | + private $http; |
| 26 | + |
| 27 | + /** |
| 28 | + * Firebase client initialization. |
| 29 | + * |
| 30 | + * @param string $baseUrl The Firebase app base URL. |
| 31 | + * @param HttpAdapterInterface $http The HTTP adapter. |
| 32 | + * @throws FirebaseException When the base URL is not valid. |
| 33 | + */ |
| 34 | + public function __construct($baseUrl, HttpAdapterInterface $http = null) |
| 35 | + { |
| 36 | + $this->logger = new \Psr\Log\NullLogger(); |
| 37 | + $this->http = $http ?: new CurlHttpAdapter(); |
| 38 | + |
| 39 | + $configuration = $this->http->getConfiguration(); |
| 40 | + $configuration->setBaseUrl(Utils::normalizeBaseUrl($baseUrl)); |
| 41 | + $configuration->setKeepAlive(true); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + public function get($location = null) |
| 48 | + { |
| 49 | + return $this->send($location, RequestInterface::METHOD_GET); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function set($data, $location) |
| 56 | + { |
| 57 | + return $this->send($location, RequestInterface::METHOD_PUT, $data); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritdoc} |
| 62 | + */ |
| 63 | + public function push($data, $location) |
| 64 | + { |
| 65 | + return $this->send($location, RequestInterface::METHOD_POST, $data); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + public function update($data, $location) |
| 72 | + { |
| 73 | + return $this->send($location, RequestInterface::METHOD_PATCH, $data); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritdoc} |
| 78 | + */ |
| 79 | + public function delete($location) |
| 80 | + { |
| 81 | + return $this->send($location, RequestInterface::METHOD_DELETE); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Sends the request and returns the processed response data. |
| 86 | + * |
| 87 | + * @param string $location The location. |
| 88 | + * @param string $method The HTTP method. |
| 89 | + * @param array|object|null $data The data. |
| 90 | + * |
| 91 | + * @throws FirebaseException |
| 92 | + * |
| 93 | + * @return array|string|void The processed response data. |
| 94 | + */ |
| 95 | + private function send($location, $method, $data = null) |
| 96 | + { |
| 97 | + $location = (string) $location; // In case it is null |
| 98 | + |
| 99 | + if ($data && !is_array($data) && !is_object($data)) { |
| 100 | + throw new \InvalidArgumentException(sprintf('array or object expected, %s given'), gettype($data)); |
| 101 | + } |
| 102 | + |
| 103 | + $relativeUrl = sprintf('/%s.json', Utils::normalizeLocation($location)); |
| 104 | + |
| 105 | + $headers = [ |
| 106 | + 'accept' => 'application/json', |
| 107 | + 'accept-charset' => 'utf-8', |
| 108 | + ]; |
| 109 | + |
| 110 | + $this->logger->debug(sprintf('%s request to %s', $method, $relativeUrl), ['data_sent' => $data]); |
| 111 | + $request = $this->http->getConfiguration()->getMessageFactory()->createRequest( |
| 112 | + $relativeUrl, |
| 113 | + $method, |
| 114 | + RequestInterface::PROTOCOL_VERSION_1_1, |
| 115 | + $headers, |
| 116 | + json_encode($data) |
| 117 | + ); |
| 118 | + |
| 119 | + try { |
| 120 | + $response = $this->http->sendRequest($request); |
| 121 | + } catch (HttpAdapterException $e) { |
| 122 | + $response = $e->hasResponse() ? $e->getResponse() : null; |
| 123 | + throw FirebaseException::httpError($request, $response); |
| 124 | + } |
| 125 | + |
| 126 | + switch ($response->getStatusCode()) { |
| 127 | + case 400: |
| 128 | + $this->logger->warning('Invalid location or PUT/POST data'); |
| 129 | + throw FirebaseException::invalidDataOrLocation($request, $response); |
| 130 | + case 403: |
| 131 | + $this->logger->warning('Forbidden'); |
| 132 | + throw FirebaseException::forbiddenAction($request, $response); |
| 133 | + case 404: |
| 134 | + $this->logger->warning('Request made of HTTP instead of HTTPS'); |
| 135 | + throw FirebaseException::requestMadeOverHttpsInsteadOfHttp($request, $response); |
| 136 | + case 417: |
| 137 | + $this->logger->warning('No namespace specified'); |
| 138 | + throw FirebaseException::noNameSpaceSpecified($request, $response); |
| 139 | + } |
| 140 | + |
| 141 | + if (!$response->hasBody()) { |
| 142 | + $this->logger->debug( |
| 143 | + sprintf('Received valid, empty response from %s request to %s', $method, $relativeUrl) |
| 144 | + ); |
| 145 | + |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + $contents = $response->getBody()->getContents(); |
| 150 | + $this->logger->debug( |
| 151 | + sprintf('Received valid response from %s request to %s', $method, $relativeUrl), |
| 152 | + ['data_received' => $contents] |
| 153 | + ); |
| 154 | + |
| 155 | + return json_decode($contents, true); |
| 156 | + } |
| 157 | +} |
0 commit comments