Total Complexity | 47 |
Total Lines | 423 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Logger 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 Logger, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Logger |
||
37 | { |
||
38 | /** |
||
39 | * @param PdoDatabase $database |
||
40 | * @param Request $object |
||
41 | */ |
||
42 | public static function emailConfirmed(PdoDatabase $database, Request $object) |
||
43 | { |
||
44 | self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param PdoDatabase $database |
||
49 | * @param DataObject $object |
||
50 | * @param string $logAction |
||
51 | * @param null|string $comment |
||
52 | * @param User|null $user |
||
53 | * @param int|null $domain |
||
54 | * |
||
55 | * @throws Exception |
||
56 | */ |
||
57 | private static function createLogEntry( |
||
58 | PdoDatabase $database, |
||
59 | DataObject $object, |
||
60 | $logAction, |
||
61 | $comment = null, |
||
62 | $user = null, |
||
63 | ?int $domain = null |
||
64 | ) { |
||
65 | if ($user == null) { |
||
66 | $user = User::getCurrent($database); |
||
67 | } |
||
68 | |||
69 | $objectType = get_class($object); |
||
70 | if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
||
71 | $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
||
72 | } |
||
73 | |||
74 | $log = new Log(); |
||
75 | |||
76 | if ($domain !== null) { |
||
77 | $log->setDomain($domain); |
||
78 | } |
||
79 | elseif (method_exists($object, 'getDomain')) { |
||
80 | $log->setDomain($object->getDomain()); |
||
81 | } |
||
82 | |||
83 | $log->setDatabase($database); |
||
84 | $log->setAction($logAction); |
||
85 | $log->setObjectId($object->getId()); |
||
86 | $log->setObjectType($objectType); |
||
87 | $log->setUser($user); |
||
88 | $log->setComment($comment); |
||
89 | $log->save(); |
||
90 | } |
||
91 | |||
92 | #region Users |
||
93 | |||
94 | /** |
||
95 | * @throws Exception |
||
96 | */ |
||
97 | public static function newUser(PdoDatabase $database, User $user) |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @throws Exception |
||
104 | */ |
||
105 | public static function approvedUser(PdoDatabase $database, User $object) |
||
106 | { |
||
107 | self::createLogEntry($database, $object, "Approved"); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @throws Exception |
||
112 | */ |
||
113 | public static function deactivatedUser(PdoDatabase $database, User $object, string $comment) |
||
114 | { |
||
115 | self::createLogEntry($database, $object, 'DeactivatedUser', $comment); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | public static function requestedReactivation(PdoDatabase $database, User $object, string $comment) |
||
122 | { |
||
123 | self::createLogEntry($database, $object, 'RequestedReactivation', $comment); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @throws Exception |
||
128 | */ |
||
129 | public static function renamedUser(PdoDatabase $database, User $object, string $comment) |
||
130 | { |
||
131 | self::createLogEntry($database, $object, "Renamed", $comment); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @throws Exception |
||
136 | */ |
||
137 | public static function userPreferencesChange(PdoDatabase $database, User $object) |
||
138 | { |
||
139 | self::createLogEntry($database, $object, "Prefchange"); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @throws Exception |
||
144 | */ |
||
145 | public static function userRolesEdited( |
||
146 | PdoDatabase $database, |
||
147 | User $object, |
||
148 | string $reason, |
||
149 | array $added, |
||
150 | array $removed, |
||
151 | int $domain |
||
152 | ) { |
||
153 | $logData = serialize(array( |
||
154 | 'added' => $added, |
||
155 | 'removed' => $removed, |
||
156 | 'reason' => $reason, |
||
157 | )); |
||
158 | |||
159 | self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @throws Exception |
||
164 | */ |
||
165 | public static function userGlobalRolesEdited( |
||
166 | PdoDatabase $database, |
||
167 | User $object, |
||
168 | string $reason, |
||
169 | array $added, |
||
170 | array $removed |
||
171 | ): void { |
||
172 | $logData = serialize(array( |
||
173 | 'added' => $added, |
||
174 | 'removed' => $removed, |
||
175 | 'reason' => $reason, |
||
176 | )); |
||
177 | |||
178 | self::createLogEntry($database, $object, "GlobalRoleChange", $logData); |
||
179 | } |
||
180 | |||
181 | #endregion |
||
182 | |||
183 | /** |
||
184 | * @param PdoDatabase $database |
||
185 | * @param SiteNotice $object |
||
186 | */ |
||
187 | public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
||
188 | { |
||
189 | self::createLogEntry($database, $object, "Edited"); |
||
190 | } |
||
191 | |||
192 | #region Welcome Templates |
||
193 | |||
194 | /** |
||
195 | * @param PdoDatabase $database |
||
196 | * @param WelcomeTemplate $object |
||
197 | */ |
||
198 | public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
||
199 | { |
||
200 | self::createLogEntry($database, $object, "CreatedTemplate"); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param PdoDatabase $database |
||
205 | * @param WelcomeTemplate $object |
||
206 | */ |
||
207 | public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
||
208 | { |
||
209 | self::createLogEntry($database, $object, "EditedTemplate"); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param PdoDatabase $database |
||
214 | * @param WelcomeTemplate $object |
||
215 | */ |
||
216 | public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
||
217 | { |
||
218 | self::createLogEntry($database, $object, "DeletedTemplate"); |
||
219 | } |
||
220 | |||
221 | #endregion |
||
222 | |||
223 | #region Bans |
||
224 | |||
225 | /** |
||
226 | * @param PdoDatabase $database |
||
227 | * @param Ban $object |
||
228 | * @param string $reason |
||
229 | */ |
||
230 | public static function banned(PdoDatabase $database, Ban $object, $reason) |
||
231 | { |
||
232 | self::createLogEntry($database, $object, "Banned", $reason); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param PdoDatabase $database |
||
237 | * @param Ban $object |
||
238 | * @param string $reason |
||
239 | */ |
||
240 | public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
||
241 | { |
||
242 | self::createLogEntry($database, $object, "Unbanned", $reason); |
||
243 | } |
||
244 | |||
245 | public static function banReplaced(PdoDatabase $database, Ban $object) |
||
246 | { |
||
247 | self::createLogEntry($database, $object, "BanReplaced"); |
||
248 | } |
||
249 | |||
250 | #endregion |
||
251 | |||
252 | #region Requests |
||
253 | |||
254 | /** |
||
255 | * @param PdoDatabase $database |
||
256 | * @param Request $object |
||
257 | * @param string $target |
||
258 | */ |
||
259 | public static function deferRequest(PdoDatabase $database, Request $object, $target) |
||
260 | { |
||
261 | self::createLogEntry($database, $object, "Deferred to $target"); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param PdoDatabase $database |
||
266 | * @param Request $object |
||
267 | * @param integer $target |
||
268 | * @param string $comment |
||
269 | * @param User|null $logUser |
||
270 | */ |
||
271 | public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
||
272 | { |
||
273 | self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
||
274 | } |
||
275 | |||
276 | public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
||
277 | { |
||
278 | self::createLogEntry($database, $object, "Manually Confirmed"); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param PdoDatabase $database |
||
283 | * @param Request $object |
||
284 | */ |
||
285 | public static function reserve(PdoDatabase $database, Request $object) |
||
286 | { |
||
287 | self::createLogEntry($database, $object, "Reserved"); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @param PdoDatabase $database |
||
292 | * @param Request $object |
||
293 | */ |
||
294 | public static function breakReserve(PdoDatabase $database, Request $object) |
||
295 | { |
||
296 | self::createLogEntry($database, $object, "BreakReserve"); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param PdoDatabase $database |
||
301 | * @param Request $object |
||
302 | */ |
||
303 | public static function unreserve(PdoDatabase $database, Request $object) |
||
304 | { |
||
305 | self::createLogEntry($database, $object, "Unreserved"); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param PdoDatabase $database |
||
310 | * @param Comment $object |
||
311 | * @param Request $request |
||
312 | */ |
||
313 | public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
||
314 | { |
||
315 | self::createLogEntry($database, $request, "EditComment-r"); |
||
316 | self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain()); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * @param PdoDatabase $database |
||
321 | * @param Comment $object |
||
322 | */ |
||
323 | public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain) |
||
324 | { |
||
325 | self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @param PdoDatabase $database |
||
330 | * @param Comment $object |
||
331 | */ |
||
332 | public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain) |
||
333 | { |
||
334 | self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @param PdoDatabase $database |
||
339 | * @param Request $object |
||
340 | * @param User $target |
||
341 | */ |
||
342 | public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
||
343 | { |
||
344 | self::createLogEntry($database, $object, "SendReserved"); |
||
345 | self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @param PdoDatabase $database |
||
350 | * @param Request $object |
||
351 | * @param string $comment |
||
352 | */ |
||
353 | public static function sentMail(PdoDatabase $database, Request $object, $comment) |
||
354 | { |
||
355 | self::createLogEntry($database, $object, "SentMail", $comment); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param PdoDatabase $database |
||
360 | * @param Request $object |
||
361 | */ |
||
362 | public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
||
363 | { |
||
364 | self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
||
365 | } |
||
366 | |||
367 | public static function hospitalised(PdoDatabase $database, Request $object) |
||
368 | { |
||
369 | self::createLogEntry($database, $object, 'Hospitalised'); |
||
370 | } |
||
371 | #endregion |
||
372 | |||
373 | #region Email templates |
||
374 | |||
375 | /** |
||
376 | * @param PdoDatabase $database |
||
377 | * @param EmailTemplate $object |
||
378 | */ |
||
379 | public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
||
380 | { |
||
381 | self::createLogEntry($database, $object, "CreatedEmail"); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param PdoDatabase $database |
||
386 | * @param EmailTemplate $object |
||
387 | */ |
||
388 | public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
||
389 | { |
||
390 | self::createLogEntry($database, $object, "EditedEmail"); |
||
391 | } |
||
392 | |||
393 | #endregion |
||
394 | |||
395 | #region Display |
||
396 | |||
397 | #endregion |
||
398 | |||
399 | #region Automation |
||
400 | |||
401 | public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
||
404 | } |
||
405 | |||
406 | public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
||
407 | { |
||
408 | $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
||
409 | self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
||
410 | } |
||
411 | |||
412 | public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
||
413 | { |
||
414 | self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
||
415 | } |
||
416 | |||
417 | public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
||
418 | { |
||
419 | self::createLogEntry($database, $job, 'JobRequeued'); |
||
420 | } |
||
421 | |||
422 | public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
||
423 | { |
||
424 | self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
||
425 | } |
||
426 | #endregion |
||
427 | |||
428 | #region Request Queues |
||
429 | public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
||
430 | { |
||
431 | self::createLogEntry($database, $queue, 'QueueCreated'); |
||
432 | } |
||
433 | |||
434 | public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
||
437 | } |
||
438 | #endregion |
||
439 | #region Domains |
||
440 | public static function domainCreated(PdoDatabase $database, Domain $domain) |
||
441 | { |
||
442 | self::createLogEntry($database, $domain, 'DomainCreated'); |
||
443 | } |
||
444 | |||
445 | public static function domainEdited(PdoDatabase $database, Domain $domain) |
||
446 | { |
||
447 | self::createLogEntry($database, $domain, 'DomainEdited'); |
||
448 | } |
||
449 | #endregion |
||
450 | #region Request Forms |
||
451 | public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
||
452 | { |
||
453 | self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
||
454 | } |
||
455 | |||
456 | public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
||
459 | } |
||
460 | #endregion |
||
461 | } |
||
462 |