| Total Complexity | 61 |
| Total Lines | 540 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LogHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LogHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class LogHelper |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @param int $requestId |
||
| 36 | * |
||
| 37 | * @return DataObject[] |
||
| 38 | */ |
||
| 39 | public static function getRequestLogsWithComments( |
||
| 40 | $requestId, |
||
| 41 | PdoDatabase $db, |
||
| 42 | ISecurityManager $securityManager |
||
| 43 | ): array { |
||
| 44 | // FIXME: domains |
||
| 45 | $logs = LogSearchHelper::get($db, 1)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
||
| 46 | |||
| 47 | $currentUser = User::getCurrent($db); |
||
| 48 | $showRestrictedComments = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser) === ISecurityManager::ALLOWED; |
||
| 49 | $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === ISecurityManager::ALLOWED; |
||
| 50 | |||
| 51 | $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
||
| 52 | |||
| 53 | $items = array_merge($logs, $comments); |
||
| 54 | |||
| 55 | $sortKey = function(DataObject $item): int { |
||
| 56 | if ($item instanceof Log) { |
||
| 57 | return $item->getTimestamp()->getTimestamp(); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ($item instanceof Comment) { |
||
| 61 | return $item->getTime()->getTimestamp(); |
||
| 62 | } |
||
| 63 | |||
| 64 | return 0; |
||
| 65 | }; |
||
| 66 | |||
| 67 | do { |
||
| 68 | $flag = false; |
||
| 69 | |||
| 70 | $loopLimit = (count($items) - 1); |
||
| 71 | for ($i = 0; $i < $loopLimit; $i++) { |
||
| 72 | // are these two items out of order? |
||
| 73 | if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
||
| 74 | // swap them |
||
| 75 | $swap = $items[$i]; |
||
| 76 | $items[$i] = $items[$i + 1]; |
||
| 77 | $items[$i + 1] = $swap; |
||
| 78 | |||
| 79 | // set a flag to say we've modified the array this time around |
||
| 80 | $flag = true; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | while ($flag); |
||
| 85 | |||
| 86 | return $items; |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function getLogDescription(Log $entry): string |
||
| 90 | { |
||
| 91 | $text = "Deferred to "; |
||
| 92 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
| 93 | // Deferred to a different queue |
||
| 94 | // This is exactly what we want to display. |
||
| 95 | return $entry->getAction(); |
||
| 96 | } |
||
| 97 | |||
| 98 | $text = "Closed custom-n"; |
||
| 99 | if ($entry->getAction() == $text) { |
||
| 100 | // Custom-closed |
||
| 101 | return "closed (custom reason - account not created)"; |
||
| 102 | } |
||
| 103 | |||
| 104 | $text = "Closed custom-y"; |
||
| 105 | if ($entry->getAction() == $text) { |
||
| 106 | // Custom-closed |
||
| 107 | return "closed (custom reason - account created)"; |
||
| 108 | } |
||
| 109 | |||
| 110 | $text = "Closed 0"; |
||
| 111 | if ($entry->getAction() == $text) { |
||
| 112 | // Dropped the request - short-circuit the lookup |
||
| 113 | return "dropped request"; |
||
| 114 | } |
||
| 115 | |||
| 116 | $text = "Closed "; |
||
| 117 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
| 118 | // Closed with a reason - do a lookup here. |
||
| 119 | $id = substr($entry->getAction(), strlen($text)); |
||
| 120 | /** @var EmailTemplate|false $template */ |
||
| 121 | $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
||
| 122 | |||
| 123 | if ($template !== false) { |
||
|
|
|||
| 124 | return 'closed (' . $template->getName() . ')'; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // Fall back to the basic stuff |
||
| 129 | $lookup = array( |
||
| 130 | 'Reserved' => 'reserved', |
||
| 131 | 'Email Confirmed' => 'email-confirmed', |
||
| 132 | 'Manually Confirmed' => 'manually confirmed the request', |
||
| 133 | 'Unreserved' => 'unreserved', |
||
| 134 | 'Approved' => 'approved', |
||
| 135 | 'DeactivatedUser' => 'deactivated user', |
||
| 136 | 'RoleChange' => 'changed roles', |
||
| 137 | 'GlobalRoleChange' => 'changed global roles', |
||
| 138 | 'RequestedReactivation' => 'requested reactivation', |
||
| 139 | 'Banned' => 'banned', |
||
| 140 | 'Edited' => 'edited interface message', |
||
| 141 | 'EditComment-c' => 'edited a comment', |
||
| 142 | 'EditComment-r' => 'edited a comment', |
||
| 143 | 'FlaggedComment' => 'flagged a comment', |
||
| 144 | 'UnflaggedComment' => 'unflagged a comment', |
||
| 145 | 'Unbanned' => 'unbanned', |
||
| 146 | 'BanReplaced' => 'replaced ban', |
||
| 147 | 'Promoted' => 'promoted to tool admin', |
||
| 148 | 'BreakReserve' => 'forcibly broke the reservation', |
||
| 149 | 'Prefchange' => 'changed user preferences', |
||
| 150 | 'Renamed' => 'renamed', |
||
| 151 | 'Demoted' => 'demoted from tool admin', |
||
| 152 | 'ReceiveReserved' => 'received the reservation', |
||
| 153 | 'SendReserved' => 'sent the reservation', |
||
| 154 | 'EditedEmail' => 'edited email', |
||
| 155 | 'DeletedTemplate' => 'deleted template', |
||
| 156 | 'EditedTemplate' => 'edited template', |
||
| 157 | 'CreatedEmail' => 'created email', |
||
| 158 | 'CreatedTemplate' => 'created template', |
||
| 159 | 'SentMail' => 'sent an email to the requester', |
||
| 160 | 'Registered' => 'registered a tool account', |
||
| 161 | 'JobIssue' => 'ran a background job unsuccessfully', |
||
| 162 | 'JobCompleted' => 'completed a background job', |
||
| 163 | 'JobAcknowledged' => 'acknowledged a job failure', |
||
| 164 | 'JobRequeued' => 'requeued a job for re-execution', |
||
| 165 | 'JobCancelled' => 'cancelled execution of a job', |
||
| 166 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||
| 167 | 'Hospitalised' => 'sent to the hospital', |
||
| 168 | 'QueueCreated' => 'created a request queue', |
||
| 169 | 'QueueEdited' => 'edited a request queue', |
||
| 170 | 'DomainCreated' => 'created a domain', |
||
| 171 | 'DomainEdited' => 'edited a domain', |
||
| 172 | 'RequestFormCreated' => 'created a request form', |
||
| 173 | 'RequestFormEdited' => 'edited a request form', |
||
| 174 | ); |
||
| 175 | |||
| 176 | if (array_key_exists($entry->getAction(), $lookup)) { |
||
| 177 | return $lookup[$entry->getAction()]; |
||
| 178 | } |
||
| 179 | |||
| 180 | // OK, I don't know what this is. Fall back to something sane. |
||
| 181 | return "performed an unknown action ({$entry->getAction()})"; |
||
| 182 | } |
||
| 183 | |||
| 184 | public static function getLogActions(PdoDatabase $database): array |
||
| 267 | } |
||
| 268 | |||
| 269 | public static function getObjectTypes(): array |
||
| 270 | { |
||
| 271 | return array( |
||
| 272 | 'Ban' => 'Ban', |
||
| 273 | 'Comment' => 'Comment', |
||
| 274 | 'EmailTemplate' => 'Email template', |
||
| 275 | 'JobQueue' => 'Job queue item', |
||
| 276 | 'Request' => 'Request', |
||
| 277 | 'SiteNotice' => 'Site notice', |
||
| 278 | 'User' => 'User', |
||
| 279 | 'WelcomeTemplate' => 'Welcome template', |
||
| 280 | 'RequestQueue' => 'Request queue', |
||
| 281 | 'Domain' => 'Domain', |
||
| 282 | 'RequestForm' => 'Request form' |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * This returns an HTML representation of the object |
||
| 288 | * |
||
| 289 | * @param int $objectId |
||
| 290 | * @param string $objectType |
||
| 291 | * |
||
| 292 | * @category Security-Critical |
||
| 293 | */ |
||
| 294 | private static function getObjectDescription( |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Log[] $logs |
||
| 437 | * @throws Exception |
||
| 438 | * |
||
| 439 | * @returns User[] |
||
| 440 | */ |
||
| 441 | private static function loadUsersFromLogs(array $logs, PdoDatabase $database): array |
||
| 442 | { |
||
| 443 | $userIds = array(); |
||
| 444 | |||
| 445 | foreach ($logs as $logEntry) { |
||
| 446 | if (!$logEntry instanceof Log) { |
||
| 447 | // if this happens, we've done something wrong with passing back the log data. |
||
| 448 | throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
||
| 449 | } |
||
| 450 | |||
| 451 | $user = $logEntry->getUser(); |
||
| 452 | if ($user === -1) { |
||
| 453 | continue; |
||
| 454 | } |
||
| 455 | |||
| 456 | if (!array_search($user, $userIds)) { |
||
| 457 | $userIds[] = $user; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
||
| 462 | $users[-1] = User::getCommunity()->getUsername(); |
||
| 463 | |||
| 464 | return $users; |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param Log[] $logs |
||
| 469 | * |
||
| 470 | * @throws Exception |
||
| 471 | */ |
||
| 472 | public static function prepareLogsForTemplate( |
||
| 572 | } |
||
| 573 | } |
||
| 574 |