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 ( 1bd209...b712a4 )
by Jared
04:45
created

JCFirebase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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