| Total Complexity | 72 |
| Total Lines | 408 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Cc13Resource 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 Cc13Resource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Cc13Resource extends Cc13Entities |
||
| 25 | { |
||
| 26 | public function generateData($resource_type) |
||
| 27 | { |
||
| 28 | $data = []; |
||
| 29 | if (!empty(Cc1p3Convert::$instances['instances'][$resource_type])) { |
||
| 30 | foreach (Cc1p3Convert::$instances['instances'][$resource_type] as $instance) { |
||
| 31 | $data[] = $this->getResourceData($instance); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | return $data; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Store web links using Doctrine entities (Chamilo 2 style). |
||
| 40 | * |
||
| 41 | * @param mixed $links |
||
| 42 | */ |
||
| 43 | public function storeLinks($links): bool |
||
| 44 | { |
||
| 45 | if (empty($links)) { |
||
| 46 | return true; |
||
| 47 | } |
||
| 48 | |||
| 49 | $em = Database::getManager(); |
||
| 50 | $course = api_get_course_entity(api_get_course_int_id()); |
||
| 51 | $session = api_get_session_entity((int) api_get_session_id()); |
||
| 52 | |||
| 53 | foreach ($links as $link) { |
||
| 54 | $title = trim(htmlspecialchars_decode((string) ($link[1] ?? ''), ENT_QUOTES)) ?: (string) ($link[4] ?? ''); |
||
| 55 | $url = trim((string) ($link[4] ?? '')); |
||
| 56 | if ('' === $url) { |
||
| 57 | Cc1p3Convert::logAction('storeLinks: empty URL skipped', ['title' => $title]); |
||
| 58 | |||
| 59 | continue; |
||
| 60 | } |
||
| 61 | |||
| 62 | // Basic sanity check (best-effort). |
||
| 63 | if (!self::validateUrlSyntax($url, 's+')) { |
||
| 64 | $try = rawurldecode($url); |
||
| 65 | if (self::validateUrlSyntax($try, 's+')) { |
||
| 66 | $url = $try; |
||
| 67 | } else { |
||
| 68 | Cc1p3Convert::logAction('storeLinks: invalid URL skipped', ['url' => $url]); |
||
| 69 | |||
| 70 | continue; // Skip invalid URL instead of creating a broken entity. |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | Cc1p3Convert::logAction('storeLinks: creating link', ['title' => $title, 'url' => $url]); |
||
| 75 | |||
| 76 | $entity = (new CLink()) |
||
| 77 | ->setUrl($url) |
||
| 78 | ->setTitle($title) |
||
| 79 | ->setDescription('') |
||
| 80 | ->setTarget('_blank') |
||
| 81 | ->setParent($course) |
||
| 82 | ->addCourseLink($course, $session) |
||
| 83 | ; |
||
| 84 | |||
| 85 | $em->persist($entity); |
||
| 86 | } |
||
| 87 | $em->flush(); |
||
| 88 | |||
| 89 | return true; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Store Documents from CC package. No SYS_COURSE_PATH. Uses DocumentManager + ResourceFile. |
||
| 94 | * |
||
| 95 | * @param array $documents Items from getResourceData() |
||
| 96 | * @param string $packageRoot Absolute path to the extracted package (directory of imsmanifest.xml) |
||
| 97 | */ |
||
| 98 | public function storeDocuments(array $documents, string $packageRoot): bool |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Build normalized resource tuple for the importer pipeline. |
||
| 290 | * Returns: |
||
| 291 | * [0]=instance, [1]=title, [2]=type('file'|'html'), [3]=html, [4]=href, [5]=options, [6]=baseDir(html only). |
||
| 292 | * |
||
| 293 | * @param mixed $instance |
||
| 294 | */ |
||
| 295 | public function getResourceData($instance) |
||
| 296 | { |
||
| 297 | $xpath = Cc1p3Convert::newxPath(Cc1p3Convert::$manifest, Cc1p3Convert::$namespaces); |
||
| 298 | $link = ''; |
||
| 299 | $baseDir = ''; |
||
| 300 | |||
| 301 | if ( |
||
| 302 | Cc1p3Convert::CC_TYPE_WEBCONTENT == $instance['common_cartridge_type'] |
||
| 303 | || Cc1p3Convert::CC_TYPE_ASSOCIATED_CONTENT == $instance['common_cartridge_type'] |
||
| 304 | ) { |
||
| 305 | $resource = $xpath->query( |
||
| 306 | '/imscc:manifest/imscc:resources/imscc:resource[@identifier="'.$instance['resource_identifier'].'"]/@href' |
||
| 307 | ); |
||
| 308 | if ($resource->length > 0) { |
||
| 309 | $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : ''; |
||
| 310 | } else { |
||
| 311 | $resource = ''; |
||
| 312 | } |
||
| 313 | |||
| 314 | if (empty($resource)) { |
||
| 315 | // Fallback: use src set in CcBase::createInstances() from <file href="..."> |
||
| 316 | $resource = $instance['src']; |
||
| 317 | } |
||
| 318 | if (!empty($resource)) { |
||
| 319 | $link = $resource; |
||
| 320 | $baseDir = trim(\dirname($resource), '.\/'); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | if (Cc1p3Convert::CC_TYPE_WEBLINK == $instance['common_cartridge_type']) { |
||
| 325 | $external_resource = $instance['src']; |
||
| 326 | if (!empty($external_resource)) { |
||
| 327 | $resourceDoc = $this->loadXmlResource( |
||
| 328 | Cc1p3Convert::$pathToManifestFolder.DIRECTORY_SEPARATOR.$external_resource |
||
| 329 | ); |
||
| 330 | |||
| 331 | if (!empty($resourceDoc)) { |
||
| 332 | // Namespace-agnostic: webLink/url with any NS |
||
| 333 | $x = new DOMXPath($resourceDoc); |
||
| 334 | $href = $x->query('/*[local-name()="webLink"]/*[local-name()="url"]/@href'); |
||
| 335 | if ($href->length > 0) { |
||
| 336 | $raw = trim((string) $href->item(0)->nodeValue); |
||
| 337 | Cc1p3Convert::logAction('getResourceData: webLink extracted', ['raw' => $raw]); |
||
| 338 | |||
| 339 | if (!self::validateUrlSyntax($raw, 's+')) { |
||
| 340 | $changed = rawurldecode($raw); |
||
| 341 | if (self::validateUrlSyntax($changed, 's+')) { |
||
| 342 | $link = $changed; |
||
| 343 | } else { |
||
| 344 | Cc1p3Convert::logAction('getResourceData: invalid webLink URL', ['raw' => $raw]); |
||
| 345 | $link = 'http://invalidurldetected/'; |
||
| 346 | } |
||
| 347 | } else { |
||
| 348 | $link = htmlspecialchars($raw, ENT_COMPAT, 'UTF-8', false); |
||
| 349 | } |
||
| 350 | } else { |
||
| 351 | Cc1p3Convert::logAction('getResourceData: webLink href not found via XPath(local-name())', ['file' => $external_resource]); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | // Decide type: file vs html |
||
| 358 | $type = 'file'; |
||
| 359 | $htmlContent = ''; |
||
| 360 | $options = ''; |
||
| 361 | |||
| 362 | if (!empty($link) && (Cc1p3Convert::CC_TYPE_WEBCONTENT == $instance['common_cartridge_type'])) { |
||
| 363 | $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION) ?: ''); |
||
| 364 | if (\in_array($ext, ['html', 'htm', 'xhtml'], true)) { |
||
| 365 | $type = 'html'; |
||
| 366 | |||
| 367 | $root = realpath(Cc1p3Convert::$pathToManifestFolder); |
||
| 368 | $abs = $root ? realpath($root.DIRECTORY_SEPARATOR.$link) : false; |
||
| 369 | |||
| 370 | if ($abs && is_file($abs)) { |
||
| 371 | // Read HTML and strip outer wrappers; keep relative URLs as-is. |
||
| 372 | $raw = (string) @file_get_contents($abs); |
||
| 373 | $htmlContent = self::safexml($this->prepareContent($raw)); |
||
| 374 | // For inline HTML we clear the href; the storage path is decided in storeDocuments. |
||
| 375 | $link = ''; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | return [ |
||
| 381 | $instance['instance'], |
||
| 382 | self::safexml($instance['title'] ?: ($link ? basename($link) : '')), |
||
| 383 | $type, |
||
| 384 | $htmlContent, |
||
| 385 | $link, |
||
| 386 | $options, |
||
| 387 | $baseDir, |
||
| 388 | ]; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Simple URL validator. |
||
| 393 | * $mode: |
||
| 394 | * - 's+' => require scheme (http/https) and validate full URL |
||
| 395 | * - 's*' => scheme optional (rarely used here). |
||
| 396 | */ |
||
| 397 | private static function validateUrlSyntax(string $url, string $mode = 's+'): bool |
||
| 432 | } |
||
| 433 | } |
||
| 434 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.