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.

JCFirebase::isValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 0
crap 1
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\Firebase\Enums\PrintType;
12
use JC\Firebase\Enums\RequestType;
13
use JC\HttpClient\JCResponseInterface;
14
15
/**
16
 * Class JCFirebase
17
 * @package JCFirebase
18
 * reference https://www.firebase.com/docs/rest/api/
19
 */
20
class JCFirebase
21
{
22
    public $firebaseURI;
23
24
    public $rootPath;
25
26
    /**
27
     * @var array
28
     */
29
    public $requestHeader = array(
30
        'accept' => 'application/json',
31
        'contentType' => 'application/json; charset=utf-8',
32
        'dataType' => 'json'
33
    );
34
35
    public $requestOptions = array();
36
37
    /**
38
     * @var OAuth
39
     */
40
    public $auth;
41
42
    public $client;
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
        $this->client = Client::getClient();
57
    }
58
59
60
    /**
61
     * @param $firebaseURI
62
     * @param $jsonString
63
     * @param string $rootPath
64
     * @return JCFirebase
65
     * @throws \Exception
66
     */
67
    public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
68
    {
69
        return new self($firebaseURI, OAuth::fromJson($jsonString), $rootPath);
70
    }
71
72
    /**
73
     * @param $firebaseURI
74
     * @param $keyFile
75
     * @param string $rootPath
76
     *
77
     * @return JCFirebase
78
     * @throws \Exception
79
     */
80
    public static function fromKeyFile($firebaseURI, $keyFile, $rootPath = '/')
81
    {
82
        return new self($firebaseURI, OAuth::fromKeyFile($keyFile), $rootPath);
83
    }
84 14
85
    public function getPathURI($path = '', $print = '')
86
    {
87 14
        //remove last slash from firebaseURI
88 14
        $template = '/';
89 14
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
90 14
        $path = rtrim($path, $template);
91
        $path = ltrim($path, $template);
92
93 14
        //check https
94
        if (strpos($this->firebaseURI, 'http://') !== false) {
95
            throw new \Exception("https is required.");
96
        }
97
98 14
        //check firebaseURI
99
        if (empty($this->firebaseURI)) {
100
            throw new \Exception("firebase URI is required");
101
        }
102 14
103
        if (strpos($this->rootPath, "/") !== 0) {
104
            throw new \Exception("firebase default path must contain /");
105
        }
106 14
107
        $pathURI = $this->firebaseURI . $this->rootPath . $path . ".json";
108
109 14
        //set query data
110 14
        $queryData = array();
111 1
        if (!empty($print)) {
112 1
            $queryData[Option::OPT_PRINT] = $print;
113 14
        }
114 1
        if (!empty($queryData)) {
115 1
            $pathURI = $pathURI . '?' . http_build_query($queryData);
116
        }
117 14
118
        $this->refreshToken();
119 14
120
        return $pathURI;
121
    }
122 1
123
    public function getShallow($path = '', $options = array())
124 1
    {
125 1
        return $this->client->get(
126 1
            $this->getPathURI($path) . '?' . http_build_query(array(
127 1
                Option::OPT_SHALLOW => 'true'
128 1
            )),
129 1
            $this->addDataToRequest($options),
130 1
            $this->requestHeader
131
        );
132
    }
133
134
    /**
135
     * @param string $path
136
     * @param array $options
137
     *
138
     * @return JCResponseInterface
139 8
     */
140
    public function get($path = '', $options = array())
141 8
    {
142 8
        return $this->client->get(
143 8
            $this->addDataToPathURI($path, $options),
144 8
            $this->addDataToRequest($options),
145 8
            $this->requestHeader
146
        );
147
    }
148
149
    /**
150
     * @param string $path
151
     * @param array $options
152
     *
153
     * @return JCResponseInterface
154 7
     */
155 View Code Duplication
    public function put($path = '', $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156 7
    {
157 7
        return $this->client->put($this->getPathURI($path),
158 7
            $this->addDataToRequest($options, true),
159 7
            $this->requestHeader
160
        );
161
    }
162
163
    /**
164
     * @param string $path
165
     * @param array $options
166
     *
167
     * @return JCResponseInterface
168 5
     */
169 View Code Duplication
    public function post($path = '', $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170 5
    {
171 5
        return $this->client->post(
172 5
            $this->getPathURI($path),
173 5
            $this->addDataToRequest($options, true),
174 5
            $this->requestHeader
175
        );
176
    }
177
178
    /**
179
     * @param string $path
180
     * @param array $options
181
     *
182
     * @return JCResponseInterface
183 1
     */
184 View Code Duplication
    public function patch($path = '', $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185 1
    {
186 1
        return $this->client->patch(
187 1
            $this->getPathURI($path),
188 1
            $this->addDataToRequest($options, true),
189 1
            $this->requestHeader
190
        );
191
    }
192
193
    /**
194
     * @param string $path
195
     * @param array $options
196
     *
197
     * @return JCResponseInterface
198 2
     */
199
    public function delete($path = '', $options = array())
200 2
    {
201 2
        return $this->client->delete(
202 2
            $this->getPathURI($path),
203 2
            $this->addDataToRequest($options),
204 2
            $this->requestHeader
205
        );
206
    }
207
208
    /**
209
     * Function that check firebase authencation
210
     * and configuration valid or not
211
     *
212
     * @return bool
213 1
     */
214
    public function isValid()
215 1
    {
216 1
        return $this->get(null, array(
217 1
                Option::OPT_PRINT => PrintType::SILENT
218
            ))->status() == 204;
219
    }
220 14
221
    protected function refreshToken()
222 14
    {
223 14
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
224
    }
225 8
226
    protected function addDataToPathURI($path = '', $options = array(), $reqType = RequestType::GET)
227 8
    {
228 8
        $print = '';
229 1
        if (isset($options[Option::OPT_PRINT]) && Option::isAllowPrint($reqType, $options['print'])) {
230 1
            $print = $options[Option::OPT_PRINT];
231
        }
232 8
233
        return $this->getPathURI($path, $print);
234
    }
235 13
236
    protected function addDataToRequest($options = array(), $jsonEncode = false)
237 13
    {
238
        $requestData = array();
239 13
240 11
        if (isset($options['data'])) {
241 11
            $requestData = array_merge($options['data'], $requestData);
242
        }
243 13
244 11
        if ($jsonEncode) {
245 11
            $requestData = json_encode($requestData);
246
        }
247 13
248
        return $requestData;
249
    }
250
}