| Conditions | 19 |
| Paths | 39 |
| Total Lines | 127 |
| Code Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 326 | public static function readCourse($filename, $delete = false) |
||
| 327 | { |
||
| 328 | self::cleanBackupDir(); |
||
| 329 | // Create a temp directory |
||
| 330 | $tmp_dir_name = 'CourseArchiver_'.uniqid(''); |
||
| 331 | $unzip_dir = self::getBackupDir().$tmp_dir_name; |
||
| 332 | $filePath = self::getBackupDir().$filename; |
||
| 333 | |||
| 334 | $perms = api_get_permissions_for_new_directories(); |
||
| 335 | |||
| 336 | if (!is_dir($unzip_dir) && !@mkdir($unzip_dir, $perms, true)) { |
||
| 337 | error_log('[COURSE_ARCHIVER] readCourse: failed to create unzip_dir="'.$unzip_dir.'"'); |
||
| 338 | return new Course(); |
||
| 339 | } |
||
| 340 | |||
| 341 | if (!is_file($filePath)) { |
||
| 342 | error_log('[COURSE_ARCHIVER] readCourse: backup zip not found filePath="'.$filePath.'"'); |
||
| 343 | return new Course(); |
||
| 344 | } |
||
| 345 | |||
| 346 | if (!@copy($filePath, $unzip_dir.'/backup.zip')) { |
||
| 347 | error_log('[COURSE_ARCHIVER] readCourse: failed to copy zip filePath="'.$filePath.'" to "'.$unzip_dir.'/backup.zip"'); |
||
| 348 | return new Course(); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Unzip the archive |
||
| 352 | $zip = new \PclZip($unzip_dir.'/backup.zip'); |
||
| 353 | |||
| 354 | if (!@chdir($unzip_dir)) { |
||
| 355 | error_log('[COURSE_ARCHIVER] readCourse: chdir failed unzip_dir="'.$unzip_dir.'"'); |
||
| 356 | return new Course(); |
||
| 357 | } |
||
| 358 | |||
| 359 | // For course backups we must preserve original filenames so that |
||
| 360 | // paths in course_info.dat still match the files in backup_path. |
||
| 361 | $extractResult = $zip->extract(PCLZIP_OPT_TEMP_FILE_ON); |
||
| 362 | |||
| 363 | if ($extractResult === 0) { |
||
| 364 | error_log('[COURSE_ARCHIVER] readCourse: extract failed error="'.$zip->errorInfo(true).'" unzip_dir="'.$unzip_dir.'"'); |
||
| 365 | return new Course(); |
||
| 366 | } |
||
| 367 | |||
| 368 | // Remove the archive-file |
||
| 369 | if ($delete) { |
||
| 370 | @unlink($filePath); |
||
| 371 | } |
||
| 372 | |||
| 373 | // Read the course |
||
| 374 | if (!is_file('course_info.dat')) { |
||
| 375 | error_log('[COURSE_ARCHIVER] readCourse: missing course_info.dat cwd="'.getcwd().'" unzip_dir="'.$unzip_dir.'"'); |
||
| 376 | return new Course(); |
||
| 377 | } |
||
| 378 | |||
| 379 | $size = (int) @filesize('course_info.dat'); |
||
| 380 | if ($size <= 0) { |
||
| 381 | error_log('[COURSE_ARCHIVER] readCourse: empty course_info.dat size='.$size.' cwd="'.getcwd().'"'); |
||
| 382 | return new Course(); |
||
| 383 | } |
||
| 384 | |||
| 385 | $fp = @fopen('course_info.dat', 'r'); |
||
| 386 | if (false === $fp) { |
||
| 387 | error_log('[COURSE_ARCHIVER] readCourse: failed to open course_info.dat cwd="'.getcwd().'"'); |
||
| 388 | return new Course(); |
||
| 389 | } |
||
| 390 | |||
| 391 | $contents = @fread($fp, $size); |
||
| 392 | @fclose($fp); |
||
| 393 | |||
| 394 | $readLen = is_string($contents) ? strlen($contents) : -1; |
||
| 395 | if (!is_string($contents) || $readLen <= 0) { |
||
| 396 | error_log('[COURSE_ARCHIVER] readCourse: failed to read course_info.dat'); |
||
| 397 | return new Course(); |
||
| 398 | } |
||
| 399 | |||
| 400 | // Backward compatibility aliases used by serialized payloads |
||
| 401 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Course', 'Course'); |
||
| 402 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Announcement', 'Announcement'); |
||
| 403 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Attendance', 'Attendance'); |
||
| 404 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\CalendarEvent', 'CalendarEvent'); |
||
| 405 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyLearnpath', 'CourseCopyLearnpath'); |
||
| 406 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseCopyTestCategory', 'CourseCopyTestCategory'); |
||
| 407 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseDescription', 'CourseDescription'); |
||
| 408 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\CourseSession', 'CourseSession'); |
||
| 409 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Document', 'Document'); |
||
| 410 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Forum', 'Forum'); |
||
| 411 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumCategory', 'ForumCategory'); |
||
| 412 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumPost', 'ForumPost'); |
||
| 413 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\ForumTopic', 'ForumTopic'); |
||
| 414 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Glossary', 'Glossary'); |
||
| 415 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\GradeBookBackup', 'GradeBookBackup'); |
||
| 416 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Link', 'Link'); |
||
| 417 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\LinkCategory', 'LinkCategory'); |
||
| 418 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Quiz', 'Quiz'); |
||
| 419 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestion', 'QuizQuestion'); |
||
| 420 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\QuizQuestionOption', 'QuizQuestionOption'); |
||
| 421 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\ScormDocument', 'ScormDocument'); |
||
| 422 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Survey', 'Survey'); |
||
| 423 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\SurveyInvitation', 'SurveyInvitation'); |
||
| 424 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\SurveyQuestion', 'SurveyQuestion'); |
||
| 425 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Thematic', 'Thematic'); |
||
| 426 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\ToolIntro', 'ToolIntro'); |
||
| 427 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Wiki', 'Wiki'); |
||
| 428 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\Work', 'Work'); |
||
| 429 | class_alias('Chamilo\CourseBundle\Component\CourseCopy\Resources\XapiTool', 'XapiTool'); |
||
| 430 | |||
| 431 | $decoded = base64_decode($contents, true); |
||
| 432 | if (false === $decoded) { |
||
| 433 | error_log('[COURSE_ARCHIVER] readCourse: base64_decode strict failed, retry non-strict'); |
||
| 434 | $decoded = base64_decode($contents); |
||
| 435 | } |
||
| 436 | |||
| 437 | if (!is_string($decoded) || $decoded === '') { |
||
| 438 | error_log('[COURSE_ARCHIVER] readCourse: base64_decode produced empty payload'); |
||
| 439 | return new Course(); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** @var mixed $course */ |
||
| 443 | $course = \UnserializeApi::unserialize('course', $decoded); |
||
| 444 | if (!is_object($course) || !in_array(get_class($course), ['Course', 'Chamilo\CourseBundle\Component\CourseCopy\Course'], true)) { |
||
| 445 | error_log('[COURSE_ARCHIVER] readCourse: invalid class after unserialize, returning empty Course'); |
||
| 446 | return new Course(); |
||
| 447 | } |
||
| 448 | |||
| 449 | // Ensure backup_path is always set when unserialize is successful |
||
| 450 | $course->backup_path = $unzip_dir; |
||
| 451 | |||
| 452 | return $course; |
||
| 453 | } |
||
| 455 |