| Conditions | 14 |
| Paths | 970 |
| Total Lines | 92 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 |