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.

TokenStorage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 131
Duplicated Lines 25.95 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 34
loc 131
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B store() 0 29 1
A getExistingToken() 0 21 1
A get() 18 19 1
A getAuthorizedClients() 16 16 1
A removeClientTokens() 0 11 1
A init() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
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\RemoteStorage\OAuth;
20
21
use DateTime;
22
use PDO;
23
24
class TokenStorage
25
{
26
    /** @var PDO */
27
    private $db;
28
29
    public function __construct(PDO $db)
30
    {
31
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
32
        $this->db = $db;
33
    }
34
35
    public function store($userId, $accessTokenKey, $accessToken, $clientId, $scope, DateTime $expiresAt)
36
    {
37
        $stmt = $this->db->prepare(
38
            'INSERT INTO tokens (
39
                user_id,    
40
                access_token_key,
41
                access_token,
42
                client_id,
43
                scope,
44
                expires_at
45
             ) 
46
             VALUES(
47
                :user_id, 
48
                :access_token_key,
49
                :access_token,
50
                :client_id,
51
                :scope,
52
                :expires_at
53
             )'
54
        );
55
56
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
57
        $stmt->bindValue(':access_token_key', $accessTokenKey, PDO::PARAM_STR);
58
        $stmt->bindValue(':access_token', $accessToken, PDO::PARAM_STR);
59
        $stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR);
60
        $stmt->bindValue(':scope', $scope, PDO::PARAM_STR);
61
        $stmt->bindValue(':expires_at', $expiresAt->format('Y-m-d H:i:s'), PDO::PARAM_STR);
62
        $stmt->execute();
63
    }
64
65
    public function getExistingToken($userId, $clientId, $scope)
66
    {
67
        $stmt = $this->db->prepare(
68
            'SELECT
69
                access_token_key,
70
                access_token,
71
                expires_at
72
             FROM tokens
73
             WHERE
74
                user_id = :user_id AND
75
                client_id = :client_id AND
76
                scope = :scope'
77
        );
78
79
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
80
        $stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR);
81
        $stmt->bindValue(':scope', $scope, PDO::PARAM_STR);
82
        $stmt->execute();
83
84
        return $stmt->fetch(PDO::FETCH_ASSOC);
85
    }
86
87 View Code Duplication
    public function get($accessTokenKey)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $stmt = $this->db->prepare(
90
            'SELECT
91
                user_id,    
92
                access_token,
93
                client_id,
94
                scope,
95
                expires_at
96
             FROM tokens
97
             WHERE
98
                access_token_key = :access_token_key'
99
        );
100
101
        $stmt->bindValue(':access_token_key', $accessTokenKey, PDO::PARAM_STR);
102
        $stmt->execute();
103
104
        return $stmt->fetch(PDO::FETCH_ASSOC);
105
    }
106
107 View Code Duplication
    public function getAuthorizedClients($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $stmt = $this->db->prepare(
110
            'SELECT
111
                client_id,
112
                scope
113
             FROM tokens
114
             WHERE
115
                user_id = :user_id'
116
        );
117
118
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
119
        $stmt->execute();
120
121
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
122
    }
123
124
    public function removeClientTokens($userId, $clientId)
125
    {
126
        $stmt = $this->db->prepare(
127
            'DELETE FROM tokens
128
             WHERE user_id = :user_id AND client_id = :client_id'
129
        );
130
131
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
132
        $stmt->bindValue(':client_id', $clientId, PDO::PARAM_STR);
133
        $stmt->execute();
134
    }
135
136
    public function init()
137
    {
138
        $queryList = [
139
            'CREATE TABLE IF NOT EXISTS tokens (
140
                user_id VARCHAR(255) NOT NULL,
141
                access_token_key VARCHAR(255) NOT NULL,
142
                access_token VARCHAR(255) NOT NULL,
143
                client_id VARCHAR(255) NOT NULL,
144
                scope VARCHAR(255) NOT NULL,
145
                expires_at VARCHAR(255) NOT NULL,
146
                UNIQUE(access_token_key)
147
            )',
148
        ];
149
150
        foreach ($queryList as $query) {
151
            $this->db->query($query);
152
        }
153
    }
154
}
155