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

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