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