Completed
Pull Request — master (#63)
by
unknown
01:32
created

FirebaseLib::getResponseInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace Firebase;
3
4
require_once __DIR__ . '/firebaseInterface.php';
5
6
use \Exception;
7
8
9
/**
10
 * Firebase PHP Client Library
11
 *
12
 * @author Tamas Kalman <[email protected]>
13
 * @url    https://github.com/ktamas77/firebase-php/
14
 * @link   https://www.firebase.com/docs/rest-api.html
15
 */
16
17
/**
18
 * Firebase PHP Class
19
 *
20
 * @author Tamas Kalman <[email protected]>
21
 * @link   https://www.firebase.com/docs/rest-api.html
22
 */
23
class FirebaseLib implements FirebaseInterface
24
{
25
    private $_baseURI;
26
    private $_timeout;
27
    private $_token;
28
    private $_curlHandler;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param string $baseURI
34
     * @param string $token
35
     */
36
    function __construct($baseURI = '', $token = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
    }
51
52
    /**
53
     * Initializing the CURL handler
54
     *
55
     * @return void
56
     */
57
    public function initCurlHandler()
58
    {
59
        $this->_curlHandler = curl_init();
60
    }
61
62
    /**
63
     * Closing the CURL handler
64
     *
65
     * @return void
66
     */
67
    public function closeCurlHandler()
68
    {
69
        curl_close($this->_curlHandler);
70
    }
71
72
    /**
73
     * Sets Token
74
     *
75
     * @param string $token Token
76
     *
77
     * @return void
78
     */
79
    public function setToken($token)
80
    {
81
        $this->_token = $token;
82
    }
83
84
    /**
85
     * Sets Base URI, ex: http://yourcompany.firebase.com/youruser
86
     *
87
     * @param string $baseURI Base URI
88
     *
89
     * @return void
90
     */
91
    public function setBaseURI($baseURI)
92
    {
93
        $baseURI .= (substr($baseURI, -1) == '/' ? '' : '/');
94
        $this->_baseURI = $baseURI;
95
    }
96
97
    /**
98
     * Returns with the normalized JSON absolute path
99
     *
100
     * @param  string $path Path
101
     * @param  array $options Options
102
     * @return string
103
     */
104
    private function _getJsonPath($path, $options = array())
105
    {
106
        $url = $this->_baseURI;
107
        if ($this->_token !== '') {
108
            $options['auth'] = $this->_token;
109
        }
110
        $path = ltrim($path, '/');
111
        return $url . $path . '.json?' . http_build_query($options);
112
    }
113
114
	/**
115
	 * Gets info for the last request made
116
	 *
117
	 * @return array details from curl_getinfo
118
	 */
119
	public function getResponseInfo()
120
	{
121
		return curl_getinfo($this->_curlHandler);
122
	}
123
124
    /**
125
     * Sets REST call timeout in seconds
126
     *
127
     * @param integer $seconds Seconds to timeout
128
     *
129
     * @return void
130
     */
131
    public function setTimeOut($seconds)
132
    {
133
        $this->_timeout = $seconds;
134
    }
135
136
    /**
137
     * Writing data into Firebase with a PUT request
138
     * HTTP 200: Ok
139
     *
140
     * @param string $path Path
141
     * @param mixed $data Data
142
     * @param array $options Options
143
     *
144
     * @return array Response
145
     */
146
    public function set($path, $data, $options = array())
147
    {
148
        return $this->_writeData($path, $data, 'PUT', $options);
149
    }
150
151
    /**
152
     * Pushing data into Firebase with a POST request
153
     * HTTP 200: Ok
154
     *
155
     * @param string $path Path
156
     * @param mixed $data Data
157
     * @param array $options Options
158
     *
159
     * @return array Response
160
     */
161
    public function push($path, $data, $options = array())
162
    {
163
        return $this->_writeData($path, $data, 'POST', $options);
164
    }
165
166
    /**
167
     * Updating data into Firebase with a PATH request
168
     * HTTP 200: Ok
169
     *
170
     * @param string $path Path
171
     * @param mixed $data Data
172
     * @param array $options Options
173
     *
174
     * @return array Response
175
     */
176
    public function update($path, $data, $options = array())
177
    {
178
        return $this->_writeData($path, $data, 'PATCH', $options);
179
    }
180
181
    /**
182
     * Reading data from Firebase
183
     * HTTP 200: Ok
184
     *
185
     * @param string $path Path
186
     * @param array $options Options
187
     *
188
     * @return array Response
189
     */
190
    public function get($path, $options = array())
191
    {
192
        try {
193
            $ch = $this->_getCurlHandler($path, 'GET', $options);
194
            $return = curl_exec($ch);
195
        } catch (Exception $e) {
196
            $return = null;
197
        }
198
        return $return;
199
    }
200
201
    /**
202
     * Deletes data from Firebase
203
     * HTTP 204: Ok
204
     *
205
     * @param string $path Path
206
     * @param array $options Options
207
     *
208
     * @return array Response
209
     */
210
    public function delete($path, $options = array())
211
    {
212
        try {
213
            $ch = $this->_getCurlHandler($path, 'DELETE', $options);
214
            $return = curl_exec($ch);
215
        } catch (Exception $e) {
216
            $return = null;
217
        }
218
        return $return;
219
    }
220
221
    /**
222
     * Returns with Initialized CURL Handler
223
     *
224
     * @param string $path Path
225
     * @param string $mode Mode
226
     * @param array $options Options
227
     *
228
     * @return resource Curl Handler
229
     */
230
    private function _getCurlHandler($path, $mode, $options = array())
231
    {
232
        $url = $this->_getJsonPath($path, $options);
233
        $ch = $this->_curlHandler;
234
        curl_setopt($ch, CURLOPT_URL, $url);
235
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout);
236
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_timeout);
237
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
238
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
239
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
240
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
241
        return $ch;
242
    }
243
244
    private function _writeData($path, $data, $method = 'PUT', $options = array())
245
    {
246
        $jsonData = json_encode($data);
247
        $header = array(
248
            'Content-Type: application/json',
249
            'Content-Length: ' . strlen($jsonData)
250
        );
251
        try {
252
            $ch = $this->_getCurlHandler($path, $method, $options);
253
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
254
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
255
            $return = curl_exec($ch);
256
        } catch (Exception $e) {
257
            $return = null;
258
        }
259
        return $return;
260
    }
261
262
}
263