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 ( 4fce89...e65c1f )
by Jared
03:17
created

JCFirebase::getPathURI()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 37
rs 8.439
cc 6
eloc 19
nc 7
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 JCFirebase;
10
11
use Requests;
12
13
/**
14
 * Class JCFirebase
15
 * @package JCFirebase
16
 * reference https://www.firebase.com/docs/rest/api/
17
 */
18
class JCFirebase
19
{
20
    public $firebaseURI;
21
22
    public $rootPath;
23
24
    public $requestHeader = array(
25
        'accept' => 'application/json',
26
        'contentType' => 'application/json; charset=utf-8',
27
        'dataType' => 'json'
28
    );
29
30
    public $requestOptions = array();
31
32
    /**
33
     * @var OAuth
34
     */
35
    public $auth;
36
37
38
    /**
39
     * JCFirebase constructor.
40
     *
41
     * @param $firebaseURI
42
     * @param array $firebaseAuth
0 ignored issues
show
Bug introduced by
There is no parameter named $firebaseAuth. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
     * @param string $rootPath
44
     */
45
    public function __construct($firebaseURI, OAuth $auth, $rootPath = '/')
46
    {
47
        $this->firebaseURI = $firebaseURI;
48
        $this->firebaseDefaultPath = $rootPath;
0 ignored issues
show
Bug introduced by
The property firebaseDefaultPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        $this->auth = $auth;
50
    }
51
52
53
    /**
54
     * @param $firebaseURI
55
     * @param $jsonString
56
     * @param string $rootPath
57
     * @return JCFirebase
58
     * @throws \Exception
59
     */
60
    public static function fromJson($firebaseURI, $jsonString, $rootPath = '/')
61
    {
62
        if ($jsonString) {
63
            $serviceAccount = $jsonString->client_email;
64
            $privateKey = $jsonString->private_key;
65
66
            return new self($firebaseURI, new OAuth($privateKey, $serviceAccount), $rootPath);
67
        } else {
68
            throw new \Exception("can't get data from key file");
69
        }
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
        $jsonString = null;
83
        try {
84
            $jsonString = json_decode(file_get_contents($keyFile));
85
        } catch (\Exception $exception) {
86
            $jsonString = json_decode(Requests::get($keyFile));
87
        }
88
89
        return self::fromJson($firebaseURI, $jsonString, $rootPath);
90
    }
91
92
    public function getPathURI($path = '', $print = '')
93
    {
94
        //remove last slash from firebaseURI
95
        $template = '/';
96
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
97
        $path = rtrim($path, $template);
98
        $path = ltrim($path, $template);
99
100
        //check https
101
        if (strpos($this->firebaseURI, 'http://') !== false) {
102
            throw new \Exception("https is required.");
103
        }
104
105
        //check firebaseURI
106
        if (empty($this->firebaseURI)) {
107
            throw new \Exception("firebase URI is required");
108
        }
109
110
        if (strpos($this->firebaseDefaultPath, "/") !== 0) {
111
            throw new \Exception("firebase default path must contain /");
112
        }
113
114
        $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json";
115
116
        //set query data
117
        $queryData = array();
118
        if (!empty($print)) {
119
            $queryData[Option::OPTION_PRINT] = $print;
120
        }
121
        if (!empty($queryData)) {
122
            $pathURI = $pathURI . '?' . http_build_query($queryData);
123
        }
124
125
        $this->refreshToken();
126
127
        return $pathURI;
128
    }
129
130
    public function getShallow($path = '', $options = array())
131
    {
132
        return Requests::get(
133
            $this->getPathURI($path) . '?' . http_build_query(array(
134
                Option::OPTION_SHALLOW => Option::SHALLOW_TRUE
135
            )),
136
            $this->requestHeader,
137
            $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, Requests::get() does only seem to accept array, 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...
138
        );
139
    }
140
141
    /**
142
     * @param string $path
143
     * @param array $options
144
     *
145
     * @return \Requests_Response
146
     */
147
    public function get($path = '', $options = array())
148
    {
149
        return Requests::get(
150
            $this->addDataToPathURI($path, $options), $this->requestHeader,
151
            $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, Requests::get() does only seem to accept array, 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...
152
        );
153
    }
154
155
    /**
156
     * @param string $path
157
     * @param array $options
158
     *
159
     * @return \Requests_Response
160
     */
161
    public function put($path = '', $options = array())
162
    {
163
        return Requests::put($this->getPathURI($path), $this->requestHeader,
164
            $this->addDataToRequest($options, true));
0 ignored issues
show
Bug introduced by
It seems like $this->addDataToRequest($options, true) targeting JCFirebase\JCFirebase::addDataToRequest() can also be of type string; however, Requests::put() does only seem to accept array, 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...
165
    }
166
167
    /**
168
     * @param string $path
169
     * @param array $options
170
     *
171
     * @return \Requests_Response
172
     */
173
    public function post($path = '', $options = array())
174
    {
175
        return Requests::post($this->getPathURI($path), $this->requestHeader,
176
            $this->addDataToRequest($options, true));
0 ignored issues
show
Bug introduced by
It seems like $this->addDataToRequest($options, true) targeting JCFirebase\JCFirebase::addDataToRequest() can also be of type string; however, Requests::post() does only seem to accept array, 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...
177
    }
178
179
    /**
180
     * @param string $path
181
     * @param array $options
182
     *
183
     * @return \Requests_Response
184
     */
185
    public function patch($path = '', $options = array())
186
    {
187
        return Requests::patch($this->getPathURI($path), $this->requestHeader,
188
            $this->addDataToRequest($options, true));
0 ignored issues
show
Bug introduced by
It seems like $this->addDataToRequest($options, true) targeting JCFirebase\JCFirebase::addDataToRequest() can also be of type string; however, Requests::patch() does only seem to accept array, 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...
189
    }
190
191
    /**
192
     * @param string $path
193
     * @param array $options
194
     *
195
     * @return \Requests_Response
196
     */
197
    public function delete($path = '', $options = array())
198
    {
199
        return Requests::delete($this->getPathURI($path), $this->requestHeader,
200
            $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, Requests::delete() does only seem to accept array, 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...
201
    }
202
203
    /**
204
     * Function that check firebase authencation
205
     * and configuration valid or not
206
     *
207
     * @return bool
208
     */
209
    public function isValid()
210
    {
211
        return $this->get(null, array(
212
                Option::OPTION_PRINT => Option::PRINT_SILENT
213
            ))->status_code == 204;
214
    }
215
216
    protected function refreshToken()
217
    {
218
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
219
    }
220
221
    protected function addDataToPathURI($path = '', $options = array(), $reqType = Option::REQ_TYPE_GET)
222
    {
223
        $print = '';
224
        if (isset($options['print'])) {
225
            if (Option::isAllowPrint($reqType, $options['print'])) {
226
                $print = $options['print'];
227
            }
228
        }
229
230
        return $this->getPathURI($path, $print);
231
    }
232
233
    protected function addDataToRequest($options = array(), $jsonEncode = false)
234
    {
235
        $requestOptions = array();
236
237
        if (isset($options['data'])) {
238
            $requestOptions = array_merge($options['data'], $requestOptions);
239
        }
240
241
        if ($jsonEncode) {
242
            $requestOptions = json_encode($requestOptions);
243
        }
244
245
        return $requestOptions;
246
    }
247
}