| Total Complexity | 80 |
| Total Lines | 795 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseChatUtils 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 CourseChatUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class CourseChatUtils |
||
| 16 | { |
||
| 17 | private $groupId; |
||
| 18 | private $courseId; |
||
| 19 | private $sessionId; |
||
| 20 | private $userId; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * CourseChat constructor. |
||
| 24 | * |
||
| 25 | * @param int $courseId |
||
| 26 | * @param int $userId |
||
| 27 | * @param int $sessionId |
||
| 28 | * @param int $groupId |
||
| 29 | */ |
||
| 30 | public function __construct($courseId, $userId, $sessionId = 0, $groupId = 0) |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Prepare a message. Clean and insert emojis. |
||
| 40 | * |
||
| 41 | * @param string $message The message to prepare |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public static function prepareMessage($message) |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Save a chat message in a HTML file. |
||
| 80 | * |
||
| 81 | * @param string $message |
||
| 82 | * @param int $friendId |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function saveMessage($message, $friendId = 0) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Disconnect a user from course chats. |
||
| 209 | * |
||
| 210 | * @param int $userId |
||
| 211 | */ |
||
| 212 | public static function exitChat($userId) |
||
| 225 | ]); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Disconnect users who are more than 5 seconds inactive. |
||
| 231 | */ |
||
| 232 | public function disconnectInactiveUsers() |
||
| 270 | ]); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Keep registered to a user as connected. |
||
| 276 | * |
||
| 277 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
| 278 | */ |
||
| 279 | public function keepUserAsConnected() |
||
| 280 | { |
||
| 281 | $em = Database::getManager(); |
||
| 282 | $extraCondition = null; |
||
| 283 | |||
| 284 | if ($this->groupId) { |
||
| 285 | $extraCondition = 'AND ccc.toGroupId = '.intval($this->groupId); |
||
| 286 | } else { |
||
| 287 | $extraCondition = 'AND ccc.sessionId = '.intval($this->sessionId); |
||
| 288 | } |
||
| 289 | |||
| 290 | $currentTime = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 291 | |||
| 292 | $connection = $em |
||
| 293 | ->createQuery(" |
||
| 294 | SELECT ccc FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 295 | WHERE ccc.userId = :user AND ccc.cId = :course $extraCondition |
||
| 296 | ") |
||
| 297 | ->setParameters([ |
||
| 298 | 'user' => $this->userId, |
||
| 299 | 'course' => $this->courseId, |
||
| 300 | ]) |
||
| 301 | ->getOneOrNullResult(); |
||
| 302 | |||
| 303 | if ($connection) { |
||
| 304 | $connection->setLastConnection($currentTime); |
||
| 305 | $em->merge($connection); |
||
| 306 | $em->flush(); |
||
| 307 | |||
| 308 | return; |
||
| 309 | } |
||
| 310 | |||
| 311 | $connection = new CChatConnected(); |
||
| 312 | $connection |
||
| 313 | ->setCId($this->courseId) |
||
| 314 | ->setUserId($this->userId) |
||
| 315 | ->setLastConnection($currentTime) |
||
| 316 | ->setSessionId($this->sessionId) |
||
| 317 | ->setToGroupId($this->groupId); |
||
| 318 | |||
| 319 | $em->persist($connection); |
||
| 320 | $em->flush(); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get the emoji allowed on course chat. |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public static function getEmojiStrategy() |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the emoji list to include in chat. |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | public static function getEmojisToInclude() |
||
| 339 | { |
||
| 340 | return [ |
||
| 341 | ':bowtie:', |
||
| 342 | ':smile:' | |
||
| 343 | ':laughing:', |
||
| 344 | ':blush:', |
||
| 345 | ':smiley:', |
||
| 346 | ':relaxed:', |
||
| 347 | ':smirk:', |
||
| 348 | ':heart_eyes:', |
||
| 349 | ':kissing_heart:', |
||
| 350 | ':kissing_closed_eyes:', |
||
| 351 | ':flushed:', |
||
| 352 | ':relieved:', |
||
| 353 | ':satisfied:', |
||
| 354 | ':grin:', |
||
| 355 | ':wink:', |
||
| 356 | ':stuck_out_tongue_winking_eye:', |
||
| 357 | ':stuck_out_tongue_closed_eyes:', |
||
| 358 | ':grinning:', |
||
| 359 | ':kissing:', |
||
| 360 | ':kissing_smiling_eyes:', |
||
| 361 | ':stuck_out_tongue:', |
||
| 362 | ':sleeping:', |
||
| 363 | ':worried:', |
||
| 364 | ':frowning:', |
||
| 365 | ':anguished:', |
||
| 366 | ':open_mouth:', |
||
| 367 | ':grimacing:', |
||
| 368 | ':confused:', |
||
| 369 | ':hushed:', |
||
| 370 | ':expressionless:', |
||
| 371 | ':unamused:', |
||
| 372 | ':sweat_smile:', |
||
| 373 | ':sweat:', |
||
| 374 | ':disappointed_relieved:', |
||
| 375 | ':weary:', |
||
| 376 | ':pensive:', |
||
| 377 | ':disappointed:', |
||
| 378 | ':confounded:', |
||
| 379 | ':fearful:', |
||
| 380 | ':cold_sweat:', |
||
| 381 | ':persevere:', |
||
| 382 | ':cry:', |
||
| 383 | ':sob:', |
||
| 384 | ':joy:', |
||
| 385 | ':astonished:', |
||
| 386 | ':scream:', |
||
| 387 | ':neckbeard:', |
||
| 388 | ':tired_face:', |
||
| 389 | ':angry:', |
||
| 390 | ':rage:', |
||
| 391 | ':triumph:', |
||
| 392 | ':sleepy:', |
||
| 393 | ':yum:', |
||
| 394 | ':mask:', |
||
| 395 | ':sunglasses:', |
||
| 396 | ':dizzy_face:', |
||
| 397 | ':imp:', |
||
| 398 | ':smiling_imp:', |
||
| 399 | ':neutral_face:', |
||
| 400 | ':no_mouth:', |
||
| 401 | ':innocent:', |
||
| 402 | ':alien:', |
||
| 403 | ]; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the chat history file name. |
||
| 408 | * |
||
| 409 | * @param bool $absolute Optional. Whether get the base or the absolute file path |
||
| 410 | * @param int $friendId optional |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getFileName($absolute = false, $friendId = 0) |
||
| 415 | { |
||
| 416 | $date = date('Y-m-d'); |
||
| 417 | $base = 'messages-'.$date.'.log.html'; |
||
| 418 | |||
| 419 | if ($this->groupId && !$friendId) { |
||
| 420 | $base = 'messages-'.$date.'_gid-'.$this->groupId.'.log.html'; |
||
| 421 | } elseif ($this->sessionId && !$friendId) { |
||
| 422 | $base = 'messages-'.$date.'_sid-'.$this->sessionId.'.log.html'; |
||
| 423 | } elseif ($friendId) { |
||
| 424 | if ($this->userId < $friendId) { |
||
| 425 | $base = 'messages-'.$date.'_uid-'.$this->userId.'-'.$friendId.'.log.html'; |
||
| 426 | } else { |
||
| 427 | $base = 'messages-'.$date.'_uid-'.$friendId.'-'.$this->userId.'.log.html'; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | if (!$absolute) { |
||
| 432 | return $base; |
||
| 433 | } |
||
| 434 | |||
| 435 | $courseInfo = api_get_course_info_by_id($this->courseId); |
||
| 436 | $document_path = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; |
||
| 437 | $chatPath = $document_path.'/chat_files/'; |
||
| 438 | |||
| 439 | if ($this->groupId) { |
||
| 440 | $group_info = GroupManager::get_group_properties($this->groupId); |
||
| 441 | $chatPath = $document_path.$group_info['directory'].'/chat_files/'; |
||
| 442 | } |
||
| 443 | |||
| 444 | return $chatPath.$base; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the chat history. |
||
| 449 | * |
||
| 450 | * @param bool $reset |
||
| 451 | * @param int $friendId optional |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function readMessages($reset = false, $friendId = 0) |
||
| 456 | { |
||
| 457 | $courseInfo = api_get_course_info_by_id($this->courseId); |
||
| 458 | $date_now = date('Y-m-d'); |
||
| 459 | $isMaster = (bool) api_is_course_admin(); |
||
| 460 | $basepath_chat = '/chat_files'; |
||
| 461 | $document_path = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; |
||
| 462 | $group_info = []; |
||
| 463 | if ($this->groupId) { |
||
| 464 | $group_info = GroupManager:: get_group_properties($this->groupId); |
||
| 465 | $basepath_chat = $group_info['directory'].'/chat_files'; |
||
| 466 | } |
||
| 467 | |||
| 468 | $chat_path = $document_path.$basepath_chat.'/'; |
||
| 469 | |||
| 470 | if (!is_dir($chat_path)) { |
||
| 471 | if (is_file($chat_path)) { |
||
| 472 | @unlink($chat_path); |
||
| 473 | } |
||
| 474 | |||
| 475 | if (!api_is_anonymous()) { |
||
| 476 | @mkdir($chat_path, api_get_permissions_for_new_directories()); |
||
| 477 | // Save chat files document for group into item property |
||
| 478 | if ($this->groupId) { |
||
| 479 | $doc_id = add_document( |
||
| 480 | $courseInfo, |
||
| 481 | $basepath_chat, |
||
| 482 | 'folder', |
||
| 483 | 0, |
||
| 484 | 'chat_files', |
||
| 485 | null, |
||
| 486 | 0, |
||
| 487 | true, |
||
| 488 | 0, |
||
| 489 | 0, |
||
| 490 | 0, |
||
| 491 | false |
||
| 492 | ); |
||
| 493 | api_item_property_update( |
||
| 494 | $courseInfo, |
||
| 495 | TOOL_DOCUMENT, |
||
| 496 | $doc_id, |
||
| 497 | 'FolderCreated', |
||
| 498 | null, |
||
| 499 | $group_info, |
||
| 500 | null, |
||
| 501 | null, |
||
| 502 | null |
||
| 503 | ); |
||
| 504 | } |
||
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | $filename_chat = 'messages-'.$date_now.'.log.html'; |
||
| 509 | |||
| 510 | if ($this->groupId && !$friendId) { |
||
| 511 | $filename_chat = 'messages-'.$date_now.'_gid-'.$this->groupId.'.log.html'; |
||
| 512 | } elseif ($this->sessionId && !$friendId) { |
||
| 513 | $filename_chat = 'messages-'.$date_now.'_sid-'.$this->sessionId.'.log.html'; |
||
| 514 | } elseif ($friendId) { |
||
| 515 | if ($this->userId < $friendId) { |
||
| 516 | $filename_chat = 'messages-'.$date_now.'_uid-'.$this->userId.'-'.$friendId.'.log.html'; |
||
| 517 | } else { |
||
| 518 | $filename_chat = 'messages-'.$date_now.'_uid-'.$friendId.'-'.$this->userId.'.log.html'; |
||
| 519 | } |
||
| 520 | } |
||
| 521 | |||
| 522 | if (!file_exists($chat_path.$filename_chat)) { |
||
| 523 | @fclose(fopen($chat_path.$filename_chat, 'w')); |
||
| 524 | if (!api_is_anonymous()) { |
||
| 525 | $doc_id = add_document( |
||
| 526 | $courseInfo, |
||
| 527 | $basepath_chat.'/'.$filename_chat, |
||
| 528 | 'file', |
||
| 529 | 0, |
||
| 530 | $filename_chat, |
||
| 531 | null, |
||
| 532 | 0, |
||
| 533 | true, |
||
| 534 | 0, |
||
| 535 | 0, |
||
| 536 | 0, |
||
| 537 | false |
||
| 538 | ); |
||
| 539 | if ($doc_id) { |
||
| 540 | api_item_property_update( |
||
| 541 | $courseInfo, |
||
| 542 | TOOL_DOCUMENT, |
||
| 543 | $doc_id, |
||
| 544 | 'DocumentAdded', |
||
| 545 | $this->userId, |
||
| 546 | $group_info, |
||
| 547 | null, |
||
| 548 | null, |
||
| 549 | null, |
||
| 550 | $this->sessionId |
||
| 551 | ); |
||
| 552 | api_item_property_update( |
||
| 553 | $courseInfo, |
||
| 554 | TOOL_DOCUMENT, |
||
| 555 | $doc_id, |
||
| 556 | 'invisible', |
||
| 557 | $this->userId, |
||
| 558 | $group_info, |
||
| 559 | null, |
||
| 560 | null, |
||
| 561 | null, |
||
| 562 | $this->sessionId |
||
| 563 | ); |
||
| 564 | item_property_update_on_folder($courseInfo, $basepath_chat, $this->userId); |
||
| 565 | } |
||
| 566 | } |
||
| 567 | } |
||
| 568 | |||
| 569 | $basename_chat = 'messages-'.$date_now; |
||
| 570 | if ($this->groupId && !$friendId) { |
||
| 571 | $basename_chat = 'messages-'.$date_now.'_gid-'.$this->groupId; |
||
| 572 | } elseif ($this->sessionId && !$friendId) { |
||
| 573 | $basename_chat = 'messages-'.$date_now.'_sid-'.$this->sessionId; |
||
| 574 | } elseif ($friendId) { |
||
| 575 | if ($this->userId < $friendId) { |
||
| 576 | $basename_chat = 'messages-'.$date_now.'_uid-'.$this->userId.'-'.$friendId; |
||
| 577 | } else { |
||
| 578 | $basename_chat = 'messages-'.$date_now.'_uid-'.$friendId.'-'.$this->userId; |
||
| 579 | } |
||
| 580 | } |
||
| 581 | |||
| 582 | if ($reset && $isMaster) { |
||
| 583 | $i = 1; |
||
| 584 | while (file_exists($chat_path.$basename_chat.'-'.$i.'.log.html')) { |
||
| 585 | $i++; |
||
| 586 | } |
||
| 587 | |||
| 588 | @rename($chat_path.$basename_chat.'.log.html', $chat_path.$basename_chat.'-'.$i.'.log.html'); |
||
| 589 | @fclose(fopen($chat_path.$basename_chat.'.log.html', 'w')); |
||
| 590 | |||
| 591 | $doc_id = add_document( |
||
| 592 | $courseInfo, |
||
| 593 | $basepath_chat.'/'.$basename_chat.'-'.$i.'.log.html', |
||
| 594 | 'file', |
||
| 595 | filesize($chat_path.$basename_chat.'-'.$i.'.log.html'), |
||
| 596 | $basename_chat.'-'.$i.'.log.html', |
||
| 597 | null, |
||
| 598 | 0, |
||
| 599 | true, |
||
| 600 | 0, |
||
| 601 | 0, |
||
| 602 | 0, |
||
| 603 | false |
||
| 604 | ); |
||
| 605 | |||
| 606 | api_item_property_update( |
||
| 607 | $courseInfo, |
||
| 608 | TOOL_DOCUMENT, |
||
| 609 | $doc_id, |
||
| 610 | 'DocumentAdded', |
||
| 611 | $this->userId, |
||
| 612 | $group_info, |
||
| 613 | null, |
||
| 614 | null, |
||
| 615 | null, |
||
| 616 | $this->sessionId |
||
| 617 | ); |
||
| 618 | api_item_property_update( |
||
| 619 | $courseInfo, |
||
| 620 | TOOL_DOCUMENT, |
||
| 621 | $doc_id, |
||
| 622 | 'invisible', |
||
| 623 | $this->userId, |
||
| 624 | $group_info, |
||
| 625 | null, |
||
| 626 | null, |
||
| 627 | null, |
||
| 628 | $this->sessionId |
||
| 629 | ); |
||
| 630 | item_property_update_on_folder($courseInfo, $basepath_chat, $this->userId); |
||
| 631 | $doc_id = DocumentManager::get_document_id( |
||
| 632 | $courseInfo, |
||
| 633 | $basepath_chat.'/'.$basename_chat.'.log.html' |
||
| 634 | ); |
||
| 635 | update_existing_document($courseInfo, $doc_id, 0); |
||
| 636 | } |
||
| 637 | |||
| 638 | $remove = 0; |
||
| 639 | $content = []; |
||
| 640 | |||
| 641 | if (file_exists($chat_path.$basename_chat.'.log.html')) { |
||
| 642 | $content = file($chat_path.$basename_chat.'.log.html'); |
||
| 643 | $nbr_lines = sizeof($content); |
||
| 644 | $remove = $nbr_lines - 100; |
||
| 645 | } |
||
| 646 | |||
| 647 | if ($remove < 0) { |
||
| 648 | $remove = 0; |
||
| 649 | } |
||
| 650 | |||
| 651 | array_splice($content, 0, $remove); |
||
| 652 | |||
| 653 | if (isset($_GET['origin']) && $_GET['origin'] == 'whoisonline') { |
||
| 654 | //the caller |
||
| 655 | $content[0] = get_lang('CallSent').'<br />'.$content[0]; |
||
| 656 | } |
||
| 657 | |||
| 658 | $history = '<div id="content-chat">'; |
||
| 659 | foreach ($content as $this_line) { |
||
| 660 | $history .= $this_line; |
||
| 661 | } |
||
| 662 | $history .= '</div>'; |
||
| 663 | |||
| 664 | if ($isMaster || $GLOBALS['is_session_general_coach']) { |
||
| 665 | $history .= ' |
||
| 666 | <div id="clear-chat"> |
||
| 667 | <button type="button" id="chat-reset" class="btn btn-danger btn-sm"> |
||
| 668 | '.get_lang('ClearList').' |
||
| 669 | </button> |
||
| 670 | </div> |
||
| 671 | '; |
||
| 672 | } |
||
| 673 | |||
| 674 | return $history; |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Get the number of users connected in chat. |
||
| 679 | * |
||
| 680 | * @return int |
||
| 681 | */ |
||
| 682 | public function countUsersOnline() |
||
| 683 | { |
||
| 684 | $date = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 685 | $date->modify('-5 seconds'); |
||
| 686 | |||
| 687 | if ($this->groupId) { |
||
| 688 | $extraCondition = 'AND ccc.toGroupId = '.intval($this->groupId); |
||
| 689 | } else { |
||
| 690 | $extraCondition = 'AND ccc.sessionId = '.intval($this->sessionId); |
||
| 691 | } |
||
| 692 | |||
| 693 | $number = Database::getManager() |
||
| 694 | ->createQuery(" |
||
| 695 | SELECT COUNT(ccc.userId) FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 696 | WHERE ccc.lastConnection > :date AND ccc.cId = :course $extraCondition |
||
| 697 | ") |
||
| 698 | ->setParameters([ |
||
| 699 | 'date' => $date, |
||
| 700 | 'course' => $this->courseId, |
||
| 701 | ]) |
||
| 702 | ->getSingleScalarResult(); |
||
| 703 | |||
| 704 | return (int) $number; |
||
| 705 | } |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Get the users online data. |
||
| 709 | * |
||
| 710 | * @return array |
||
| 711 | */ |
||
| 712 | public function listUsersOnline() |
||
| 713 | { |
||
| 714 | $subscriptions = $this->getUsersSubscriptions(); |
||
| 715 | $usersInfo = []; |
||
| 716 | /** @var CourseRelUser $subscription */ |
||
| 717 | foreach ($subscriptions as $subscription) { |
||
| 718 | $user = $subscription->getUser(); |
||
| 719 | $usersInfo[] = [ |
||
| 720 | 'id' => $user->getId(), |
||
| 721 | 'firstname' => $user->getFirstname(), |
||
| 722 | 'lastname' => $user->getLastname(), |
||
| 723 | 'status' => !$this->sessionId ? $subscription->getStatus() : $user->getStatus(), |
||
| 724 | 'image_url' => UserManager::getUserPicture($user->getId(), USER_IMAGE_SIZE_MEDIUM), |
||
| 725 | 'profile_url' => api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user->getId(), |
||
| 726 | 'complete_name' => $user->getCompleteName(), |
||
| 727 | 'username' => $user->getUsername(), |
||
| 728 | 'email' => $user->getEmail(), |
||
| 729 | 'isConnected' => $this->userIsConnected($user->getId()), |
||
| 730 | ]; |
||
| 731 | } |
||
| 732 | |||
| 733 | return $usersInfo; |
||
| 734 | } |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Get the users subscriptions (SessionRelCourseRelUser array or CourseRelUser array) for chat. |
||
| 738 | * |
||
| 739 | * @throws \Doctrine\ORM\ORMException |
||
| 740 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 741 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
| 742 | * |
||
| 743 | * @return \Doctrine\Common\Collections\ArrayCollection |
||
| 744 | */ |
||
| 745 | private function getUsersSubscriptions() |
||
| 777 | } |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Check if a user is connected in course chat. |
||
| 781 | * |
||
| 782 | * @param int $userId |
||
| 783 | * |
||
| 784 | * @return int |
||
| 785 | */ |
||
| 786 | private function userIsConnected($userId) |
||
| 810 | } |
||
| 811 | } |
||
| 812 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: