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 ( 012605...863ffb )
by Jared
02:17
created

JCFirebase::addDataToRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 4
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
    public $firebaseDefaultPath;
22
    public $requestHeader = array(
23
        'accept' => 'application/json',
24
        'contentType' => 'application/json; charset=utf-8',
25
        'dataType' => 'json'
26
    );
27
    public $requestOptions = array();
28
    /**
29
     * @var OAuth
30
     */
31
    protected $auth;
32
33
    public function __construct($firebaseURI, $firebaseSerivceAccount = '', $firebaseDefaultPath = '/')
34
    {
35
        $this->firebaseURI = $firebaseURI;
36
        $this->firebaseDefaultPath = $firebaseDefaultPath;
37
        $this->setAuth($firebaseSerivceAccount);
38
    }
39
40
    public function setAuth($firebaseServiceAccount)
41
    {
42
        if (isset($firebaseServiceAccount['key']) && isset($firebaseServiceAccount['iss'])) {
43
            $this->auth = new OAuth($firebaseServiceAccount['key'], $firebaseServiceAccount['iss']);
44
        }
45
    }
46
47
    public function getPathURI($path = '', $print = '')
48
    {
49
        //remove last slash from firebaseURI
50
        $template = '/';
51
        $this->firebaseURI = rtrim($this->firebaseURI, $template);
52
        $path = rtrim($path, $template);
53
        $path = ltrim($path, $template);
54
55
        //check https
56
        if (strpos($this->firebaseURI, 'http://') !== false) {
57
            throw new \Exception("https is required.");
58
        }
59
60
        //check firebaseURI
61
        if (empty($this->firebaseURI)) {
62
            throw new \Exception("firebase URI is required");
63
        }
64
65
        if (strpos($this->firebaseDefaultPath, "/") !== 0) {
66
            throw new \Exception("firebase default path must contain /");
67
        }
68
69
        $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json";
70
71
        //set query data
72
        $queryData = array();
73
        if (!empty($print)) {
74
            $queryData[JCFirebaseOption::OPTION_PRINT] = $print;
75
        }
76
        if (!empty($queryData)) {
77
            $pathURI = $pathURI . '?' . http_build_query($queryData);
78
        }
79
80
        $this->refreshToken();
81
82
        return $pathURI;
83
    }
84
85
    public function getShallow($path = '', $options = array())
86
    {
87
        return Requests::get(
88
            $this->getPathURI($path) . '?' . http_build_query(array(
89
                JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE
90
            )),
91
            $this->requestHeader,
92
            $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...
93
        );
94
    }
95
96
    /**
97
     * @param string $path
98
     * @param array $options
99
     * @return \Requests_Response
100
     */
101
    public function get($path = '', $options = array())
102
    {
103
        return Requests::get(
104
            $this->addDataToPathURI($path, $options), $this->requestHeader,
105
            $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...
106
        );
107
    }
108
109
    /**
110
     * @param string $path
111
     * @param array $options
112
     * @return \Requests_Response
113
     */
114
    public function put($path = '', $options = array())
115
    {
116
        return Requests::put($this->getPathURI($path), $this->requestHeader,
117
            $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...
118
    }
119
120
    /**
121
     * @param string $path
122
     * @param array $options
123
     * @return \Requests_Response
124
     */
125
    public function post($path = '', $options = array())
126
    {
127
        return Requests::post($this->getPathURI($path), $this->requestHeader,
128
            $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...
129
    }
130
131
    /**
132
     * @param string $path
133
     * @param array $options
134
     * @return \Requests_Response
135
     */
136
    public function patch($path = '', $options = array())
137
    {
138
        return Requests::patch($this->getPathURI($path), $this->requestHeader,
139
            $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...
140
    }
141
142
    /**
143
     * @param string $path
144
     * @param array $options
145
     * @return \Requests_Response
146
     */
147
    public function delete($path = '', $options = array())
148
    {
149
        return Requests::delete($this->getPathURI($path), $this->requestHeader,
150
            $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...
151
    }
152
153
    protected function refreshToken()
154
    {
155
        $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken();
156
    }
157
158
    protected function addDataToPathURI($path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET)
159
    {
160
        $print = '';
161
        if (isset($options['print'])) {
162
            if (JCFirebaseOption::isAllowPrint($reqType, $options['print'])) {
163
                $print = $options['print'];
164
            }
165
        }
166
        return $this->getPathURI($path, $print);
167
    }
168
169
    protected function addDataToRequest($options = array(), $jsonEncode = false)
170
    {
171
        $requestOptions = array();
172
173
        if (isset($options['data'])) {
174
            $requestOptions = array_merge($options['data'], $requestOptions);
175
        }
176
177
        if ($jsonEncode) {
178
            $requestOptions = json_encode($requestOptions);
179
        }
180
181
        return $requestOptions;
182
    }
183
}