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 ( 874bad...cd49ac )
by François
01:52
created

Stats::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
19
namespace SURFnet\VPN\Server;
20
21
use DateTime;
22
23
class Stats
24
{
25
    /** @var \DateTime */
26
    private $now;
27
28
    public function __construct(DateTime $now)
29
    {
30
        $this->now = $now;
31
    }
32
33
    public function get(array $logEntries)
34
    {
35
        $statsData = [];
36
        $timeConnection = [];
37
        $uniqueUsers = [];
38
        $activeUserCount = 0;
39
40
        foreach ($logEntries as $entry) {
41
            // determine user_id
42
43
            $userId = substr($entry['common_name'], 0, strpos($entry['common_name'], '_'));
44
            $connectedAt = $entry['connected_at'];
45
            $disconnectedAt = $entry['disconnected_at'];
46
            $dateOfConnection = date('Y-m-d', $connectedAt);
47
48
            if (!array_key_exists($dateOfConnection, $statsData)) {
49
                $statsData[$dateOfConnection] = [
50
                    'number_of_connections' => 0,
51
                    'bytes_transferred' => 0,
52
                    'unique_user_list' => [],
53
                ];
54
            }
55
56
            if (is_null($disconnectedAt)) {
57
                $activeUserCount += 1;
58
            }
59
60
            $statsData[$dateOfConnection]['number_of_connections'] += 1;
61
            $statsData[$dateOfConnection]['bytes_transferred'] += $entry['bytes_transferred'];
62
63
            // add it to table to be able to determine max concurrent connection
64
            // count
65
            if (!array_key_exists($connectedAt, $timeConnection)) {
66
                $timeConnection[$connectedAt] = [];
67
            }
68
            $timeConnection[$connectedAt][] = 'C';
69
70
            if (!is_null($disconnectedAt)) {
71
                if (!array_key_exists($disconnectedAt, $timeConnection)) {
72
                    $timeConnection[$disconnectedAt] = [];
73
                }
74
                $timeConnection[$disconnectedAt][] = 'D';
75
            }
76
77
            // unique user list per day
78
            if (!in_array($userId, $statsData[$dateOfConnection]['unique_user_list'])) {
79
                $statsData[$dateOfConnection]['unique_user_list'][] = $userId;
80
            }
81
82
            // unique user list for the whole logging period
83
            if (!in_array($userId, $uniqueUsers)) {
84
                $uniqueUsers[] = $userId;
85
            }
86
        }
87
88
        ksort($timeConnection);
89
        $firstEntryTime = intval(key($timeConnection));
0 ignored issues
show
Unused Code introduced by
$firstEntryTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        end($timeConnection);
91
        $lastEntryTime = intval(key($timeConnection));
0 ignored issues
show
Unused Code introduced by
$lastEntryTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        reset($timeConnection);
93
94
        $maxConcurrentConnections = 0;
95
        $maxConcurrentConnectionsTime = 0;
0 ignored issues
show
Unused Code introduced by
$maxConcurrentConnectionsTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
96
        $concurrentConnections = 0;
97
        foreach ($timeConnection as $unixTime => $eventArray) {
98
            foreach ($eventArray as $event) {
99
                if ('C' === $event) {
100
                    ++$concurrentConnections;
101
                    if ($concurrentConnections > $maxConcurrentConnections) {
102
                        $maxConcurrentConnections = $concurrentConnections;
103
                        $maxConcurrentConnectionsTime = $unixTime;
0 ignored issues
show
Unused Code introduced by
$maxConcurrentConnectionsTime is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
                    }
105
                } else {
106
                    --$concurrentConnections;
107
                }
108
            }
109
        }
110
111
        $totalTraffic = 0;
112
        // convert the user list in unique user count for that day, rework array
113
        // key and determine total amount of traffic
114
        foreach ($statsData as $date => $entry) {
115
            $statsData[$date]['date'] = $date;
116
            $statsData[$date]['unique_user_count'] = count($entry['unique_user_list']);
117
            unset($statsData[$date]['unique_user_list']);
118
            $totalTraffic += $entry['bytes_transferred'];
119
        }
120
121
        return [
122
            'days' => array_values($statsData),
123
            'total_traffic' => $totalTraffic,
124
            'generated_at' => $this->now->getTimestamp(),
125
            'max_concurrent_connections' => $maxConcurrentConnections,
126
//            'max_concurrent_connections_time' => $maxConcurrentConnectionsTime,
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
//            'first_entry' => $firstEntryTime,
128
//            'last_entry' => $lastEntryTime,
129
            'unique_user_count' => count($uniqueUsers),
130
            'active_user_count' => $activeUserCount,
131
        ];
132
    }
133
}
134