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 ( 4b7930...e406d9 )
by François
02:07
created

AccessToken::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AccessToken::getToken() 0 4 1
1
<?php
2
/**
3
 *  Copyright (C) 2017 François Kooman <[email protected]>.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace fkooman\OAuth\Client;
20
21
use DateTime;
22
23
/**
24
 * AccessToken object containing the response from the OAuth 2.0 provider's
25
 * token response.
26
 */
27
class AccessToken
28
{
29
    /** @var string */
30
    private $accessToken;
31
32
    /** @var string */
33
    private $tokenType;
34
35
    /** @var string */
36
    private $scope;
37
38
    /** @var string|null */
39
    private $refreshToken;
40
41
    /** @var \DateTime */
42
    private $expiresAt;
43
44
    public function __construct($accessToken, $tokenType, $scope, $refreshToken, DateTime $expiresAt)
45
    {
46
        $this->accessToken = $accessToken;
47
        $this->tokenType = $tokenType;
48
        $this->scope = $scope;
49
        $this->refreshToken = $refreshToken;
50
        $this->expiresAt = $expiresAt;
51
    }
52
53
    /**
54
     * Get the access token.
55
     *
56
     * @return string the access token
57
     *
58
     * @see https://tools.ietf.org/html/rfc6749#section-5.1
59
     */
60
    public function getToken()
61
    {
62
        return $this->accessToken;
63
    }
64
65
    /**
66
     * Get the token type.
67
     *
68
     * @return string the token type
69
     *
70
     * @see https://tools.ietf.org/html/rfc6749#section-7.1
71
     */
72
    public function getTokenType()
73
    {
74
        return $this->tokenType;
75
    }
76
77
    /**
78
     * Get the scope.
79
     *
80
     * @return string the scope
81
     *
82
     * @see https://tools.ietf.org/html/rfc6749#section-3.3
83
     */
84
    public function getScope()
85
    {
86
        return $this->scope;
87
    }
88
89
    /**
90
     * Get the refresh token.
91
     *
92
     * @return string|null the refresh token
93
     *
94
     * @see https://tools.ietf.org/html/rfc6749#section-1.5
95
     */
96
    public function getRefreshToken()
97
    {
98
        return $this->refreshToken;
99
    }
100
101
    /**
102
     * @return DateTime
103
     */
104
    public function getExpiresAt()
105
    {
106
        return $this->expiresAt;
107
    }
108
109
    /**
110
     * @param DateTime|null $dateTime
111
     *
112
     * @return bool
113
     */
114
    public function isExpired(DateTime $dateTime = null)
115
    {
116
        if (is_null($dateTime)) {
117
            $dateTime = new DateTime();
118
        }
119
120
        return $dateTime >= $this->expiresAt;
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function json()
127
    {
128
        return json_encode(
129
            [
130
                'access_token' => $this->accessToken,
131
                'token_type' => $this->tokenType,
132
                'scope' => $this->scope,
133
                'refresh_token' => $this->refreshToken,
134
                'expires_at' => $this->expiresAt->format('Y-m-d H:i:s'),
135
            ]
136
        );
137
    }
138
139
    /**
140
     * @param string $jsonData
141
     *
142
     * @return AccessToken
143
     */
144
    public static function fromJson($jsonData)
145
    {
146
        // XXX verify json decoding, verify all keys
147
        $tokenData = json_decode($jsonData, true);
148
149
        return new self(
150
            $tokenData['access_token'],
151
            $tokenData['token_type'],
152
            $tokenData['scope'],
153
            $tokenData['refresh_token'],
154
            new DateTime($tokenData['expires_at'])
155
        );
156
    }
157
}
158