| Total Complexity | 50 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SectionExport 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 SectionExport, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class SectionExport |
||
| 16 | { |
||
| 17 | private $course; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor to initialize the course object. |
||
| 21 | * |
||
| 22 | * @param object $course The course object to be exported. |
||
| 23 | */ |
||
| 24 | public function __construct($course) |
||
| 25 | { |
||
| 26 | $this->course = $course; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Export a section and its activities to the specified directory. |
||
| 31 | */ |
||
| 32 | public function exportSection(int $sectionId, string $exportDir): void |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Export the activities of a section. |
||
| 66 | */ |
||
| 67 | private function exportActivities(array $activities, string $exportDir, int $sectionId): void |
||
| 68 | { |
||
| 69 | $exportClasses = [ |
||
| 70 | 'quiz' => QuizExport::class, |
||
| 71 | 'glossary' => GlossaryExport::class, |
||
| 72 | 'url' => UrlExport::class, |
||
| 73 | 'assign' => AssignExport::class, |
||
| 74 | 'forum' => ForumExport::class, |
||
| 75 | 'page' => PageExport::class, |
||
| 76 | 'resource' => ResourceExport::class, |
||
| 77 | 'folder' => FolderExport::class, |
||
| 78 | 'feedback' => FeedbackExport::class, |
||
| 79 | ]; |
||
| 80 | |||
| 81 | foreach ($activities as $activity) { |
||
| 82 | $moduleName = $activity['modulename']; |
||
| 83 | if (isset($exportClasses[$moduleName])) { |
||
| 84 | $exportClass = new $exportClasses[$moduleName]($this->course); |
||
| 85 | $exportClass->export($activity['id'], $exportDir, $activity['moduleid'], $sectionId); |
||
| 86 | } else { |
||
| 87 | throw new \Exception("Export for module '$moduleName' is not supported."); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get all general items not linked to any lesson (learnpath). |
||
| 94 | */ |
||
| 95 | public function getGeneralItems(): array |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Get the activities for the general section. |
||
| 133 | */ |
||
| 134 | public function getActivitiesForGeneral(): array |
||
| 135 | { |
||
| 136 | $generalLearnpath = (object) [ |
||
| 137 | 'items' => $this->getGeneralItems(), |
||
| 138 | 'source_id' => 0 |
||
| 139 | ]; |
||
| 140 | |||
| 141 | return $this->getActivitiesForSection($generalLearnpath, true); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check if an item is associated with any learnpath. |
||
| 146 | */ |
||
| 147 | private function isItemInLearnpath(object $item, string $type): bool |
||
| 148 | { |
||
| 149 | if (!empty($this->course->resources[RESOURCE_LEARNPATH])) { |
||
| 150 | foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) { |
||
| 151 | if (!empty($learnpath->items)) { |
||
| 152 | foreach ($learnpath->items as $learnpathItem) { |
||
| 153 | if ($learnpathItem['item_type'] === $type && $learnpathItem['path'] == $item->source_id) { |
||
| 154 | return true; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | return false; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the learnpath object by its ID. |
||
| 166 | */ |
||
| 167 | public function getLearnpathById(int $sectionId): ?object |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get section data for a learnpath. |
||
| 180 | */ |
||
| 181 | public function getSectionData(object $learnpath): array |
||
| 182 | { |
||
| 183 | return [ |
||
| 184 | 'id' => $learnpath->source_id, |
||
| 185 | 'number' => $learnpath->display_order, |
||
| 186 | 'name' => $learnpath->name, |
||
| 187 | 'summary' => $learnpath->description, |
||
| 188 | 'sequence' => $learnpath->source_id, |
||
| 189 | 'visible' => $learnpath->visibility, |
||
| 190 | 'timemodified' => strtotime($learnpath->modified_on), |
||
| 191 | 'activities' => $this->getActivitiesForSection($learnpath) |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the activities for a specific section. |
||
| 197 | */ |
||
| 198 | public function getActivitiesForSection(object $learnpath, bool $isGeneral = false): array |
||
| 199 | { |
||
| 200 | $activities = []; |
||
| 201 | $sectionId = $isGeneral ? 0 : $learnpath->source_id; |
||
| 202 | |||
| 203 | foreach ($learnpath->items as $item) { |
||
| 204 | $this->addActivityToList($item, $sectionId, $activities); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $activities; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Add an activity to the activities list. |
||
| 212 | */ |
||
| 213 | private function addActivityToList(array $item, int $sectionId, array &$activities): void |
||
| 265 | ]; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Determine the document type based on filetype and path. |
||
| 271 | */ |
||
| 272 | private function getDocumentType(string $filetype, string $path): ?string |
||
| 273 | { |
||
| 274 | if ('html' === pathinfo($path, PATHINFO_EXTENSION)) { |
||
| 275 | return 'page'; |
||
| 276 | } elseif ('file' === $filetype) { |
||
| 277 | return 'resource'; |
||
| 278 | } elseif ('folder' === $filetype) { |
||
| 279 | return 'folder'; |
||
| 280 | } |
||
| 281 | |||
| 282 | return null; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Create the section.xml file. |
||
| 287 | */ |
||
| 288 | private function createSectionXml(array $sectionData, string $destinationDir): void |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Create the inforef.xml file for the section. |
||
| 307 | */ |
||
| 308 | private function createInforefXml(array $sectionData, string $destinationDir): void |
||
| 309 | { |
||
| 323 |