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