Completed
Pull Request — master (#94)
by
unknown
05:04
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
     */
158
    public function set($path, $data, array $options = [])
159
    {
160
        return $this->writeData($path, $data, 'PUT', $options);
161
    }
162
163
    /**
164
     * Pushing data into Firebase with a POST request
165
     * HTTP 200: Ok
166
     *
167
     * @param string $path Path
168
     * @param mixed $data Data
169
     * @param array $options Options
170
     *
171
     * @return array Response
172
     */
173
    public function push($path, $data, array $options = [])
174
    {
175
        return $this->writeData($path, $data, 'POST', $options);
176
    }
177
178
    /**
179
     * Updating data into Firebase with a PATH request
180
     * HTTP 200: Ok
181
     *
182
     * @param string $path Path
183
     * @param mixed $data Data
184
     * @param array $options Options
185
     *
186
     * @return array Response
187
     */
188
    public function update($path, $data, array $options = [])
189
    {
190
        return $this->writeData($path, $data, 'PATCH', $options);
191
    }
192
193
    /**
194
     * Reading data from Firebase
195
     * HTTP 200: Ok
196
     *
197
     * @param string $path Path
198
     * @param array $options Options
199
     *
200
     * @return array Response
201
     */
202
    public function get($path, array $options = [])
203
    {
204
        try {
205
            $ch = $this->getCurlHandler($path, 'GET', $options);
206
            $return = curl_exec($ch);
207
        } catch (Exception $e) {
208
            $return = null;
209
        }
210
        return $return;
211
    }
212
213
    /**
214
     * Reading data from Firebase
215
     * HTTP 200: Ok
216
     *
217
     * @param string $path Path
218
     * @param array $options Options
219
     *
220
     * @return array Response
221
     */
222
    public function getWithException($path, array $options = [])
223
    {
224
        $ch = $this->getCurlHandler($path, 'GET', $options);
225
226
        return curl_exec($ch);
227
    }
228
229
    /**
230
     * Deletes data from Firebase
231
     * HTTP 204: Ok
232
     *
233
     * @param string $path Path
234
     * @param array $options Options
235
     *
236
     * @return array Response
237
     */
238
    public function delete($path, array $options = [])
239
    {
240
        try {
241
            $ch = $this->getCurlHandler($path, 'DELETE', $options);
242
            $return = curl_exec($ch);
243
        } catch (Exception $e) {
244
            $return = null;
245
        }
246
        return $return;
247
    }
248
249
    /**
250
     * Returns with Initialized CURL Handler
251
     *
252
     * @param string $path Path
253
     * @param string $mode Mode
254
     * @param array $options Options
255
     *
256
     * @return resource Curl Handler
257
     */
258
    private function getCurlHandler($path, $mode, $options = array())
259
    {
260
        $url = $this->getJsonPath($path, $options);
261
        $ch = $this->curlHandler;
262
        curl_setopt($ch, CURLOPT_URL, $url);
263
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
264
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
265
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getSSLConnection());
266
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
267
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
268
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
269
        return $ch;
270
    }
271
272
    private function writeData($path, $data, $method = 'PUT', $options = array())
273
    {
274
        $jsonData = json_encode($data);
275
        $header = array(
276
            'Content-Type: application/json',
277
            'Content-Length: ' . strlen($jsonData)
278
        );
279
        try {
280
            $ch = $this->getCurlHandler($path, $method, $options);
281
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
282
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
283
            $return = curl_exec($ch);
284
        } catch (Exception $e) {
285
            $return = null;
286
        }
287
        return $return;
288
    }
289
}
290