| Conditions | 2 |
| Paths | 2 |
| Total Lines | 57 |
| Code Lines | 26 |
| 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 |
||
| 43 | public function getStats(Project $project, $start, $end) |
||
| 44 | { |
||
| 45 | $cacheKey = 'adminstats.'.$project->getDatabaseName().$start.$end; |
||
| 46 | if ($this->cache->hasItem($cacheKey)) { |
||
| 47 | return $this->cache->getItem($cacheKey)->get(); |
||
| 48 | } |
||
| 49 | |||
| 50 | $userTable = $project->getTableName('user'); |
||
| 51 | $loggingTable = $project->getTableName('logging', 'userindex'); |
||
| 52 | $userGroupsTable = $project->getTableName('user_groups'); |
||
| 53 | $ufgTable = $project->getTableName('user_former_groups'); |
||
| 54 | |||
| 55 | $adminGroups = join(array_map(function ($group) { |
||
| 56 | return "'$group'"; |
||
| 57 | }, $this->getAdminGroups($project)), ','); |
||
| 58 | |||
| 59 | $sql = "SELECT user_name, user_id, |
||
| 60 | SUM(IF( (log_type = 'delete' AND log_action != 'restore'),1,0)) AS 'delete', |
||
| 61 | SUM(IF( (log_type = 'delete' AND log_action = 'restore'),1,0)) AS 'restore', |
||
| 62 | SUM(IF( (log_type = 'block' AND log_action != 'unblock'),1,0)) AS 'block', |
||
| 63 | SUM(IF( (log_type = 'block' AND log_action = 'unblock'),1,0)) AS 'unblock', |
||
| 64 | SUM(IF( (log_type = 'protect' AND log_action != 'unprotect'),1,0)) AS 'protect', |
||
| 65 | SUM(IF( (log_type = 'protect' AND log_action = 'unprotect'),1,0)) AS 'unprotect', |
||
| 66 | SUM(IF( log_type = 'rights',1,0)) AS 'rights', |
||
| 67 | SUM(IF( log_type = 'import',1,0)) AS 'import', |
||
| 68 | SUM(IF(log_type != '',1,0)) AS 'total' |
||
| 69 | FROM $loggingTable |
||
| 70 | JOIN $userTable ON user_id = log_user |
||
| 71 | WHERE log_timestamp > '$start' AND log_timestamp <= '$end' |
||
| 72 | AND log_type IS NOT NULL |
||
| 73 | AND log_action IS NOT NULL |
||
| 74 | AND log_type IN ('block', 'delete', 'protect', 'import', 'rights') |
||
| 75 | GROUP BY user_name |
||
| 76 | HAVING 'delete' > 0 OR user_id IN ( |
||
| 77 | -- Make sure they were at some point were in a qualifying user group. |
||
| 78 | -- This also ensures we account for users who were inactive within the time period. |
||
| 79 | SELECT ug_user |
||
| 80 | FROM $userGroupsTable |
||
| 81 | WHERE ug_group IN ($adminGroups) |
||
| 82 | UNION |
||
| 83 | SELECT ufg_user |
||
| 84 | FROM $ufgTable |
||
| 85 | WHERE ufg_group IN ($adminGroups) |
||
| 86 | ) |
||
| 87 | ORDER BY 'total' DESC"; |
||
| 88 | |||
| 89 | $results = $this->getProjectsConnection()->query($sql)->fetchAll(); |
||
| 90 | |||
| 91 | // Cache for 10 minutes. |
||
| 92 | $cacheItem = $this->cache |
||
| 93 | ->getItem($cacheKey) |
||
| 94 | ->set($results) |
||
| 95 | ->expiresAfter(new DateInterval('PT10M')); |
||
| 96 | $this->cache->save($cacheItem); |
||
| 97 | |||
| 98 | return $results; |
||
| 99 | } |
||
| 100 | |||
| 147 |