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 ( a3174d...5adad1 )
by François
02:55
created

PdoAccessTokenStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 40.54 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 30
loc 74
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A storeAccessToken() 0 19 1
A retrieveAccessToken() 22 22 2
A createTableQueries() 0 16 1

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
/**
4
 *  Copyright 2015 François Kooman <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace fkooman\RemoteStorage\OAuth\Storage;
20
21
use fkooman\IO\IO;
22
use fkooman\RemoteStorage\OAuth\AccessToken;
23
use fkooman\RemoteStorage\OAuth\AccessTokenStorageInterface;
24
use PDO;
25
26
class PdoAccessTokenStorage extends PdoBaseStorage implements AccessTokenStorageInterface
27
{
28
    /** @var \fkooman\IO\IO */
29
    private $io;
30
31 View Code Duplication
    public function __construct(PDO $db, $dbPrefix = '', IO $io = null)
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...
32
    {
33
        parent::__construct($db, $dbPrefix);
34
        if (null === $io) {
35
            $io = new IO();
36
        }
37
        $this->io = $io;
38
    }
39
40
    public function storeAccessToken(AccessToken $accessToken)
41
    {
42
        $generatedToken = $this->io->getRandom();
43
44
        $stmt = $this->db->prepare(
45
            sprintf(
46
                'INSERT INTO %s (token, client_id, user_id, issued_at, scope) VALUES(:token, :client_id, :user_id, :issued_at, :scope)',
47
                $this->dbPrefix.'access_token'
48
            )
49
        );
50
        $stmt->bindValue(':token', $generatedToken, PDO::PARAM_STR);
51
        $stmt->bindValue(':client_id', $accessToken->getClientId(), PDO::PARAM_STR);
52
        $stmt->bindValue(':user_id', $accessToken->getUserId(), PDO::PARAM_STR);
53
        $stmt->bindValue(':issued_at', $accessToken->getIssuedAt(), PDO::PARAM_INT);
54
        $stmt->bindValue(':scope', $accessToken->getScope(), PDO::PARAM_STR);
55
        $stmt->execute();
56
57
        return $generatedToken;
58
    }
59
60 View Code Duplication
    public function retrieveAccessToken($accessToken)
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...
61
    {
62
        $stmt = $this->db->prepare(
63
            sprintf(
64
                'SELECT client_id, user_id, issued_at, scope FROM %s WHERE token = :token',
65
                $this->dbPrefix.'access_token'
66
            )
67
        );
68
        $stmt->bindValue(':token', $accessToken, PDO::PARAM_STR);
69
        $stmt->execute();
70
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
71
        if (false === $result) {
72
            return false;
73
        }
74
75
        return new AccessToken(
76
            $result['client_id'],
77
            $result['user_id'],
78
            $result['issued_at'],
79
            $result['scope']
80
        );
81
    }
82
83
    public function createTableQueries($dbPrefix)
84
    {
85
        return [
86
            sprintf(
87
                'CREATE TABLE IF NOT EXISTS %s (
88
                    token VARCHAR(255) NOT NULL,
89
                    client_id VARCHAR(255) NOT NULL,
90
                    user_id VARCHAR(255) NOT NULL,
91
                    issued_at INT NOT NULL,
92
                    scope VARCHAR(255) NOT NULL,
93
                    PRIMARY KEY (token)
94
                )',
95
                $dbPrefix.'access_token'
96
            ),
97
        ];
98
    }
99
}
100