Completed
Push — master ( 428e18...2b1c60 )
by Tamas
01:20
created

FirebaseLib::setToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
     * Deletes data from Firebase
215
     * HTTP 204: Ok
216
     *
217
     * @param string $path Path
218
     * @param array $options Options
219
     *
220
     * @return array Response
221
     */
222
    public function delete($path, array $options = [])
223
    {
224
        try {
225
            $ch = $this->getCurlHandler($path, 'DELETE', $options);
226
            $return = curl_exec($ch);
227
        } catch (Exception $e) {
228
            $return = null;
229
        }
230
        return $return;
231
    }
232
233
    /**
234
     * Returns with Initialized CURL Handler
235
     *
236
     * @param string $path Path
237
     * @param string $mode Mode
238
     * @param array $options Options
239
     *
240
     * @return resource Curl Handler
241
     */
242
    private function getCurlHandler($path, $mode, $options = array())
243
    {
244
        $url = $this->getJsonPath($path, $options);
245
        $ch = $this->curlHandler;
246
        curl_setopt($ch, CURLOPT_URL, $url);
247
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
248
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
249
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getSSLConnection());
250
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
251
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode);
252
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
253
        return $ch;
254
    }
255
256
    private function writeData($path, $data, $method = 'PUT', $options = array())
257
    {
258
        $jsonData = json_encode($data);
259
        $header = array(
260
            'Content-Type: application/json',
261
            'Content-Length: ' . strlen($jsonData)
262
        );
263
        try {
264
            $ch = $this->getCurlHandler($path, $method, $options);
265
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
266
            curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
267
            $return = curl_exec($ch);
268
        } catch (Exception $e) {
269
            $return = null;
270
        }
271
        return $return;
272
    }
273
}
274