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 ( 04b66e...f5e077 )
by Jared
04:12
created

JCFirebase   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 79.8%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 25
c 6
b 0
f 0
lcom 1
cbo 4
dl 0
loc 242
ccs 79
cts 99
cp 0.798
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromJson() 0 11 2
A fromKeyFile() 0 11 2
B getPathURI() 0 37 6
A getShallow() 0 10 1
A get() 0 8 1
A put() 0 7 1
A post() 0 8 1
A patch() 0 8 1
A delete() 0 8 1
A isValid() 0 6 1
A refreshToken() 0 4 1
A addDataToPathURI() 0 11 3
A addDataToRequest() 0 14 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 11/29/16
6
 * Time: 3:47 PM
7
 */
8
9
namespace JCFirebase;
10
11
use JC\JCRequest;
12
use JC\JCResponse;
13
use JCFirebase\Enums\RequestType;
14
use JCFirebase\Enums\PrintType;
15
16
/**
17
 * Class JCFirebase
18
 * @package JCFirebase
19
 * reference https://www.firebase.com/docs/rest/api/
20
 */
21
class JCFirebase
22
{
23
    public $firebaseURI;
24
25
    public $rootPath;
26
27
    public $requestHeader = array(
28
        'accept' => 'application/json',
29
        'contentType' => 'application/json; charset=utf-8',
30
        'dataType' => 'json'
31
    );
32
33
    public $requestOptions = array();
34
35
    /**
36
     * @var OAuth
37
     */
38
    public $auth;
39
40
41
    /**
42
     * JCFirebase constructor.
43
     *
44
     * @param $firebaseURI
45
     * @param OAuth $auth
46
     * @param string $rootPath
47
     */
48
    public function __construct($firebaseURI, OAuth $auth, $rootPath = '/')
49
    {
50
        $this->firebaseURI = $firebaseURI;
51
        $this->rootPath = $rootPath;
52
        $this->auth = $auth;
53
    }
54
55
56
    /**
57
     * @param $firebaseURI
58
     * @param $jsonString
59
     * @param string $rootPath
60
     * @return JCFirebase
61
     * @throws \Exception
62
     */
63
    public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
64
    {
65
        if ($jsonString) {
66
            $serviceAccount = $jsonString->client_email;
67
            $privateKey = $jsonString->private_key;
68
69
            return new self($firebaseURI, new OAuth($privateKey, $serviceAccount), $rootPath);
70
        } else {
71
            throw new \Exception("can't get data from key file");
72
        }
73
    }
74
75
    /**
76
     * @param $firebaseURI
77
     * @param $keyFile
78
     * @param string $rootPath
79
     *
80
     * @return JCFirebase
81
     * @throws \Exception
82
     */
83
    public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/')
84
    {
85
        $jsonString = null;
86
        try {
87
            $jsonString = json_decode(file_get_contents($keyFile));
88
        } catch (\Exception $exception) {
89
            $jsonString = json_decode(JCRequest::get($keyFile));
90
        }
91
92
        return self::fromJson($firebaseURI, $jsonString, $rootPath);
93
    }
94
95 13
    public function getPathURI($path = '', $print = '')
96
    {
97
        //remove last slash from firebaseURI
98 13
        $template = '/';
99 13
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
100 13
        $path = rtrim($path, $template);
101 13
        $path = ltrim($path, $template);
102
103
        //check https
104 13
        if (strpos($this->firebaseURI, 'http://') !== false) {
105
            throw new \Exception("https is required.");
106
        }
107
108
        //check firebaseURI
109 13
        if (empty($this->firebaseURI)) {
110
            throw new \Exception("firebase URI is required");
111
        }
112
113 13
        if (strpos($this->rootPath, "/") !== 0) {
114
            throw new \Exception("firebase default path must contain /");
115
        }
116
117 13
        $pathURI = $this->firebaseURI . $this->rootPath . $path . ".json";
118
119
        //set query data
120 13
        $queryData = array();
121 13
        if (!empty($print)) {
122 1
            $queryData[Option::_PRINT] = $print;
123 1
        }
124 13
        if (!empty($queryData)) {
125 1
            $pathURI = $pathURI . '?' . http_build_query($queryData);
126 1
        }
127
128 13
        $this->refreshToken();
129
130 13
        return $pathURI;
131
    }
132
133 1
    public function getShallow($path = '', $options = array())
134
    {
135 1
        return JCRequest::get(
136 1
            $this->getPathURI($path) . '?' . http_build_query(array(
137 1
                Option::_SHALLOW => 'true'
138 1
            )),
139 1
            $this->addDataToRequest($options),
0 ignored issues
show
Bug introduced by
It seems like $this->addDataToRequest($options) targeting JCFirebase\JCFirebase::addDataToRequest() can also be of type string; however, JC\JCRequest::get() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
140 1
            $this->requestHeader
141 1
        );
142
    }
143
144
    /**
145
     * @param string $path
146
     * @param array $options
147
     *
148
     * @return JCResponse
149
     */
150 7
    public function get($path = '', $options = array())
151
    {
152 7
        return JCRequest::get(
153 7
            $this->addDataToPathURI($path, $options),
154 7
            $this->addDataToRequest($options),
0 ignored issues
show
Bug introduced by
It seems like $this->addDataToRequest($options) targeting JCFirebase\JCFirebase::addDataToRequest() can also be of type string; however, JC\JCRequest::get() does only seem to accept array|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
155 7
            $this->requestHeader
156 7
        );
157
    }
158
159
    /**
160
     * @param string $path
161
     * @param array $options
162
     *
163
     * @return JCResponse
164
     */
165 7
    public function put($path = '', $options = array())
166
    {
167 7
        return JCRequest::put($this->getPathURI($path),
168 7
            $this->addDataToRequest($options, true),
169 7
            $this->requestHeader
170 7
        );
171
    }
172
173
    /**
174
     * @param string $path
175
     * @param array $options
176
     *
177
     * @return JCResponse
178
     */
179 4
    public function post($path = '', $options = array())
180
    {
181 4
        return JCRequest::post(
182 4
            $this->getPathURI($path),
183 4
            $this->addDataToRequest($options, true),
184 4
            $this->requestHeader
185 4
        );
186
    }
187
188
    /**
189
     * @param string $path
190
     * @param array $options
191
     *
192
     * @return JCResponse
193
     */
194 1
    public function patch($path = '', $options = array())
195
    {
196 1
        return JCRequest::patch(
197 1
            $this->getPathURI($path),
198 1
            $this->addDataToRequest($options, true),
199 1
            $this->requestHeader
200 1
        );
201
    }
202
203
    /**
204
     * @param string $path
205
     * @param array $options
206
     *
207
     * @return JCResponse
208
     */
209 2
    public function delete($path = '', $options = array())
210
    {
211 2
        return JCRequest::delete(
212 2
            $this->getPathURI($path),
213 2
            $this->addDataToRequest($options),
214 2
            $this->requestHeader
215 2
        );
216
    }
217
218
    /**
219
     * Function that check firebase authencation
220
     * and configuration valid or not
221
     *
222
     * @return bool
223
     */
224 1
    public function isValid()
225
    {
226 1
        return $this->get(null, array(
227 1
                Option::_PRINT => PrintType::SILENT
228 1
            ))->status() == 204;
229
    }
230
231 13
    protected function refreshToken()
232
    {
233 13
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
234 13
    }
235
236 7
    protected function addDataToPathURI($path = '', $options = array(), $reqType = RequestType::GET)
237
    {
238 7
        $print = '';
239 7
        if (isset($options['print'])) {
240 1
            if (Option::isAllowPrint($reqType, $options['print'])) {
241 1
                $print = $options['print'];
242 1
            }
243 1
        }
244
245 7
        return $this->getPathURI($path, $print);
246
    }
247
248 12
    protected function addDataToRequest($options = array(), $jsonEncode = false)
249
    {
250 12
        $requestOptions = array();
251
252 12
        if (isset($options['data'])) {
253 10
            $requestOptions = array_merge($options['data'], $requestOptions);
254 10
        }
255
256 12
        if ($jsonEncode) {
257 10
            $requestOptions = json_encode($requestOptions);
258 10
        }
259
260 12
        return $requestOptions;
261
    }
262
}