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.

Stats::get()   F
last analyzed

Complexity

Conditions 14
Paths 970

Size

Total Lines 92
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 2.4369
c 0
b 0
f 0
cc 14
eloc 58
nc 970
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server;
11
12
use DateTime;
13
14
class Stats
15
{
16
    /** @var \DateTime */
17
    private $now;
18
19
    public function __construct(DateTime $now)
20
    {
21
        $this->now = $now;
22
    }
23
24
    public function get(array $logEntries)
25
    {
26
        $statsData = [];
27
        $timeConnection = [];
28
        $uniqueUsers = [];
29
        $activeUserCount = 0;
30
31
        foreach ($logEntries as $entry) {
32
            $userId = $entry['user_id'];
33
            $connectedAt = $entry['connected_at'];
34
            $disconnectedAt = $entry['disconnected_at'];
35
            $connectedAtDateTime = new DateTime($entry['connected_at']);
36
            $dateOfConnection = $connectedAtDateTime->format('Y-m-d');
37
38
            if (!array_key_exists($dateOfConnection, $statsData)) {
39
                $statsData[$dateOfConnection] = [
40
                    'number_of_connections' => 0,
41
                    'bytes_transferred' => 0,
42
                    'unique_user_list' => [],
43
                ];
44
            }
45
46
            if (is_null($disconnectedAt)) {
47
                $activeUserCount += 1;
48
            }
49
50
            $statsData[$dateOfConnection]['number_of_connections'] += 1;
51
            $statsData[$dateOfConnection]['bytes_transferred'] += $entry['bytes_transferred'];
52
53
            // add it to table to be able to determine max concurrent connection
54
            // count
55
            if (!array_key_exists($connectedAt, $timeConnection)) {
56
                $timeConnection[$connectedAt] = [];
57
            }
58
            $timeConnection[$connectedAt][] = 'C';
59
60
            if (!is_null($disconnectedAt)) {
61
                if (!array_key_exists($disconnectedAt, $timeConnection)) {
62
                    $timeConnection[$disconnectedAt] = [];
63
                }
64
                $timeConnection[$disconnectedAt][] = 'D';
65
            }
66
67
            // unique user list per day
68
            if (!in_array($userId, $statsData[$dateOfConnection]['unique_user_list'])) {
69
                $statsData[$dateOfConnection]['unique_user_list'][] = $userId;
70
            }
71
72
            // unique user list for the whole logging period
73
            if (!in_array($userId, $uniqueUsers)) {
74
                $uniqueUsers[] = $userId;
75
            }
76
        }
77
78
        ksort($timeConnection);
79
        $maxConcurrentConnections = 0;
80
        $maxConcurrentConnectionsTime = 0;
81
        $concurrentConnections = 0;
82
        foreach ($timeConnection as $unixTime => $eventArray) {
83
            foreach ($eventArray as $event) {
84
                if ('C' === $event) {
85
                    ++$concurrentConnections;
86
                    if ($concurrentConnections > $maxConcurrentConnections) {
87
                        $maxConcurrentConnections = $concurrentConnections;
88
                        $maxConcurrentConnectionsTime = $unixTime;
89
                    }
90
                } else {
91
                    --$concurrentConnections;
92
                }
93
            }
94
        }
95
96
        $totalTraffic = 0;
97
        // convert the user list in unique user count for that day, rework array
98
        // key and determine total amount of traffic
99
        foreach ($statsData as $date => $entry) {
100
            $statsData[$date]['date'] = $date;
101
            $statsData[$date]['unique_user_count'] = count($entry['unique_user_list']);
102
            unset($statsData[$date]['unique_user_list']);
103
            $totalTraffic += $entry['bytes_transferred'];
104
        }
105
106
        return [
107
            'days' => array_values($statsData),
108
            'total_traffic' => $totalTraffic,
109
            'generated_at' => $this->now->getTimestamp(),
110
            'max_concurrent_connections' => $maxConcurrentConnections,
111
            'max_concurrent_connections_time' => $maxConcurrentConnectionsTime,
112
            'unique_user_count' => count($uniqueUsers),
113
            'active_user_count' => $activeUserCount,
114
        ];
115
    }
116
}
117