| Conditions | 12 |
| Paths | 147 |
| Total Lines | 83 |
| Code Lines | 52 |
| 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 indexCourseDescription(CCourseDescription $description): ?int |
||
| 34 | { |
||
| 35 | // Global feature toggle |
||
| 36 | $enabled = (string) $this->settingsManager->getSetting('search.search_enabled', true); |
||
| 37 | if ($enabled !== 'true') { |
||
| 38 | return null; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Per-request flag set from the form |
||
| 42 | if ($description->shouldSkipSearchIndex()) { |
||
| 43 | return null; |
||
| 44 | } |
||
| 45 | |||
| 46 | $resourceNode = $description->getResourceNode(); |
||
| 47 | if (!$resourceNode instanceof ResourceNode) { |
||
| 48 | return null; |
||
| 49 | } |
||
| 50 | |||
| 51 | // Resolve course & session |
||
| 52 | [$courseId, $sessionId] = $this->resolveCourseAndSession($resourceNode); |
||
| 53 | |||
| 54 | $title = (string) ($description->getTitle() ?? ''); |
||
| 55 | $body = (string) ($description->getContent() ?? ''); |
||
| 56 | $content = trim($title.' '.$body); |
||
| 57 | |||
| 58 | $fields = [ |
||
| 59 | 'kind' => 'course_description', |
||
| 60 | 'tool' => 'course_description', |
||
| 61 | 'title' => $title, |
||
| 62 | 'description' => $title, |
||
| 63 | 'content' => $content, |
||
| 64 | 'resource_node_id' => (string) $resourceNode->getId(), |
||
| 65 | 'course_id' => $courseId !== null ? (string) $courseId : '', |
||
| 66 | 'session_id' => $sessionId !== null ? (string) $sessionId : '', |
||
| 67 | 'xapian_data' => json_encode([ |
||
| 68 | 'type' => 'course_description', |
||
| 69 | 'description_id' => (int) $description->getIid(), |
||
| 70 | 'course_id' => $courseId, |
||
| 71 | 'session_id' => $sessionId, |
||
| 72 | ]), |
||
| 73 | ]; |
||
| 74 | |||
| 75 | $terms = ['Tcourse_description']; |
||
| 76 | if ($courseId !== null) { |
||
| 77 | $terms[] = 'C'.$courseId; |
||
| 78 | } |
||
| 79 | if ($sessionId !== null) { |
||
| 80 | $terms[] = 'S'.$sessionId; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** @var SearchEngineRef|null $existingRef */ |
||
| 84 | $existingRef = $this->em |
||
| 85 | ->getRepository(SearchEngineRef::class) |
||
| 86 | ->findOneBy(['resourceNodeId' => $resourceNode->getId()]); |
||
| 87 | |||
| 88 | $existingDocId = $existingRef?->getSearchDid(); |
||
| 89 | |||
| 90 | if ($existingDocId !== null) { |
||
| 91 | try { |
||
| 92 | $this->xapianIndexService->deleteDocument($existingDocId); |
||
| 93 | } catch (\Throwable) { |
||
| 94 | // Best-effort delete |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | try { |
||
| 99 | $docId = $this->xapianIndexService->indexDocument($fields, $terms); |
||
| 100 | } catch (\Throwable) { |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($existingRef instanceof SearchEngineRef) { |
||
| 105 | $existingRef->setSearchDid($docId); |
||
| 106 | } else { |
||
| 107 | $existingRef = new SearchEngineRef(); |
||
| 108 | $existingRef->setResourceNodeId((int) $resourceNode->getId()); |
||
| 109 | $existingRef->setSearchDid($docId); |
||
| 110 | $this->em->persist($existingRef); |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->em->flush(); |
||
| 114 | |||
| 115 | return $docId; |
||
| 116 | } |
||
| 173 |