GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c428a4...8cb3bd )
by Jared
12:57
created

JCFirebase::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 11/29/16
6
 * Time: 3:47 PM
7
 */
8
9
namespace JC\Firebase;
10
11
use JC\JCRequest;
12
use JC\JCResponseInterface;
13
use JC\Firebase\Enums\PrintType;
14
use JC\Firebase\Enums\RequestType;
15
use JC\Firebase\OAuth;
16
17
/**
18
 * Class JCFirebase
19
 * @package JCFirebase
20
 * reference https://www.firebase.com/docs/rest/api/
21
 */
22
class JCFirebase
23
{
24
    public $firebaseURI;
25
26
    public $rootPath;
27
28
    /**
29
     * @var array
30
     */
31
    public $requestHeader = array(
32
        'accept' => 'application/json',
33
        'contentType' => 'application/json; charset=utf-8',
34
        'dataType' => 'json'
35
    );
36
37
    public $requestOptions = array();
38
39
    /**
40
     * @var OAuth
41
     */
42
    public $auth;
43
44
    /**
45
     * JCFirebase constructor.
46
     *
47
     * @param $firebaseURI
48
     * @param OAuth $auth
49
     * @param string $rootPath
50
     */
51
    public function __construct($firebaseURI, OAuth $auth, $rootPath = '/')
52
    {
53
        $this->firebaseURI = $firebaseURI;
54
        $this->rootPath = $rootPath;
55
        $this->auth = $auth;
56
    }
57
58
59
    /**
60
     * @param $firebaseURI
61
     * @param $jsonString
62
     * @param string $rootPath
63
     * @return JCFirebase
64
     * @throws \Exception
65
     */
66
    public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
67
    {
68
        return new self($firebaseURI, OAuth::fromJson($jsonString), $rootPath);
69
    }
70
71
    /**
72
     * @param $firebaseURI
73
     * @param $keyFile
74
     * @param string $rootPath
75
     *
76
     * @return JCFirebase
77
     * @throws \Exception
78
     */
79
    public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/')
80
    {
81
        return new self($firebaseURI, OAuth::fromKeyFile($keyFile), $rootPath);
82
    }
83
84
    public function getPathURI($path = '', $print = '')
85
    {
86
        //remove last slash from firebaseURI
87
        $template = '/';
88
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
89
        $path = rtrim($path, $template);
90
        $path = ltrim($path, $template);
91
92
        //check https
93
        if (strpos($this->firebaseURI, 'http://') !== false) {
94
            throw new \Exception("https is required.");
95
        }
96
97
        //check firebaseURI
98
        if (empty($this->firebaseURI)) {
99
            throw new \Exception("firebase URI is required");
100
        }
101
102
        if (strpos($this->rootPath, "/") !== 0) {
103
            throw new \Exception("firebase default path must contain /");
104
        }
105
106
        $pathURI = $this->firebaseURI . $this->rootPath . $path . ".json";
107
108
        //set query data
109
        $queryData = array();
110
        if (!empty($print)) {
111
            $queryData[Option::OPT_PRINT] = $print;
112
        }
113
        if (!empty($queryData)) {
114
            $pathURI = $pathURI . '?' . http_build_query($queryData);
115
        }
116
117
        $this->refreshToken();
118
119
        return $pathURI;
120
    }
121
122
    public function getShallow($path = '', $options = array())
123
    {
124
        return JCRequest::get(
125
            $this->getPathURI($path) . '?' . http_build_query(array(
126
                Option::OPT_SHALLOW => 'true'
127
            )),
128
            $this->addDataToRequest($options),
129
            $this->requestHeader
130
        );
131
    }
132
133
    /**
134
     * @param string $path
135
     * @param array $options
136
     *
137
     * @return JCResponseInterface
138
     */
139
    public function get($path = '', $options = array())
140
    {
141
        return JCRequest::get(
142
            $this->addDataToPathURI($path, $options),
143
            $this->addDataToRequest($options),
144
            $this->requestHeader
145
        );
146
    }
147
148
    /**
149
     * @param string $path
150
     * @param array $options
151
     *
152
     * @return JCResponseInterface
153
     */
154
    public function put($path = '', $options = array())
155
    {
156
        return JCRequest::put($this->getPathURI($path),
157
            $this->addDataToRequest($options, true),
158
            $this->requestHeader
159
        );
160
    }
161
162
    /**
163
     * @param string $path
164
     * @param array $options
165
     *
166
     * @return JCResponseInterface
167
     */
168
    public function post($path = '', $options = array())
169
    {
170
        return JCRequest::post(
171
            $this->getPathURI($path),
172
            $this->addDataToRequest($options, true),
173
            $this->requestHeader
174
        );
175
    }
176
177
    /**
178
     * @param string $path
179
     * @param array $options
180
     *
181
     * @return JCResponseInterface
182
     */
183
    public function patch($path = '', $options = array())
184
    {
185
        return JCRequest::patch(
186
            $this->getPathURI($path),
187
            $this->addDataToRequest($options, true),
188
            $this->requestHeader
189
        );
190
    }
191
192
    /**
193
     * @param string $path
194
     * @param array $options
195
     *
196
     * @return JCResponseInterface
197
     */
198
    public function delete($path = '', $options = array())
199
    {
200
        return JCRequest::delete(
201
            $this->getPathURI($path),
202
            $this->addDataToRequest($options),
203
            $this->requestHeader
204
        );
205
    }
206
207
    /**
208
     * Function that check firebase authencation
209
     * and configuration valid or not
210
     *
211
     * @return bool
212
     */
213
    public function isValid()
214
    {
215
        return $this->get(null, array(
216
                Option::OPT_PRINT => PrintType::SILENT
217
            ))->status() == 204;
218
    }
219
220
    protected function refreshToken()
221
    {
222
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
223
    }
224
225
    protected function addDataToPathURI($path = '', $options = array(), $reqType = RequestType::GET)
226
    {
227
        $print = '';
228
        if (isset($options[Option::OPT_PRINT]) && Option::isAllowPrint($reqType, $options['print'])) {
229
            $print = $options[Option::OPT_PRINT];
230
        }
231
232
        return $this->getPathURI($path, $print);
233
    }
234
235
    protected function addDataToRequest($options = array(), $jsonEncode = false)
236
    {
237
        $requestData = array();
238
239
        if (isset($options['data'])) {
240
            $requestData = array_merge($options['data'], $requestData);
241
        }
242
243
        if ($jsonEncode) {
244
            $requestData = json_encode($requestData);
245
        }
246
247
        return $requestData;
248
    }
249
}