| Total Complexity | 66 |
| Total Lines | 706 |
| 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 |
||
| 25 | class CourseChatUtils |
||
| 26 | { |
||
| 27 | private $groupId; |
||
| 28 | private $courseId; |
||
| 29 | private $sessionId; |
||
| 30 | private $userId; |
||
| 31 | private $resourceNode; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * CourseChat constructor. |
||
| 35 | * |
||
| 36 | * @param int $courseId |
||
| 37 | * @param int $userId |
||
| 38 | * @param int $sessionId |
||
| 39 | * @param int $groupId |
||
| 40 | */ |
||
| 41 | public function __construct($courseId, $userId, $sessionId = 0, $groupId = 0, ResourceNode $resourceNode, ResourceRepository $repository) |
||
| 42 | { |
||
| 43 | $this->courseId = (int) $courseId; |
||
| 44 | $this->userId = (int) $userId; |
||
| 45 | $this->sessionId = (int) $sessionId; |
||
| 46 | $this->groupId = (int) $groupId; |
||
| 47 | $this->resourceNode = $resourceNode; |
||
| 48 | $this->repository = $repository; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Prepare a message. Clean and insert emojis. |
||
| 53 | * |
||
| 54 | * @param string $message The message to prepare |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function prepareMessage($message) |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Save a chat message in a HTML file. |
||
| 94 | * |
||
| 95 | * @param string $message |
||
| 96 | * @param int $friendId |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function saveMessage($message, $friendId = 0) |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Disconnect a user from course chats. |
||
| 186 | * |
||
| 187 | * @param int $userId |
||
| 188 | */ |
||
| 189 | public static function exitChat($userId) |
||
| 190 | { |
||
| 191 | $listCourse = CourseManager::get_courses_list_by_user_id($userId); |
||
| 192 | |||
| 193 | foreach ($listCourse as $course) { |
||
| 194 | Database::getManager() |
||
| 195 | ->createQuery(' |
||
| 196 | DELETE FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 197 | WHERE ccc.cId = :course AND ccc.userId = :user |
||
| 198 | ') |
||
| 199 | ->execute([ |
||
| 200 | 'course' => intval($course['real_id']), |
||
| 201 | 'user' => intval($userId), |
||
| 202 | ]); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Disconnect users who are more than 5 seconds inactive. |
||
| 208 | */ |
||
| 209 | public function disconnectInactiveUsers() |
||
| 210 | { |
||
| 211 | $em = Database::getManager(); |
||
| 212 | $extraCondition = "AND ccc.toGroupId = {$this->groupId}"; |
||
| 213 | if (empty($this->groupId)) { |
||
| 214 | $extraCondition = "AND ccc.sessionId = {$this->sessionId}"; |
||
| 215 | } |
||
| 216 | |||
| 217 | $connectedUsers = $em |
||
| 218 | ->createQuery(" |
||
| 219 | SELECT ccc FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 220 | WHERE ccc.cId = :course $extraCondition |
||
| 221 | ") |
||
| 222 | ->setParameter('course', $this->courseId) |
||
| 223 | ->getResult(); |
||
| 224 | |||
| 225 | $now = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 226 | $cd_count_time_seconds = $now->getTimestamp(); |
||
| 227 | /** @var CChatConnected $connection */ |
||
| 228 | foreach ($connectedUsers as $connection) { |
||
| 229 | $date_count_time_seconds = $connection->getLastConnection()->getTimestamp(); |
||
| 230 | if (0 !== strcmp($now->format('Y-m-d'), $connection->getLastConnection()->format('Y-m-d'))) { |
||
| 231 | continue; |
||
| 232 | } |
||
| 233 | |||
| 234 | if (($cd_count_time_seconds - $date_count_time_seconds) <= 5) { |
||
| 235 | continue; |
||
| 236 | } |
||
| 237 | |||
| 238 | $em |
||
| 239 | ->createQuery(' |
||
| 240 | DELETE FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 241 | WHERE ccc.cId = :course AND ccc.userId = :user AND ccc.toGroupId = :group |
||
| 242 | ') |
||
| 243 | ->execute([ |
||
| 244 | 'course' => $this->courseId, |
||
| 245 | 'user' => $connection->getUserId(), |
||
| 246 | 'group' => $this->groupId, |
||
| 247 | ]); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Keep registered to a user as connected. |
||
| 253 | */ |
||
| 254 | public function keepUserAsConnected() |
||
| 255 | { |
||
| 256 | $em = Database::getManager(); |
||
| 257 | $extraCondition = null; |
||
| 258 | |||
| 259 | if ($this->groupId) { |
||
| 260 | $extraCondition = 'AND ccc.toGroupId = '.$this->groupId; |
||
| 261 | } else { |
||
| 262 | $extraCondition = 'AND ccc.sessionId = '.$this->sessionId; |
||
| 263 | } |
||
| 264 | |||
| 265 | $currentTime = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 266 | |||
| 267 | /** @var CChatConnected $connection */ |
||
| 268 | $connection = $em |
||
| 269 | ->createQuery(" |
||
| 270 | SELECT ccc FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 271 | WHERE ccc.userId = :user AND ccc.cId = :course $extraCondition |
||
| 272 | ") |
||
| 273 | ->setParameters([ |
||
| 274 | 'user' => $this->userId, |
||
| 275 | 'course' => $this->courseId, |
||
| 276 | ]) |
||
| 277 | ->getOneOrNullResult(); |
||
| 278 | |||
| 279 | if ($connection) { |
||
| 280 | $connection->setLastConnection($currentTime); |
||
| 281 | $em->persist($connection); |
||
| 282 | $em->flush(); |
||
| 283 | |||
| 284 | return; |
||
| 285 | } |
||
| 286 | |||
| 287 | $connection = new CChatConnected(); |
||
| 288 | $connection |
||
| 289 | ->setCId($this->courseId) |
||
| 290 | ->setUserId($this->userId) |
||
| 291 | ->setLastConnection($currentTime) |
||
| 292 | ->setSessionId($this->sessionId) |
||
| 293 | ->setToGroupId($this->groupId); |
||
| 294 | |||
| 295 | $em->persist($connection); |
||
| 296 | $em->flush(); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get the emoji allowed on course chat. |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | /*public static function getEmojiStrategy() |
||
| 305 | { |
||
| 306 | return require_once api_get_path(SYS_CODE_PATH).'chat/emoji_strategy.php'; |
||
| 307 | }*/ |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get the emoji list to include in chat. |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public static function getEmojisToInclude() |
||
| 315 | { |
||
| 316 | return [ |
||
| 317 | ':bowtie:', |
||
| 318 | ':smile:' | |
||
| 319 | ':laughing:', |
||
| 320 | ':blush:', |
||
| 321 | ':smiley:', |
||
| 322 | ':relaxed:', |
||
| 323 | ':smirk:', |
||
| 324 | ':heart_eyes:', |
||
| 325 | ':kissing_heart:', |
||
| 326 | ':kissing_closed_eyes:', |
||
| 327 | ':flushed:', |
||
| 328 | ':relieved:', |
||
| 329 | ':satisfied:', |
||
| 330 | ':grin:', |
||
| 331 | ':wink:', |
||
| 332 | ':stuck_out_tongue_winking_eye:', |
||
| 333 | ':stuck_out_tongue_closed_eyes:', |
||
| 334 | ':grinning:', |
||
| 335 | ':kissing:', |
||
| 336 | ':kissing_smiling_eyes:', |
||
| 337 | ':stuck_out_tongue:', |
||
| 338 | ':sleeping:', |
||
| 339 | ':worried:', |
||
| 340 | ':frowning:', |
||
| 341 | ':anguished:', |
||
| 342 | ':open_mouth:', |
||
| 343 | ':grimacing:', |
||
| 344 | ':confused:', |
||
| 345 | ':hushed:', |
||
| 346 | ':expressionless:', |
||
| 347 | ':unamused:', |
||
| 348 | ':sweat_smile:', |
||
| 349 | ':sweat:', |
||
| 350 | ':disappointed_relieved:', |
||
| 351 | ':weary:', |
||
| 352 | ':pensive:', |
||
| 353 | ':disappointed:', |
||
| 354 | ':confounded:', |
||
| 355 | ':fearful:', |
||
| 356 | ':cold_sweat:', |
||
| 357 | ':persevere:', |
||
| 358 | ':cry:', |
||
| 359 | ':sob:', |
||
| 360 | ':joy:', |
||
| 361 | ':astonished:', |
||
| 362 | ':scream:', |
||
| 363 | ':neckbeard:', |
||
| 364 | ':tired_face:', |
||
| 365 | ':angry:', |
||
| 366 | ':rage:', |
||
| 367 | ':triumph:', |
||
| 368 | ':sleepy:', |
||
| 369 | ':yum:', |
||
| 370 | ':mask:', |
||
| 371 | ':sunglasses:', |
||
| 372 | ':dizzy_face:', |
||
| 373 | ':imp:', |
||
| 374 | ':smiling_imp:', |
||
| 375 | ':neutral_face:', |
||
| 376 | ':no_mouth:', |
||
| 377 | ':innocent:', |
||
| 378 | ':alien:', |
||
| 379 | ]; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the chat history file name. |
||
| 384 | * |
||
| 385 | * @param bool $absolute Optional. Whether get the base or the absolute file path |
||
| 386 | * @param int $friendId optional |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function getFileName($absolute = false, $friendId = 0) |
||
| 391 | { |
||
| 392 | $date = date('Y-m-d'); |
||
| 393 | $base = 'messages-'.$date.'.log.html'; |
||
| 394 | |||
| 395 | if ($this->groupId && !$friendId) { |
||
| 396 | $base = 'messages-'.$date.'_gid-'.$this->groupId.'.log.html'; |
||
| 397 | } elseif ($this->sessionId && !$friendId) { |
||
| 398 | $base = 'messages-'.$date.'_sid-'.$this->sessionId.'.log.html'; |
||
| 399 | } elseif ($friendId) { |
||
| 400 | if ($this->userId < $friendId) { |
||
| 401 | $base = 'messages-'.$date.'_uid-'.$this->userId.'-'.$friendId.'.log.html'; |
||
| 402 | } else { |
||
| 403 | $base = 'messages-'.$date.'_uid-'.$friendId.'-'.$this->userId.'.log.html'; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | if (!$absolute) { |
||
| 408 | return $base; |
||
| 409 | } |
||
| 410 | |||
| 411 | //$courseInfo = api_get_course_info_by_id($this->courseId); |
||
| 412 | |||
| 413 | $document_path = '/document'; |
||
| 414 | $chatPath = $document_path.'/chat_files/'; |
||
| 415 | |||
| 416 | if ($this->groupId) { |
||
| 417 | $group_info = GroupManager::get_group_properties($this->groupId); |
||
| 418 | $chatPath = $document_path.$group_info['directory'].'/chat_files/'; |
||
| 419 | } |
||
| 420 | |||
| 421 | return $chatPath.$base; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Get the chat history. |
||
| 426 | * |
||
| 427 | * @param bool $reset |
||
| 428 | * @param int $friendId optional |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function readMessages($reset = false, $friendId = 0) |
||
| 433 | { |
||
| 434 | $courseInfo = api_get_course_info_by_id($this->courseId); |
||
| 435 | $date_now = date('Y-m-d'); |
||
| 436 | $isMaster = (bool) api_is_course_admin(); |
||
| 437 | //$basepath_chat = '/chat_files'; |
||
| 438 | //$document_path = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; |
||
| 439 | if ($this->groupId) { |
||
| 440 | $group_info = GroupManager:: get_group_properties($this->groupId); |
||
| 441 | //$basepath_chat = $group_info['directory'].'/chat_files'; |
||
| 442 | } |
||
| 443 | |||
| 444 | //$chat_path = $document_path.$basepath_chat.'/'; |
||
| 445 | |||
| 446 | $filename_chat = 'messages-'.$date_now.'-log.html'; |
||
| 447 | |||
| 448 | if ($this->groupId && !$friendId) { |
||
| 449 | $filename_chat = 'messages-'.$date_now.'_gid-'.$this->groupId.'-log.html'; |
||
| 450 | } elseif ($this->sessionId && !$friendId) { |
||
| 451 | $filename_chat = 'messages-'.$date_now.'_sid-'.$this->sessionId.'-log.html'; |
||
| 452 | } elseif ($friendId) { |
||
| 453 | if ($this->userId < $friendId) { |
||
| 454 | $filename_chat = 'messages-'.$date_now.'_uid-'.$this->userId.'-'.$friendId.'-log.html'; |
||
| 455 | } else { |
||
| 456 | $filename_chat = 'messages-'.$date_now.'_uid-'.$friendId.'-'.$this->userId.'-log.html'; |
||
| 457 | } |
||
| 458 | } |
||
| 459 | |||
| 460 | $criteria = [ |
||
| 461 | 'slug' => $filename_chat, |
||
| 462 | 'parent' => $this->resourceNode, |
||
| 463 | ]; |
||
| 464 | |||
| 465 | $resourceNode = $this->repository->getResourceNodeRepository()->findOneBy($criteria); |
||
| 466 | |||
| 467 | /** @var ResourceNode $resourceNode */ |
||
| 468 | //$resourceNode = $this->repository->findOneBy($criteria); |
||
| 469 | //var_dump($filename_chat, $this->resourceNode->getId());exit; |
||
| 470 | |||
| 471 | if (null === $resourceNode) { |
||
| 472 | $em = Database::getManager(); |
||
| 473 | $resource = new CChatConversation(); |
||
| 474 | $resource->setName($filename_chat); |
||
| 475 | |||
| 476 | $handle = tmpfile(); |
||
| 477 | fwrite($handle, ''); |
||
| 478 | $meta = stream_get_meta_data($handle); |
||
| 479 | $file = new UploadedFile($meta['uri'], $filename_chat, 'text/html', null, true); |
||
| 480 | |||
| 481 | $this->repository->addResourceToCourse( |
||
| 482 | $resource, |
||
| 483 | ResourceLink::VISIBILITY_PUBLISHED, |
||
| 484 | api_get_user_entity(api_get_user_id()), |
||
| 485 | api_get_course_entity(), |
||
| 486 | api_get_session_entity(), |
||
| 487 | api_get_group_entity(), |
||
| 488 | $file |
||
| 489 | ); |
||
| 490 | $em->flush(); |
||
| 491 | |||
| 492 | $resourceNode = $resource->getResourceNode(); |
||
| 493 | } |
||
| 494 | |||
| 495 | if ($resourceNode->hasResourceFile()) { |
||
| 496 | //$resourceFile = $resourceNode->getResourceFile(); |
||
| 497 | //$fileName = $this->getFilename($resourceFile); |
||
| 498 | return $this->repository->getResourceNodeFileContent($resourceNode); |
||
| 499 | } |
||
| 500 | |||
| 501 | return ''; |
||
| 502 | |||
| 503 | |||
| 504 | $remove = 0; |
||
| 505 | $content = []; |
||
| 506 | |||
| 507 | if (file_exists($chat_path.$basename_chat.'.log.html')) { |
||
| 508 | $content = file($chat_path.$basename_chat.'.log.html'); |
||
| 509 | $nbr_lines = sizeof($content); |
||
| 510 | $remove = $nbr_lines - 100; |
||
| 511 | } |
||
| 512 | |||
| 513 | if ($remove < 0) { |
||
| 514 | $remove = 0; |
||
| 515 | } |
||
| 516 | |||
| 517 | array_splice($content, 0, $remove); |
||
| 518 | |||
| 519 | if (isset($_GET['origin']) && 'whoisonline' == $_GET['origin']) { |
||
| 520 | //the caller |
||
| 521 | $content[0] = get_lang('Chat call has been sent. Waiting for the approval of your partner.').'<br />'.$content[0]; |
||
| 522 | } |
||
| 523 | |||
| 524 | $history = '<div id="content-chat">'; |
||
| 525 | foreach ($content as $this_line) { |
||
| 526 | $history .= $this_line; |
||
| 527 | } |
||
| 528 | $history .= '</div>'; |
||
| 529 | |||
| 530 | if ($isMaster || $GLOBALS['is_session_general_coach']) { |
||
| 531 | $history .= ' |
||
| 532 | <div id="clear-chat"> |
||
| 533 | <button type="button" id="chat-reset" class="btn btn-danger btn-sm"> |
||
| 534 | '.get_lang('Clear the chat').' |
||
| 535 | </button> |
||
| 536 | </div> |
||
| 537 | '; |
||
| 538 | } |
||
| 539 | |||
| 540 | return $history; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Get the number of users connected in chat. |
||
| 545 | * |
||
| 546 | * @return int |
||
| 547 | */ |
||
| 548 | public function countUsersOnline() |
||
| 549 | { |
||
| 550 | $date = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC')); |
||
| 551 | $date->modify('-5 seconds'); |
||
| 552 | |||
| 553 | if ($this->groupId) { |
||
| 554 | $extraCondition = 'AND ccc.toGroupId = '.$this->groupId; |
||
| 555 | } else { |
||
| 556 | $extraCondition = 'AND ccc.sessionId = '.$this->sessionId; |
||
| 557 | } |
||
| 558 | |||
| 559 | $number = Database::getManager() |
||
| 560 | ->createQuery(" |
||
| 561 | SELECT COUNT(ccc.userId) FROM ChamiloCourseBundle:CChatConnected ccc |
||
| 562 | WHERE ccc.lastConnection > :date AND ccc.cId = :course $extraCondition |
||
| 563 | ") |
||
| 564 | ->setParameters([ |
||
| 565 | 'date' => $date, |
||
| 566 | 'course' => $this->courseId, |
||
| 567 | ]) |
||
| 568 | ->getSingleScalarResult(); |
||
| 569 | |||
| 570 | return (int) $number; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get the users online data. |
||
| 575 | * |
||
| 576 | * @return array |
||
| 577 | */ |
||
| 578 | public function listUsersOnline() |
||
| 579 | { |
||
| 580 | $subscriptions = $this->getUsersSubscriptions(); |
||
| 581 | $usersInfo = []; |
||
| 582 | |||
| 583 | if ($this->groupId) { |
||
| 584 | /** @var User $groupUser */ |
||
| 585 | foreach ($subscriptions as $groupUser) { |
||
| 586 | $usersInfo[] = $this->formatUser( |
||
| 587 | $groupUser, |
||
| 588 | $groupUser->getStatus() |
||
| 589 | ); |
||
| 590 | } |
||
| 591 | } else { |
||
| 592 | /** @var CourseRelUser|SessionRelCourseRelUser $subscription */ |
||
| 593 | foreach ($subscriptions as $subscription) { |
||
| 594 | $user = $subscription->getUser(); |
||
| 595 | $usersInfo[] = $this->formatUser( |
||
| 596 | $user, |
||
| 597 | $this->sessionId ? $user->getStatus() : $subscription->getStatus() |
||
| 598 | ); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | return $usersInfo; |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Format the user data to return it in the user list. |
||
| 607 | * |
||
| 608 | * @param int $status |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | private function formatUser(User $user, $status) |
||
| 613 | { |
||
| 614 | return [ |
||
| 615 | 'id' => $user->getId(), |
||
| 616 | 'firstname' => $user->getFirstname(), |
||
| 617 | 'lastname' => $user->getLastname(), |
||
| 618 | 'status' => $status, |
||
| 619 | 'image_url' => UserManager::getUserPicture($user->getId(), USER_IMAGE_SIZE_MEDIUM), |
||
| 620 | 'profile_url' => api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user->getId(), |
||
| 621 | 'complete_name' => UserManager::formatUserFullName($user), |
||
| 622 | 'username' => $user->getUsername(), |
||
| 623 | 'email' => $user->getEmail(), |
||
| 624 | 'isConnected' => $this->userIsConnected($user->getId()), |
||
| 625 | ]; |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Get the users subscriptions (SessionRelCourseRelUser array or CourseRelUser array) for chat. |
||
| 630 | * |
||
| 631 | * @return ArrayCollection |
||
| 632 | */ |
||
| 633 | private function getUsersSubscriptions() |
||
| 697 | }); |
||
| 698 | } |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Check if a user is connected in course chat. |
||
| 702 | * |
||
| 703 | * @param int $userId |
||
| 704 | * |
||
| 705 | * @return int |
||
| 706 | */ |
||
| 707 | private function userIsConnected($userId) |
||
| 731 | } |
||
| 732 | } |
||
| 733 |
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: