Total Complexity | 54 |
Total Lines | 519 |
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 |
||
31 | class LogHelper |
||
32 | { |
||
33 | /** |
||
34 | * Summary of getRequestLogsWithComments |
||
35 | * |
||
36 | * @param int $requestId |
||
37 | * @param PdoDatabase $db |
||
38 | * @param SecurityManager $securityManager |
||
39 | * |
||
40 | * @return DataObject[] |
||
41 | */ |
||
42 | public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
||
43 | { |
||
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) === SecurityManager::ALLOWED; |
||
49 | $showCheckuserComments = $securityManager->allows('RequestData', 'seeCheckuserComments', $currentUser) === SecurityManager::ALLOWED; |
||
50 | |||
51 | $comments = Comment::getForRequest($requestId, $db, $showRestrictedComments, $showCheckuserComments, $currentUser->getId()); |
||
52 | |||
53 | $items = array_merge($logs, $comments); |
||
54 | |||
55 | /** |
||
56 | * @param DataObject $item |
||
57 | * |
||
58 | * @return int |
||
59 | */ |
||
60 | $sortKey = function(DataObject $item) { |
||
61 | if ($item instanceof Log) { |
||
62 | return $item->getTimestamp()->getTimestamp(); |
||
63 | } |
||
64 | |||
65 | if ($item instanceof Comment) { |
||
66 | return $item->getTime()->getTimestamp(); |
||
67 | } |
||
68 | |||
69 | return 0; |
||
70 | }; |
||
71 | |||
72 | do { |
||
73 | $flag = false; |
||
74 | |||
75 | $loopLimit = (count($items) - 1); |
||
76 | for ($i = 0; $i < $loopLimit; $i++) { |
||
77 | // are these two items out of order? |
||
78 | if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
||
79 | // swap them |
||
80 | $swap = $items[$i]; |
||
81 | $items[$i] = $items[$i + 1]; |
||
82 | $items[$i + 1] = $swap; |
||
83 | |||
84 | // set a flag to say we've modified the array this time around |
||
85 | $flag = true; |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | while ($flag); |
||
90 | |||
91 | return $items; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Summary of getLogDescription |
||
96 | * |
||
97 | * @param Log $entry |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public static function getLogDescription(Log $entry) |
||
102 | { |
||
103 | $text = "Deferred to "; |
||
104 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
105 | // Deferred to a different queue |
||
106 | // This is exactly what we want to display. |
||
107 | return $entry->getAction(); |
||
108 | } |
||
109 | |||
110 | $text = "Closed custom-n"; |
||
111 | if ($entry->getAction() == $text) { |
||
112 | // Custom-closed |
||
113 | return "closed (custom reason - account not created)"; |
||
114 | } |
||
115 | |||
116 | $text = "Closed custom-y"; |
||
117 | if ($entry->getAction() == $text) { |
||
118 | // Custom-closed |
||
119 | return "closed (custom reason - account created)"; |
||
120 | } |
||
121 | |||
122 | $text = "Closed 0"; |
||
123 | if ($entry->getAction() == $text) { |
||
124 | // Dropped the request - short-circuit the lookup |
||
125 | return "dropped request"; |
||
126 | } |
||
127 | |||
128 | $text = "Closed "; |
||
129 | if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
||
130 | // Closed with a reason - do a lookup here. |
||
131 | $id = substr($entry->getAction(), strlen($text)); |
||
132 | /** @var EmailTemplate $template */ |
||
133 | $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
||
134 | |||
135 | if ($template != false) { |
||
|
|||
136 | return "closed (" . $template->getName() . ")"; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | // Fall back to the basic stuff |
||
141 | $lookup = array( |
||
142 | 'Reserved' => 'reserved', |
||
143 | 'Email Confirmed' => 'email-confirmed', |
||
144 | 'Manually Confirmed' => 'manually confirmed the request', |
||
145 | 'Unreserved' => 'unreserved', |
||
146 | 'Approved' => 'approved', |
||
147 | 'Suspended' => 'suspended', |
||
148 | 'RoleChange' => 'changed roles', |
||
149 | 'Banned' => 'banned', |
||
150 | 'Edited' => 'edited interface message', |
||
151 | 'Declined' => 'declined', |
||
152 | 'EditComment-c' => 'edited a comment', |
||
153 | 'EditComment-r' => 'edited a comment', |
||
154 | 'FlaggedComment' => 'flagged a comment', |
||
155 | 'UnflaggedComment' => 'unflagged a comment', |
||
156 | 'Unbanned' => 'unbanned', |
||
157 | 'Promoted' => 'promoted to tool admin', |
||
158 | 'BreakReserve' => 'forcibly broke the reservation', |
||
159 | 'Prefchange' => 'changed user preferences', |
||
160 | 'Renamed' => 'renamed', |
||
161 | 'Demoted' => 'demoted from tool admin', |
||
162 | 'ReceiveReserved' => 'received the reservation', |
||
163 | 'SendReserved' => 'sent the reservation', |
||
164 | 'EditedEmail' => 'edited email', |
||
165 | 'DeletedTemplate' => 'deleted template', |
||
166 | 'EditedTemplate' => 'edited template', |
||
167 | 'CreatedEmail' => 'created email', |
||
168 | 'CreatedTemplate' => 'created template', |
||
169 | 'SentMail' => 'sent an email to the requester', |
||
170 | 'Registered' => 'registered a tool account', |
||
171 | 'JobIssue' => 'ran a background job unsuccessfully', |
||
172 | 'JobCompleted' => 'completed a background job', |
||
173 | 'JobAcknowledged' => 'acknowledged a job failure', |
||
174 | 'JobRequeued' => 'requeued a job for re-execution', |
||
175 | 'JobCancelled' => 'cancelled execution of a job', |
||
176 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||
177 | 'Hospitalised' => 'sent to the hospital', |
||
178 | 'QueueCreated' => 'created a request queue', |
||
179 | 'QueueEdited' => 'edited a request queue', |
||
180 | 'DomainCreated' => 'created a domain', |
||
181 | 'DomainEdited' => 'edited a domain', |
||
182 | 'RequestFormCreated' => 'created a request form', |
||
183 | 'RequestFormEdited' => 'edited a request form', |
||
184 | ); |
||
185 | |||
186 | if (array_key_exists($entry->getAction(), $lookup)) { |
||
187 | return $lookup[$entry->getAction()]; |
||
188 | } |
||
189 | |||
190 | // OK, I don't know what this is. Fall back to something sane. |
||
191 | return "performed an unknown action ({$entry->getAction()})"; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param PdoDatabase $database |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | public static function getLogActions(PdoDatabase $database) |
||
200 | { |
||
201 | $lookup = array( |
||
202 | "Requests" => [ |
||
203 | 'Reserved' => 'reserved', |
||
204 | 'Email Confirmed' => 'email-confirmed', |
||
205 | 'Manually Confirmed' => 'manually confirmed', |
||
206 | 'Unreserved' => 'unreserved', |
||
207 | 'EditComment-c' => 'edited a comment (by comment ID)', |
||
208 | 'EditComment-r' => 'edited a comment (by request)', |
||
209 | 'FlaggedComment' => 'flagged a comment', |
||
210 | 'UnflaggedComment' => 'unflagged a comment', |
||
211 | 'BreakReserve' => 'forcibly broke the reservation', |
||
212 | 'ReceiveReserved' => 'received the reservation', |
||
213 | 'SendReserved' => 'sent the reservation', |
||
214 | 'SentMail' => 'sent an email to the requester', |
||
215 | 'Closed 0' => 'dropped request', |
||
216 | 'Closed custom-y' => 'closed (custom reason - account created)', |
||
217 | 'Closed custom-n' => 'closed (custom reason - account not created)', |
||
218 | ], |
||
219 | 'Users' => [ |
||
220 | 'Approved' => 'approved', |
||
221 | 'Suspended' => 'suspended', |
||
222 | 'RoleChange' => 'changed roles', |
||
223 | 'Declined' => 'declined', |
||
224 | 'Prefchange' => 'changed user preferences', |
||
225 | 'Renamed' => 'renamed', |
||
226 | 'Promoted' => 'promoted to tool admin', |
||
227 | 'Demoted' => 'demoted from tool admin', |
||
228 | 'Registered' => 'registered a tool account', |
||
229 | ], |
||
230 | "Bans" => [ |
||
231 | 'Banned' => 'banned', |
||
232 | 'Unbanned' => 'unbanned', |
||
233 | ], |
||
234 | "Site notice" => [ |
||
235 | 'Edited' => 'edited interface message', |
||
236 | ], |
||
237 | "Email close templates" => [ |
||
238 | 'EditedEmail' => 'edited email', |
||
239 | 'CreatedEmail' => 'created email', |
||
240 | ], |
||
241 | "Welcome templates" => [ |
||
242 | 'DeletedTemplate' => 'deleted template', |
||
243 | 'EditedTemplate' => 'edited template', |
||
244 | 'CreatedTemplate' => 'created template', |
||
245 | ], |
||
246 | "Job queue" => [ |
||
247 | 'JobIssue' => 'ran a background job unsuccessfully', |
||
248 | 'JobCompleted' => 'completed a background job', |
||
249 | 'JobAcknowledged' => 'acknowledged a job failure', |
||
250 | 'JobRequeued' => 'requeued a job for re-execution', |
||
251 | 'JobCancelled' => 'cancelled execution of a job', |
||
252 | 'EnqueuedJobQueue' => 'scheduled for creation', |
||
253 | 'Hospitalised' => 'sent to the hospital', |
||
254 | ], |
||
255 | "Request queues" => [ |
||
256 | 'QueueCreated' => 'created a request queue', |
||
257 | 'QueueEdited' => 'edited a request queue', |
||
258 | ], |
||
259 | "Domains" => [ |
||
260 | 'DomainCreated' => 'created a domain', |
||
261 | 'DomainEdited' => 'edited a domain', |
||
262 | ], |
||
263 | "Request forms" => [ |
||
264 | 'RequestFormCreated' => 'created a request form', |
||
265 | 'RequestFormEdited' => 'edited a request form', |
||
266 | ], |
||
267 | ); |
||
268 | |||
269 | $databaseDrivenLogKeys = $database->query(<<<SQL |
||
270 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v FROM emailtemplate |
||
271 | UNION ALL |
||
272 | SELECT CONCAT('Deferred to ', logname) AS k, CONCAT('deferred to ', displayname) AS v FROM requestqueue; |
||
273 | SQL |
||
274 | ); |
||
275 | foreach ($databaseDrivenLogKeys->fetchAll(PDO::FETCH_ASSOC) as $row) { |
||
276 | $lookup["Requests"][$row['k']] = $row['v']; |
||
277 | } |
||
278 | |||
279 | return $lookup; |
||
280 | } |
||
281 | |||
282 | public static function getObjectTypes() |
||
283 | { |
||
284 | return array( |
||
285 | 'Ban' => 'Ban', |
||
286 | 'Comment' => 'Comment', |
||
287 | 'EmailTemplate' => 'Email template', |
||
288 | 'JobQueue' => 'Job queue item', |
||
289 | 'Request' => 'Request', |
||
290 | 'SiteNotice' => 'Site notice', |
||
291 | 'User' => 'User', |
||
292 | 'WelcomeTemplate' => 'Welcome template', |
||
293 | 'RequestQueue' => 'Request queue', |
||
294 | 'Domain' => 'Domain', |
||
295 | 'RequestForm' => 'Request form' |
||
296 | ); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * This returns a HTML |
||
301 | * |
||
302 | * @param string $objectId |
||
303 | * @param string $objectType |
||
304 | * @param PdoDatabase $database |
||
305 | * @param SiteConfiguration $configuration |
||
306 | * |
||
307 | * @return null|string |
||
308 | * @category Security-Critical |
||
309 | */ |
||
310 | private static function getObjectDescription( |
||
311 | $objectId, |
||
312 | $objectType, |
||
313 | PdoDatabase $database, |
||
314 | SiteConfiguration $configuration |
||
315 | ) { |
||
316 | if ($objectType == '') { |
||
317 | return null; |
||
318 | } |
||
319 | |||
320 | $baseurl = $configuration->getBaseUrl(); |
||
321 | |||
322 | switch ($objectType) { |
||
323 | case 'Ban': |
||
324 | /** @var Ban $ban */ |
||
325 | $ban = Ban::getById($objectId, $database); |
||
326 | |||
327 | if ($ban === false) { |
||
328 | return 'Ban #' . $objectId; |
||
329 | } |
||
330 | |||
331 | return <<<HTML |
||
332 | <a href="{$baseurl}/internal.php/bans/show?id={$objectId}">Ban #{$objectId}</a> |
||
333 | HTML; |
||
334 | case 'EmailTemplate': |
||
335 | /** @var EmailTemplate $emailTemplate */ |
||
336 | $emailTemplate = EmailTemplate::getById($objectId, $database); |
||
337 | |||
338 | if ($emailTemplate === false) { |
||
339 | return 'Email Template #' . $objectId; |
||
340 | } |
||
341 | |||
342 | $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
||
343 | |||
344 | return <<<HTML |
||
345 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
||
346 | HTML; |
||
347 | case 'SiteNotice': |
||
348 | return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
||
349 | case 'Request': |
||
350 | /** @var Request $request */ |
||
351 | $request = Request::getById($objectId, $database); |
||
352 | |||
353 | if ($request === false) { |
||
354 | return 'Request #' . $objectId; |
||
355 | } |
||
356 | |||
357 | $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
||
358 | |||
359 | return <<<HTML |
||
360 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
||
361 | HTML; |
||
362 | case 'User': |
||
363 | /** @var User $user */ |
||
364 | $user = User::getById($objectId, $database); |
||
365 | |||
366 | // Some users were merged out of existence |
||
367 | if ($user === false) { |
||
368 | return 'User #' . $objectId; |
||
369 | } |
||
370 | |||
371 | $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
||
372 | |||
373 | return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
||
374 | case 'WelcomeTemplate': |
||
375 | /** @var WelcomeTemplate $welcomeTemplate */ |
||
376 | $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
||
377 | |||
378 | // some old templates have been completely deleted and lost to the depths of time. |
||
379 | if ($welcomeTemplate === false) { |
||
380 | return "Welcome template #{$objectId}"; |
||
381 | } |
||
382 | else { |
||
383 | $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
||
384 | |||
385 | return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
||
386 | } |
||
387 | case 'JobQueue': |
||
388 | /** @var JobQueue $job */ |
||
389 | $job = JobQueue::getById($objectId, $database); |
||
390 | |||
391 | $taskDescriptions = JobQueue::getTaskDescriptions(); |
||
392 | |||
393 | if ($job === false) { |
||
394 | return 'Job Queue Task #' . $objectId; |
||
395 | } |
||
396 | |||
397 | $task = $job->getTask(); |
||
398 | if (isset($taskDescriptions[$task])) { |
||
399 | $description = $taskDescriptions[$task]; |
||
400 | } |
||
401 | else { |
||
402 | $description = 'Unknown task'; |
||
403 | } |
||
404 | |||
405 | return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
||
406 | case 'RequestQueue': |
||
407 | /** @var RequestQueue $queue */ |
||
408 | $queue = RequestQueue::getById($objectId, $database); |
||
409 | |||
410 | if ($queue === false) { |
||
411 | return "Request Queue #{$objectId}"; |
||
412 | } |
||
413 | |||
414 | $queueHeader = htmlentities($queue->getHeader(), ENT_COMPAT, 'UTF-8'); |
||
415 | |||
416 | return "<a href=\"{$baseurl}/internal.php/queueManagement/edit?queue={$objectId}\">{$queueHeader}</a>"; |
||
417 | case 'Domain': |
||
418 | /** @var Domain $domain */ |
||
419 | $domain = Domain::getById($objectId, $database); |
||
420 | |||
421 | if ($domain === false) { |
||
422 | return "Domain #{$objectId}"; |
||
423 | } |
||
424 | |||
425 | $domainName = htmlentities($domain->getShortName(), ENT_COMPAT, 'UTF-8'); |
||
426 | return "<a href=\"{$baseurl}/internal.php/domainManagement/edit?domain={$objectId}\">{$domainName}</a>"; |
||
427 | case 'RequestForm': |
||
428 | /** @var RequestForm $queue */ |
||
429 | $queue = RequestForm::getById($objectId, $database); |
||
430 | |||
431 | if ($queue === false) { |
||
432 | return "Request Form #{$objectId}"; |
||
433 | } |
||
434 | |||
435 | $formName = htmlentities($queue->getName(), ENT_COMPAT, 'UTF-8'); |
||
436 | |||
437 | return "<a href=\"{$baseurl}/internal.php/requestFormManagement/edit?form={$objectId}\">{$formName}</a>"; |
||
438 | case 'Comment': |
||
439 | /** @var Comment $comment */ |
||
440 | $comment = Comment::getById($objectId, $database); |
||
441 | $requestName = htmlentities(Request::getById($comment->getRequest(), $database)->getName(), ENT_COMPAT, 'UTF-8'); |
||
442 | |||
443 | return "<a href=\"{$baseurl}/internal.php/editComment?id={$objectId}\">Comment {$objectId}</a> on request <a href=\"{$baseurl}/internal.php/viewRequest?id={$comment->getRequest()}#comment-{$objectId}\">#{$comment->getRequest()} ({$requestName})</a>"; |
||
444 | default: |
||
445 | return '[' . $objectType . " " . $objectId . ']'; |
||
446 | } |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param Log[] $logs |
||
451 | * @param PdoDatabase $database |
||
452 | * @param SiteConfiguration $configuration |
||
453 | * |
||
454 | * @return array |
||
455 | * @throws Exception |
||
456 | */ |
||
457 | public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
||
550 | } |
||
551 | } |
||
552 |