| Conditions | 13 |
| Paths | 578 |
| Total Lines | 88 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 33 | public function indexLp(CLp $lp): ?int |
||
| 34 | { |
||
| 35 | // Global feature toggle (same style as QuestionXapianIndexer) |
||
| 36 | $enabled = (string) $this->settingsManager->getSetting('search.search_enabled', true); |
||
| 37 | if ($enabled !== 'true') { |
||
| 38 | return null; |
||
| 39 | } |
||
| 40 | |||
| 41 | $resourceNode = $lp->getResourceNode(); |
||
| 42 | if (!$resourceNode instanceof ResourceNode) { |
||
| 43 | // Learning path without a resource node cannot be indexed |
||
| 44 | return null; |
||
| 45 | } |
||
| 46 | |||
| 47 | // Resolve course and session from resource links |
||
| 48 | [$courseId, $sessionId] = $this->resolveCourseAndSession($resourceNode); |
||
| 49 | |||
| 50 | $title = (string) $lp->getTitle(); |
||
| 51 | $description = (string) ($lp->getDescription() ?? ''); |
||
| 52 | $content = \trim($title.' '.$description); |
||
| 53 | |||
| 54 | // Keep field names consistent across indexers |
||
| 55 | $fields = [ |
||
| 56 | 'kind' => 'learnpath', |
||
| 57 | 'tool' => 'learnpath', |
||
| 58 | 'title' => $title, |
||
| 59 | 'description' => $description, |
||
| 60 | 'content' => $content, |
||
| 61 | 'filetype' => 'learnpath', |
||
| 62 | 'resource_node_id' => (string) $resourceNode->getId(), |
||
| 63 | 'lp_id' => $lp->getIid() !== null ? (string) $lp->getIid() : '', |
||
| 64 | 'course_id' => $courseId !== null ? (string) $courseId : '', |
||
| 65 | 'session_id' => $sessionId !== null ? (string) $sessionId : '', |
||
| 66 | 'full_path' => (string) $lp->getPath(), |
||
| 67 | 'xapian_data' => json_encode([ |
||
| 68 | 'type' => 'learnpath', |
||
| 69 | 'lp_id' => $lp->getIid(), |
||
| 70 | 'course_id' => $courseId, |
||
| 71 | 'session_id' => $sessionId, |
||
| 72 | ]), |
||
| 73 | ]; |
||
| 74 | |||
| 75 | // Terms for filtering |
||
| 76 | $terms = ['Tlearnpath', 'Tlp']; |
||
| 77 | if ($courseId !== null) { |
||
| 78 | $terms[] = 'C'.$courseId; |
||
| 79 | } |
||
| 80 | if ($sessionId !== null) { |
||
| 81 | $terms[] = 'S'.$sessionId; |
||
| 82 | } |
||
| 83 | if ($lp->getIid() !== null) { |
||
| 84 | $terms[] = 'L'.$lp->getIid(); |
||
| 85 | } |
||
| 86 | |||
| 87 | // Reuse SearchEngineRef per resource node (same pattern as questions) |
||
| 88 | /** @var SearchEngineRef|null $existingRef */ |
||
| 89 | $existingRef = $this->em |
||
| 90 | ->getRepository(SearchEngineRef::class) |
||
| 91 | ->findOneBy(['resourceNodeId' => $resourceNode->getId()]); |
||
| 92 | |||
| 93 | $existingDocId = $existingRef?->getSearchDid(); |
||
| 94 | |||
| 95 | if ($existingDocId !== null) { |
||
| 96 | try { |
||
| 97 | $this->xapianIndexService->deleteDocument($existingDocId); |
||
| 98 | } catch (\Throwable) { |
||
| 99 | // Best-effort delete: ignore errors |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | try { |
||
| 104 | $docId = $this->xapianIndexService->indexDocument($fields, $terms); |
||
| 105 | } catch (\Throwable) { |
||
| 106 | return null; |
||
| 107 | } |
||
| 108 | |||
| 109 | if ($existingRef instanceof SearchEngineRef) { |
||
| 110 | $existingRef->setSearchDid($docId); |
||
| 111 | } else { |
||
| 112 | $existingRef = new SearchEngineRef(); |
||
| 113 | $existingRef->setResourceNodeId((int) $resourceNode->getId()); |
||
| 114 | $existingRef->setSearchDid($docId); |
||
| 115 | $this->em->persist($existingRef); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->em->flush(); |
||
| 119 | |||
| 120 | return $docId; |
||
| 121 | } |
||
| 183 |