1
|
|
|
<?php |
2
|
|
|
namespace Firebase; |
3
|
|
|
|
4
|
|
|
require_once __DIR__ . '/firebaseInterface.php'; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class FirebaseStub |
8
|
|
|
* |
9
|
|
|
* Stubs the Firebase interface without issuing any cURL requests. |
10
|
|
|
* |
11
|
|
|
* @package Firebase |
12
|
|
|
*/ |
13
|
|
|
class FirebaseStub implements FirebaseInterface |
14
|
|
|
{ |
15
|
|
|
private $response; |
16
|
|
|
public $baseURI; |
17
|
|
|
public $token; |
18
|
|
|
|
19
|
|
|
private $sslConnection; |
20
|
|
|
public $timeout; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $baseURI |
24
|
|
|
* @param string $token |
25
|
|
|
*/ |
26
|
|
|
public function __construct($baseURI = '', $token = '') |
27
|
|
|
{ |
28
|
|
|
if (!extension_loaded('curl')) { |
29
|
|
|
trigger_error('Extension CURL is not loaded.', E_USER_ERROR); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->setBaseURI($baseURI); |
33
|
|
|
$this->setTimeOut(10); |
34
|
|
|
$this->setToken($token); |
35
|
|
|
$this->setSSLConnection(false); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $token |
40
|
|
|
* @return null |
41
|
|
|
*/ |
42
|
|
|
public function setToken($token) |
43
|
|
|
{ |
44
|
|
|
$this->token = $token; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $baseURI |
49
|
|
|
* @return null |
50
|
|
|
*/ |
51
|
|
|
public function setBaseURI($baseURI) |
52
|
|
|
{ |
53
|
|
|
$baseURI .= (substr($baseURI, -1) === '/' ? '' : '/'); |
54
|
|
|
$this->baseURI = $baseURI; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Enabling/Disabling SSL Connection |
59
|
|
|
* |
60
|
|
|
* @param bool $enableSSLConnection |
61
|
|
|
*/ |
62
|
|
|
public function setSSLConnection($enableSSLConnection) { |
63
|
|
|
$this->sslConnection = $enableSSLConnection; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns status of SSL Connection |
68
|
|
|
* |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
|
|
public function getSSLConnection() |
72
|
|
|
{ |
73
|
|
|
return $this->sslConnection; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $seconds |
78
|
|
|
* @return null |
79
|
|
|
*/ |
80
|
|
|
public function setTimeOut($seconds) |
81
|
|
|
{ |
82
|
|
|
$this->timeout = $seconds; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $path |
87
|
|
|
* @param $data |
88
|
|
|
* @param $options |
89
|
|
|
* @return null |
90
|
|
|
*/ |
91
|
|
|
public function set($path, $data, array $options = []) |
92
|
|
|
{ |
93
|
|
|
return $this->getSetResponse($data); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $path |
98
|
|
|
* @param $data |
99
|
|
|
* @param $options |
100
|
|
|
* @return null |
101
|
|
|
*/ |
102
|
|
|
public function push($path, $data, array $options = []) |
103
|
|
|
{ |
104
|
|
|
return $this->set($path, $data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param $path |
109
|
|
|
* @param $data |
110
|
|
|
* @param $options |
111
|
|
|
* @return null |
112
|
|
|
*/ |
113
|
|
|
public function update($path, $data, array $options = []) |
114
|
|
|
{ |
115
|
|
|
return $this->set($path, $data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $path |
120
|
|
|
* @param $options |
121
|
|
|
* @return null |
122
|
|
|
*/ |
123
|
|
|
public function get($path, array $options = []) |
124
|
|
|
{ |
125
|
|
|
return $this->getGetResponse(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $path |
130
|
|
|
* @param $options |
131
|
|
|
* @return null |
132
|
|
|
*/ |
133
|
|
|
public function delete($path, array $options = []) |
134
|
|
|
{ |
135
|
|
|
return $this->getDeleteResponse(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $expectedResponse |
140
|
|
|
*/ |
141
|
|
|
public function setResponse($expectedResponse) |
142
|
|
|
{ |
143
|
|
|
$this->response = $expectedResponse; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @uses $this->baseURI |
148
|
|
|
* @return Error |
149
|
|
|
*/ |
150
|
|
|
private function isBaseURIValid() |
151
|
|
|
{ |
152
|
|
|
$error = preg_match('/^https:\/\//', $this->baseURI); |
153
|
|
|
return new Error($error === 0, 'Firebase does not support non-ssl traffic. Please try your request again over https.'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $data |
158
|
|
|
* @return Error |
159
|
|
|
*/ |
160
|
|
|
private function isDataValid($data) |
161
|
|
|
{ |
162
|
|
|
if ($data === '' || $data === null) { |
163
|
|
|
return new Error(true, 'Missing data; Perhaps you forgot to send the data.'); |
164
|
|
|
} |
165
|
|
|
$error = json_decode($data); |
166
|
|
|
return new Error($error === null, "Invalid data; couldn't parse JSON object, array, or value. Perhaps you're using invalid characters in your key names."); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $data |
171
|
|
|
* @return null |
172
|
|
|
*/ |
173
|
|
|
private function getSetResponse($data) |
174
|
|
|
{ |
175
|
|
|
$validBaseUriObject = $this->isBaseURIValid(); |
176
|
|
|
if ($validBaseUriObject->error) { |
177
|
|
|
return $validBaseUriObject->message; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$validDataObject = $this->isDataValid($data); |
181
|
|
|
if ($validDataObject->error) { |
182
|
|
|
return $validDataObject->message; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->response; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return null |
190
|
|
|
*/ |
191
|
|
|
private function getGetResponse() |
192
|
|
|
{ |
193
|
|
|
$validBaseUriObject = $this->isBaseURIValid(); |
194
|
|
|
if ($validBaseUriObject->error) { |
195
|
|
|
return $validBaseUriObject->message; |
196
|
|
|
} |
197
|
|
|
return $this->response; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return null |
202
|
|
|
*/ |
203
|
|
|
private function getDeleteResponse() |
204
|
|
|
{ |
205
|
|
|
return $this->getGetResponse(); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Class Error |
211
|
|
|
* |
212
|
|
|
* @package Firebase |
213
|
|
|
*/ |
214
|
|
|
class Error |
215
|
|
|
{ |
216
|
|
|
public $error; |
217
|
|
|
public $message; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param $error |
221
|
|
|
* @param $message |
222
|
|
|
*/ |
223
|
|
|
public function __construct($error, $message) |
224
|
|
|
{ |
225
|
|
|
$this->error = $error; |
226
|
|
|
$this->message = $message; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|