Completed
Pull Request — master (#96)
by
unknown
07:11
created

FirebaseLib::getWithException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace Firebase;
3
4
require_once __DIR__ . '/firebaseInterface.php';
5
6
use Exception;
7
8
/**
9
 * Firebase PHP Client Library
10
 *
11
 * @author Tamas Kalman <[email protected]>
12
 * @url    https://github.com/ktamas77/firebase-php/
13
 * @link   https://www.firebase.com/docs/rest-api.html
14
 */
15
16
/**
17
 * Firebase PHP Class
18
 *
19
 * @author Tamas Kalman <[email protected]>
20
 * @link   https://www.firebase.com/docs/rest-api.html
21
 */
22
class FirebaseLib implements FirebaseInterface
23
{
24
    private $baseURI;
25
    private $timeout;
26
    private $token;
27
    private $curlHandler;
28
    private $sslConnection;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string $baseURI
34
     * @param string $token
35
     */
36
    public function __construct($baseURI = '', $token = '')
37
    {
38
        if ($baseURI === '') {
39
            trigger_error('You must provide a baseURI variable.', E_USER_ERROR);
40
        }
41
42
        if (!extension_loaded('curl')) {
43
            trigger_error('Extension CURL is not loaded.', E_USER_ERROR);
44
        }
45
46
        $this->setBaseURI($baseURI);
47
        $this->setTimeOut(10);
48
        $this->setToken($token);
49
        $this->initCurlHandler();
50
        $this->setSSLConnection(true);
51
    }
52
53
    /**
54
     * Initializing the CURL handler
55
     *
56
     * @return void
57
     */
58
    public function initCurlHandler()
59
    {
60
        $this->curlHandler = curl_init();
61
    }
62
63
    /**
64
     * Closing the CURL handler
65
     *
66
     * @return void
67
     */
68
    public function closeCurlHandler()
69
    {
70
        curl_close($this->curlHandler);
71
    }
72
73
    /**
74
     * Enabling/Disabling SSL Connection
75
     *
76
     * @param bool $enableSSLConnection
77
     */
78
    public function setSSLConnection($enableSSLConnection)
79
    {
80
        $this->sslConnection = $enableSSLConnection;
81
    }
82
83
    /**
84
     * Returns status of SSL Connection
85
     *
86
     * @return boolean
87
     */
88
    public function getSSLConnection()
89
    {
90
        return $this->sslConnection;
91
    }
92
93
    /**
94
     * Sets Token
95
     *
96
     * @param string $token Token
97
     *
98
     * @return void
99
     */
100
    public function setToken($token)
101
    {
102
        $this->token = $token;
103
    }
104
105
    /**
106
     * Sets Base URI, ex: http://yourcompany.firebase.com/youruser
107
     *
108
     * @param string $baseURI Base URI
109
     *
110
     * @return void
111
     */
112
    public function setBaseURI($baseURI)
113
    {
114
        $baseURI .= (substr($baseURI, -1) === '/' ? '' : '/');
115
        $this->baseURI = $baseURI;
116
    }
117
118
    /**
119
     * Returns with the normalized JSON absolute path
120
     *
121
     * @param  string $path Path
122
     * @param  array $options Options
123
     * @return string
124
     */
125
    private function getJsonPath($path, $options = array())
126
    {
127
        $url = $this->baseURI;
128
        if ($this->token !== '') {
129
            $options['auth'] = $this->token;
130
        }
131
        $path = ltrim($path, '/');
132
        $query = http_build_query($options);
133
        return "$url$path.json?$query";
134
    }
135
136
    /**
137
     * Sets REST call timeout in seconds
138
     *
139
     * @param integer $seconds Seconds to timeout
140
     *
141
     * @return void
142
     */
143
    public function setTimeOut($seconds)
144
    {
145
        $this->timeout = $seconds;
146
    }
147
148
    /**
149
     * Writing data into Firebase with a PUT request
150
     * HTTP 200: Ok
151
     *
152
     * @param string $path Path
153
     * @param mixed $data Data
154
     * @param array $options Options
155
     *
156
     * @return array Response
157
     * @throws Exception
158
     */
159
    public function set($path, $data, array $options = [])
160
    {
161
        try {
162
            return $this->writeData($path, $data, 'PUT', $options);
163
        } catch (Exception $e) {
164
            throw new Exception($e);
165
        }
166
    }
167
168
    /**
169
     * Pushing data into Firebase with a POST request
170
     * HTTP 200: Ok
171
     *
172
     * @param string $path Path
173
     * @param mixed $data Data
174
     * @param array $options Options
175
     *
176
     * @return array Response
177
     * @throws Exception
178
     */
179
    public function push($path, $data, array $options = [])
180
    {
181
        try {
182
            return $this->writeData($path, $data, 'POST', $options);
183
        } catch (Exception $e) {
184
            throw new Exception($e);
185
        }
186
    }
187
188
    /**
189
     * Updating data into Firebase with a PATH request
190
     * HTTP 200: Ok
191
     *
192
     * @param string $path Path
193
     * @param mixed $data Data
194
     * @param array $options Options
195
     *
196
     * @return array Response
197
     * @throws Exception
198
     */
199
    public function update($path, $data, array $options = [])
200
    {
201
        try {
202
            return $this->writeData($path, $data, 'PATCH', $options);
203
        } catch (Exception $e) {
204
            throw new Exception($e);
205
        }
206
    }
207
208
    /**
209
     * Reading data from Firebase
210
     * HTTP 200: Ok
211
     *
212
     * @param string $path Path
213
     * @param array $options Options
214
     *
215
     * @return array Response
216
     * @throws Exception
217
     */
218
    public function get($path, array $options = [])
219
    {
220
        $return = null;
221
        try {
222
            $ch = $this->getCurlHandler($path, 'GET', $options);
223
            $return = curl_exec($ch);
224
        } catch (Exception $e) {
225
            //return null in consult-api in case of exception
226
            throw new Exception($e);
227
        }
228
        return $return;
229
    }
230
231
    /**
232
     * Reading data from Firebase
233
     * HTTP 200: Ok
234
     *
235
     * @param string $path Path
236
     * @param array $options Options
237
     *
238
     * @return array Response
239
     */
240
    public function getWithException($path, array $options = [])
241
    {
242
        $ch = $this->getCurlHandler($path, 'GET', $options);
243
244
        return curl_exec($ch);
245
    }
246
247
    /**
248
     * Deletes data from Firebase
249
     * HTTP 204: Ok
250
     *
251
     * @param string $path Path
252
     * @param array $options Options
253
     *
254
     * @return array Response
255
     * @throws Exception
256
     */
257
    public function delete($path, array $options = [])
258
    {
259
        $return = null;
260
        try {
261
            $ch = $this->getCurlHandler($path, 'DELETE', $options);
262
            $return = curl_exec($ch);
263
        } catch (Exception $e) {
264
            //return null in consult-api in case of exception
265
            throw new Exception($e);
266
        }
267
        return $return;
268
    }
269
270
    /**
271
     * Returns with Initialized CURL Handler
272
     *
273
     * @param string $path Path
274
     * @param string $mode Mode
275
     * @param array $options Options
276
     *
277
     * @return resource Curl Handler
278
     */
279
    private function getCurlHandler($path, $mode, $options = array())
280
    {
281
        $url = $this->getJsonPath($path, $options);
282
        $ch = $this->curlHandler;
283
        curl_setopt($ch, CURLOPT_URL, $url);
284
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
285
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
286
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getSSLConnection());
287
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
288
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
289
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
290
        return $ch;
291
    }
292
293
    private function writeData($path, $data, $method = 'PUT', $options = array())
294
    {
295
        $jsonData = json_encode($data);
296
        $header = array(
297
            'Content-Type: application/json',
298
            'Content-Length: ' . strlen($jsonData)
299
        );
300
        try {
301
            $ch = $this->getCurlHandler($path, $method, $options);
302
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
303
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
304
            $return = curl_exec($ch);
305
        } catch (Exception $e) {
306
            $return = null;
307
        }
308
        return $return;
309
    }
310
}
311