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 ( d4fd4b...8b3606 )
by François
03:01
created

OtpLog::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace fkooman\VPN\Server\Log;
4
5
use PDO;
6
use PDOException;
7
8
/**
9
 * Keep track of the used OTP keys so they cannot be replayed.
10
 */
11
class OtpLog
12
{
13
    /** @var PDO */
14
    private $db;
15
16
    /** @var string */
17
    private $prefix;
18
19
    public function __construct(PDO $db, $prefix = '')
20
    {
21
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
22
        $this->db = $db;
23
        $this->prefix = $prefix;
24
    }
25
26
    public function record($userId, $otpKey, $timeUnix)
27
    {
28
        $stmt = $this->db->prepare(
29
            sprintf(
30
                'INSERT INTO %s (
31
                    user_id,
32
                    otp_key,
33
                    time_unix
34
                 ) 
35
                 VALUES(
36
                    :user_id, 
37
                    :otp_key,
38
                    :time_unix
39
                 )',
40
                $this->prefix.'otp_log'
41
            )
42
        );
43
44
        $stmt->bindValue(':user_id', $userId, PDO::PARAM_STR);
45
        $stmt->bindValue(':otp_key', $otpKey, PDO::PARAM_STR);
46
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
47
        try {
48
            $stmt->execute();
49
        } catch (PDOException $e) {
50
            return false;
51
        }
52
53
        return true;
54
    }
55
56 View Code Duplication
    public function housekeeping($timeUnix)
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...
57
    {
58
        $stmt = $this->db->prepare(
59
            sprintf(
60
                'DELETE FROM %s 
61
                    WHERE time_unix < :time_unix',
62
                $this->prefix.'otp_log'
63
            )
64
        );
65
66
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
67
        $stmt->execute();
68
    }
69
70
    public static function createTableQueries($prefix)
71
    {
72
        $query = array(
73
            sprintf(
74
                'CREATE TABLE IF NOT EXISTS %s (
75
                    user_id VARCHAR(255) NOT NULL,
76
                    otp_key VARCHAR(255) NOT NULL,
77
                    time_unix INTEGER NOT NULL,
78
                    UNIQUE(user_id, otp_key)
79
                )',
80
                $prefix.'otp_log'
81
            ),
82
        );
83
84
        return $query;
85
    }
86
87 View Code Duplication
    public function initDatabase()
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
        $queries = self::createTableQueries($this->prefix);
90
        foreach ($queries as $q) {
91
            $this->db->query($q);
92
        }
93
94
        $tables = array('otp_log');
95
        foreach ($tables as $t) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
96
            // make sure the tables are empty
97
#            $this->db->query(
98
#                sprintf(
99
#                    'DELETE FROM %s',
100
#                    $this->prefix.$t
101
#                )
102
#            );
103
        }
104
    }
105
}
106