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.

PdoTokenStorage::storeAccessToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 1
cc 1
eloc 12
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2016, 2017 François Kooman <[email protected]>.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
25
namespace fkooman\OAuth\Client;
26
27
use PDO;
28
29
class PdoTokenStorage implements TokenStorageInterface
30
{
31
    /** @var \PDO */
32
    private $db;
33
34
    public function __construct(PDO $db)
35
    {
36
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
37
        if ('sqlite' === $db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
38
            $db->query('PRAGMA foreign_keys = ON');
39
        }
40
41
        $this->db = $db;
42
    }
43
44
    public function init()
45
    {
46
        $this->db->query(
47
            'CREATE TABLE IF NOT EXISTS access_tokens (
48
                user_id TEXT NOT NULL,
49
                provider_id TEXT NOT NULL,
50
                issued_at DATETIME NOT NULL,
51
                access_token TEXT NOT NULL,
52
                token_type TEXT NOT NULL,
53
                expires_in INT,
54
                refresh_token TEXT,
55
                scope TEXT NOT NULL
56
            )'
57
        );
58
    }
59
60
    /**
61
     * @param string $userId
62
     *
63
     * @return array
64
     */
65
    public function getAccessTokenList($userId)
66
    {
67
        $stmt = $this->db->prepare(
68
            'SELECT
69
                provider_id, issued_at, access_token, token_type, expires_in, refresh_token, scope
70
             FROM access_tokens
71
             WHERE
72
                user_id = :user_id'
73
        );
74
75
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
76
        $stmt->execute();
77
78
        $accessTokenList = [];
79
        foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
80
            // convert expires_in to int if it is not NULL
81
            $row['expires_in'] = !is_null($row['expires_in']) ? (int) $row['expires_in'] : null;
82
            $accessTokenList[] = new AccessToken($row);
83
        }
84
85
        return $accessTokenList;
86
    }
87
88
    /**
89
     * @param string      $userId
90
     * @param AccessToken $accessToken
91
     */
92
    public function storeAccessToken($userId, AccessToken $accessToken)
93
    {
94
        $stmt = $this->db->prepare(
95
            'INSERT INTO access_tokens (
96
                user_id, provider_id, issued_at, access_token, token_type, expires_in, refresh_token, scope
97
             ) 
98
             VALUES(
99
                :user_id,
100
                :provider_id, :issued_at, :access_token, :token_type, :expires_in, :refresh_token, :scope
101
             )'
102
        );
103
104
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
105
        $stmt->bindValue(':provider_id', $accessToken->getProviderId(), PDO::PARAM_STR);
106
        $stmt->bindValue(':issued_at', $accessToken->getIssuedAt()->format('Y-m-d H:i:s'), PDO::PARAM_STR);
107
        $stmt->bindValue(':access_token', $accessToken->getToken(), PDO::PARAM_STR);
108
        $stmt->bindValue(':token_type', $accessToken->getTokenType(), PDO::PARAM_STR);
109
        $stmt->bindValue(':expires_in', $accessToken->getExpiresIn(), PDO::PARAM_INT);
110
        $stmt->bindValue(':refresh_token', $accessToken->getRefreshToken(), PDO::PARAM_STR);
111
        $stmt->bindValue(':scope', $accessToken->getScope(), PDO::PARAM_STR);
112
        $stmt->execute();
113
    }
114
115
    /**
116
     * @param string      $userId
117
     * @param AccessToken $accessToken
118
     */
119
    public function deleteAccessToken($userId, AccessToken $accessToken)
120
    {
121
        $stmt = $this->db->prepare(
122
            'DELETE FROM
123
                access_tokens
124
            WHERE
125
                user_id = :user_id
126
            AND
127
                provider_id = :provider_id
128
            AND
129
                access_token = :access_token'
130
        );
131
132
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
133
        $stmt->bindValue(':provider_id', $accessToken->getProviderId(), PDO::PARAM_STR);
134
        $stmt->bindValue(':access_token', $accessToken->getToken(), PDO::PARAM_STR);
135
        $stmt->execute();
136
    }
137
}
138