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 ( 21b00d...73aedc )
by Jared
02:28
created

JCFirebase   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.52%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 23
c 6
b 0
f 0
lcom 1
cbo 4
dl 0
loc 226
ccs 77
cts 89
cp 0.8652
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromJson() 0 4 1
A fromKeyFile() 0 4 1
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 9 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\PrintType;
14
use JCFirebase\Enums\RequestType;
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
        return new self($firebaseURI, OAuth::fromJson($jsonString), $rootPath);
66
    }
67
68
    /**
69
     * @param $firebaseURI
70
     * @param $keyFile
71
     * @param string $rootPath
72
     *
73
     * @return JCFirebase
74
     * @throws \Exception
75
     */
76
    public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/')
77
    {
78
        return new self($firebaseURI, OAuth::fromKeyFile($keyFile), $rootPath);
79
    }
80
81 13
    public function getPathURI($path = '', $print = '')
82
    {
83
        //remove last slash from firebaseURI
84 13
        $template = '/';
85 13
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
86 13
        $path = rtrim($path, $template);
87 13
        $path = ltrim($path, $template);
88
89
        //check https
90 13
        if (strpos($this->firebaseURI, 'http://') !== false) {
91
            throw new \Exception("https is required.");
92
        }
93
94
        //check firebaseURI
95 13
        if (empty($this->firebaseURI)) {
96
            throw new \Exception("firebase URI is required");
97
        }
98
99 13
        if (strpos($this->rootPath, "/") !== 0) {
100
            throw new \Exception("firebase default path must contain /");
101
        }
102
103 13
        $pathURI = $this->firebaseURI . $this->rootPath . $path . ".json";
104
105
        //set query data
106 13
        $queryData = array();
107 13
        if (!empty($print)) {
108 1
            $queryData[Option::OPT_PRINT] = $print;
109 1
        }
110 13
        if (!empty($queryData)) {
111 1
            $pathURI = $pathURI . '?' . http_build_query($queryData);
112 1
        }
113
114 13
        $this->refreshToken();
115
116 13
        return $pathURI;
117
    }
118
119 1
    public function getShallow($path = '', $options = array())
120
    {
121 1
        return JCRequest::get(
122 1
            $this->getPathURI($path) . '?' . http_build_query(array(
123 1
                Option::OPT_SHALLOW => 'true'
124 1
            )),
125 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...
126 1
            $this->requestHeader
127 1
        );
128
    }
129
130
    /**
131
     * @param string $path
132
     * @param array $options
133
     *
134
     * @return JCResponse
135
     */
136 7
    public function get($path = '', $options = array())
137
    {
138 7
        return JCRequest::get(
139 7
            $this->addDataToPathURI($path, $options),
140 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...
141 7
            $this->requestHeader
142 7
        );
143
    }
144
145
    /**
146
     * @param string $path
147
     * @param array $options
148
     *
149
     * @return JCResponse
150
     */
151 7
    public function put($path = '', $options = array())
152
    {
153 7
        return JCRequest::put($this->getPathURI($path),
154 7
            $this->addDataToRequest($options, true),
155 7
            $this->requestHeader
156 7
        );
157
    }
158
159
    /**
160
     * @param string $path
161
     * @param array $options
162
     *
163
     * @return JCResponse
164
     */
165 4
    public function post($path = '', $options = array())
166
    {
167 4
        return JCRequest::post(
168 4
            $this->getPathURI($path),
169 4
            $this->addDataToRequest($options, true),
170 4
            $this->requestHeader
171 4
        );
172
    }
173
174
    /**
175
     * @param string $path
176
     * @param array $options
177
     *
178
     * @return JCResponse
179
     */
180 1
    public function patch($path = '', $options = array())
181
    {
182 1
        return JCRequest::patch(
183 1
            $this->getPathURI($path),
184 1
            $this->addDataToRequest($options, true),
185 1
            $this->requestHeader
186 1
        );
187
    }
188
189
    /**
190
     * @param string $path
191
     * @param array $options
192
     *
193
     * @return JCResponse
194
     */
195 2
    public function delete($path = '', $options = array())
196
    {
197 2
        return JCRequest::delete(
198 2
            $this->getPathURI($path),
199 2
            $this->addDataToRequest($options),
200 2
            $this->requestHeader
201 2
        );
202
    }
203
204
    /**
205
     * Function that check firebase authencation
206
     * and configuration valid or not
207
     *
208
     * @return bool
209
     */
210 1
    public function isValid()
211
    {
212 1
        return $this->get(null, array(
213 1
                Option::OPT_PRINT => PrintType::SILENT
214 1
            ))->status() == 204;
215
    }
216
217 13
    protected function refreshToken()
218
    {
219 13
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
220 13
    }
221
222 7
    protected function addDataToPathURI($path = '', $options = array(), $reqType = RequestType::GET)
223
    {
224 7
        $print = '';
225 7
        if (isset($options[Option::OPT_PRINT]) && Option::isAllowPrint($reqType, $options['print'])) {
226 1
            $print = $options[Option::OPT_PRINT];
227 1
        }
228
229 7
        return $this->getPathURI($path, $print);
230
    }
231
232 12
    protected function addDataToRequest($options = array(), $jsonEncode = false)
233
    {
234 12
        $requestData = array();
235
236 12
        if (isset($options['data'])) {
237 10
            $requestData = array_merge($options['data'], $requestData);
238 10
        }
239
240 12
        if ($jsonEncode) {
241 10
            $requestData = json_encode($requestData);
242 10
        }
243
244 12
        return $requestData;
245
    }
246
}