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.

OAuth::getAccessToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jaredchu
5
 * Date: 12/2/16
6
 * Time: 4:25 PM
7
 */
8
9
namespace JC\Firebase;
10
11
use Firebase\JWT\JWT;
12
use JC\JCRequest;
13
14
class OAuth
15
{
16
    /*
17
     * token life time in second
18
     * */
19
    public $tokenLifeTime;
20
    public $key;
21
    public $iss;
22
23
    protected $expireTimestamp;
24
    protected $accessToken;
25
26
    /**
27
     * OAuth constructor.
28
     *
29
     * @param $key
30
     * @param $iss
31
     * @param $lifeTime
32
     */
33
    public function __construct($key, $iss, $lifeTime = 3600)
34
    {
35
        $this->key = $key;
36
        $this->iss = $iss;
37
        $this->tokenLifeTime = $lifeTime;
38
    }
39
40
    public static function fromJson($jsonString, $lifeTime = 3600)
41
    {
42
        if ($jsonString) {
43
            $jsonObject = json_decode($jsonString);
44 1
45
            $privateKey = $jsonObject->private_key;
46
            $serviceAccount = $jsonObject->client_email;
47
48
            return new static($privateKey, $serviceAccount, $lifeTime);
49
        } else {
50
            throw new \Exception("can't get data from key file");
51 1
        }
52
    }
53
54
    public static function fromKeyFile($keyFile, $lifeTime = 3600)
55
    {
56
        try {
57
            $jsonString = file_get_contents($keyFile);
58
        } catch (\Exception $exception) {
59
            $jsonString = static::getClient()->get($keyFile)->body();
60
        }
61
62
        return static::fromJson($jsonString, $lifeTime);
63
    }
64
65
    public static function getClient(){
66
        return Client::getClient();
67 2
    }
68
69 2
    protected function requestAccessToken()
70 2
    {
71
        $currentTimestamp = time();
72 2
        $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime;
73 2
        $jsonToken = array(
74 2
            "iss" => $this->iss,
75 2
            "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email",
76
            "aud" => "https://www.googleapis.com/oauth2/v4/token",
77 2
            "exp" => $this->expireTimestamp,
78 2
            "iat" => $currentTimestamp
79
        );
80 2
        $jwt = JWT::encode($jsonToken, $this->key, 'RS256');
81 2
82
        $OAuthResponse = static::getClient()->post('https://www.googleapis.com/oauth2/v4/token', array(
83 2
            'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
84
            'assertion' => $jwt
85 2
        ));
86 2
87
        if ($OAuthResponse->status() == 200) {
88 2
            $this->accessToken = json_decode($OAuthResponse->body())->access_token;
89
90
            return true;
91
        }
92
93
        return false;
94 14
    }
95
96 14
    public function getAccessToken()
97 14
    {
98 2
        $currentTime = time();
99 2
        if ($this->expireTimestamp < $currentTime) {
100 2
            $startTime = time();
101 2
            $this->requestAccessToken();
102 2
            $endTime = time();
103
            $this->expireTimestamp -= ($endTime - $startTime);
104 14
        }
105
106
        return $this->accessToken;
107
    }
108
}