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 ( 183df2...72019f )
by François
02:10
created

ConnectionLog::housekeeping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
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
namespace SURFnet\VPN\Server\Api;
19
20
use PDO;
21
use PDOException;
22
23
/**
24
 * Keep track of VPN connections.
25
 */
26
class ConnectionLog
27
{
28
    /** @var PDO */
29
    private $db;
30
31
    public function __construct(PDO $db)
32
    {
33
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
34
        $this->db = $db;
35
    }
36
37
    public function connect($profileId, $commonName, $ip4, $ip6, $connectedAt)
38
    {
39
        $stmt = $this->db->prepare(
40
            'INSERT INTO connection_log (
41
                profile_id,
42
                common_name,
43
                ip4,
44
                ip6,
45
                connected_at
46
             ) 
47
             VALUES(
48
                :profile_id, 
49
                :common_name,
50
                :ip4,
51
                :ip6,
52
                :connected_at
53
             )'
54
        );
55
56
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
57
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
58
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
59
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
60
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
61
62
        try {
63
            $stmt->execute();
64
        } catch (PDOException $e) {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public function disconnect($profileId, $commonName, $ip4, $ip6, $connectedAt, $disconnectedAt, $bytesTransferred)
72
    {
73
        $stmt = $this->db->prepare(
74
            'UPDATE connection_log
75
                SET 
76
                    disconnected_at = :disconnected_at, 
77
                    bytes_transferred = :bytes_transferred
78
                WHERE 
79
                    profile_id = :profile_id AND
80
                    common_name = :common_name AND
81
                    ip4 = :ip4 AND
82
                    ip6 = :ip6 AND
83
                    connected_at = :connected_at
84
            '
85
        );
86
87
        $stmt->bindValue(':profile_id', $profileId, PDO::PARAM_STR);
88
        $stmt->bindValue(':common_name', $commonName, PDO::PARAM_STR);
89
        $stmt->bindValue(':ip4', $ip4, PDO::PARAM_STR);
90
        $stmt->bindValue(':ip6', $ip6, PDO::PARAM_STR);
91
        $stmt->bindValue(':connected_at', $connectedAt, PDO::PARAM_INT);
92
        $stmt->bindValue(':disconnected_at', $disconnectedAt, PDO::PARAM_INT);
93
        $stmt->bindValue(':bytes_transferred', $bytesTransferred, PDO::PARAM_INT);
94
95
        // XXX number of affected rows should be one!
96
        try {
97
            $stmt->execute();
98
        } catch (PDOException $e) {
99
            return false;
100
        }
101
102
        return true;
103
    }
104
105 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...
106
    {
107
        $stmt = $this->db->prepare(
108
            sprintf(
109
                'DELETE FROM connection_log
110
                    WHERE connected_at < :time_unix'
111
            )
112
        );
113
114
        $stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT);
115
        $stmt->execute();
116
    }
117
118
    public function init()
119
    {
120
        $queryList = [
121
            'CREATE TABLE IF NOT EXISTS connection_log (
122
                profile_id VARCHAR(255) NOT NULL,
123
                common_name VARCHAR(255) NOT NULL,
124
                ip4 VARCHAR(255) NOT NULL,
125
                ip6 VARCHAR(255) NOT NULL,
126
                connected_at INTEGER NOT NULL,
127
                disconnected_at INTEGER DEFAULT NULL,
128
                bytes_transferred INTEGER DEFAULT NULL                
129
            )',
130
        ];
131
132
        foreach ($queryList as $query) {
133
            $this->db->query($query);
134
        }
135
    }
136
}
137