Completed
Push — master ( c4b5d1...80ccc4 )
by Tamas
19s queued 10s
created

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