| Conditions | 12 |
| Paths | 341 |
| Total Lines | 87 |
| 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 namespace VojtaSvoboda\UserAccessLog\ReportWidgets; |
||
| 57 | protected function loadData() |
||
| 58 | { |
||
| 59 | $days = $this->property('days'); |
||
| 60 | if (!$days) { |
||
| 61 | throw new ApplicationException('Invalid days value: ' . $days); |
||
| 62 | } |
||
| 63 | |||
| 64 | // all accesses for last month |
||
| 65 | $items = AccessLog::where('created_at', '>=', Carbon::now()->subDays($days)->format('Y-m-d'))->get(); |
||
| 66 | |||
| 67 | // parse data |
||
| 68 | $all = []; |
||
| 69 | $users = []; |
||
| 70 | $user_rows = []; |
||
| 71 | foreach ($items as $item) |
||
| 72 | { |
||
| 73 | // user |
||
| 74 | $user_id = $item->user_id ? $item->user_id : 0; |
||
| 75 | $users[$user_id] = $user_id > 0 ? User::find($user_id) : $this->getDeletedFakeUser(); |
||
| 76 | |||
| 77 | // date |
||
| 78 | $timestamp = strtotime($item->created_at) * 1000; |
||
| 79 | $day = Carbon::createFromFormat('Y-m-d H:i:s', $item->created_at)->format('Y-m-d'); |
||
| 80 | |||
| 81 | if (isset($user_rows[$user_id][$day])) { |
||
| 82 | $user_rows[$user_id][$day][1]++; |
||
| 83 | } else { |
||
| 84 | $user_rows[$user_id][$day] = [$timestamp, 1]; |
||
| 85 | } |
||
| 86 | |||
| 87 | // init empty day |
||
| 88 | if (!isset($all[$day])) { |
||
| 89 | $all[$day] = [ |
||
| 90 | 'timestamp' => $timestamp, |
||
| 91 | 'date' => $day, |
||
| 92 | 'count' => 0, |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | |||
| 96 | // increase count |
||
| 97 | $all[$day]['count']++; |
||
| 98 | } |
||
| 99 | |||
| 100 | // we need at least two days, to display chart |
||
| 101 | if (sizeof($all) == 1) { |
||
| 102 | $day = reset($all); |
||
| 103 | $date = Carbon::createFromFormat('Y-m-d', $day['date'])->subDays(1); |
||
| 104 | $dateFormated = $date->format('Y-m-d'); |
||
| 105 | $all[$dateFormated] = [ |
||
| 106 | 'timestamp' => $date->timestamp * 1000, |
||
| 107 | 'date' => $dateFormated, |
||
| 108 | 'count' => 0, |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | // transform user line to json |
||
| 113 | foreach ($user_rows as $key => $user_row) |
||
| 114 | { |
||
| 115 | $rows = []; |
||
| 116 | foreach($user_row as $row) { |
||
| 117 | $rows[] = [ |
||
| 118 | $row[0], |
||
| 119 | $row[1], |
||
| 120 | ]; |
||
| 121 | } |
||
| 122 | |||
| 123 | // we need at least two days, to display chart |
||
| 124 | if (sizeof($rows) == 1) { |
||
| 125 | $first = reset($rows); |
||
| 126 | $rows[] = [$first[0] - 86400000, 0]; |
||
| 127 | } |
||
| 128 | |||
| 129 | $user_rows[$key] = $rows; |
||
| 130 | } |
||
| 131 | |||
| 132 | // count all |
||
| 133 | $all_render = []; |
||
| 134 | foreach ($all as $a) { |
||
| 135 | $all_render[] = [$a['timestamp'], $a['count']]; |
||
| 136 | } |
||
| 137 | |||
| 138 | return [ |
||
| 139 | 'all' => $all_render, |
||
| 140 | 'user_rows' => $user_rows, |
||
| 141 | 'users' => $users, |
||
| 142 | ]; |
||
| 143 | } |
||
| 144 | |||
| 159 |