Total Complexity | 55 |
Total Lines | 340 |
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 | * Get all general items not linked to any lesson (learnpath). |
||
66 | */ |
||
67 | public function getGeneralItems(): array |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the activities for the general section. |
||
106 | */ |
||
107 | public function getActivitiesForGeneral(): array |
||
108 | { |
||
109 | $generalLearnpath = (object) [ |
||
110 | 'items' => $this->getGeneralItems(), |
||
111 | 'source_id' => 0, |
||
112 | ]; |
||
113 | |||
114 | $activities = $this->getActivitiesForSection($generalLearnpath, true); |
||
115 | |||
116 | if (!in_array('folder', array_column($activities, 'modulename'))) { |
||
117 | $activities[] = [ |
||
118 | 'id' => 0, |
||
119 | 'moduleid' => 0, |
||
120 | 'modulename' => 'folder', |
||
121 | 'name' => 'Documents', |
||
122 | 'sectionid' => 0, |
||
123 | ]; |
||
124 | } |
||
125 | |||
126 | return $activities; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get the learnpath object by its ID. |
||
131 | */ |
||
132 | public function getLearnpathById(int $sectionId): ?object |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get section data for a learnpath. |
||
145 | */ |
||
146 | public function getSectionData(object $learnpath): array |
||
147 | { |
||
148 | return [ |
||
149 | 'id' => $learnpath->source_id, |
||
150 | 'number' => $learnpath->display_order, |
||
151 | 'name' => $learnpath->name, |
||
152 | 'summary' => $learnpath->description, |
||
153 | 'sequence' => $learnpath->source_id, |
||
154 | 'visible' => $learnpath->visibility, |
||
155 | 'timemodified' => strtotime($learnpath->modified_on), |
||
156 | 'activities' => $this->getActivitiesForSection($learnpath), |
||
157 | ]; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get the activities for a specific section. |
||
162 | */ |
||
163 | public function getActivitiesForSection(object $learnpath, bool $isGeneral = false): array |
||
164 | { |
||
165 | $activities = []; |
||
166 | $sectionId = $isGeneral ? 0 : $learnpath->source_id; |
||
167 | |||
168 | foreach ($learnpath->items as $item) { |
||
169 | $this->addActivityToList($item, $sectionId, $activities); |
||
170 | } |
||
171 | |||
172 | return $activities; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Export the activities of a section. |
||
177 | */ |
||
178 | private function exportActivities(array $activities, string $exportDir, int $sectionId): void |
||
179 | { |
||
180 | $exportClasses = [ |
||
181 | 'quiz' => QuizExport::class, |
||
182 | 'glossary' => GlossaryExport::class, |
||
183 | 'url' => UrlExport::class, |
||
184 | 'assign' => AssignExport::class, |
||
185 | 'forum' => ForumExport::class, |
||
186 | 'page' => PageExport::class, |
||
187 | 'resource' => ResourceExport::class, |
||
188 | 'folder' => FolderExport::class, |
||
189 | 'feedback' => FeedbackExport::class, |
||
190 | ]; |
||
191 | |||
192 | foreach ($activities as $activity) { |
||
193 | $moduleName = $activity['modulename']; |
||
194 | if (isset($exportClasses[$moduleName])) { |
||
195 | $exportClass = new $exportClasses[$moduleName]($this->course); |
||
196 | $exportClass->export($activity['id'], $exportDir, $activity['moduleid'], $sectionId); |
||
197 | } else { |
||
198 | throw new \Exception("Export for module '$moduleName' is not supported."); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Check if an item is associated with any learnpath. |
||
205 | */ |
||
206 | private function isItemInLearnpath(object $item, string $type): bool |
||
207 | { |
||
208 | if (!empty($this->course->resources[RESOURCE_LEARNPATH])) { |
||
209 | foreach ($this->course->resources[RESOURCE_LEARNPATH] as $learnpath) { |
||
210 | if (!empty($learnpath->items)) { |
||
211 | foreach ($learnpath->items as $learnpathItem) { |
||
212 | if ($learnpathItem['item_type'] === $type && $learnpathItem['path'] == $item->source_id) { |
||
213 | return true; |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return false; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Add an activity to the activities list. |
||
225 | */ |
||
226 | private function addActivityToList(array $item, int $sectionId, array &$activities): void |
||
299 | ]; |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Determine the document type based on filetype and path. |
||
305 | */ |
||
306 | private function getDocumentType(string $filetype, string $path): ?string |
||
307 | { |
||
308 | if ('html' === pathinfo($path, PATHINFO_EXTENSION)) { |
||
309 | return 'page'; |
||
310 | } elseif ('file' === $filetype) { |
||
311 | return 'resource'; |
||
312 | } /*elseif ('folder' === $filetype) { |
||
313 | return 'folder'; |
||
314 | }*/ |
||
315 | |||
316 | return null; |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Create the section.xml file. |
||
321 | */ |
||
322 | private function createSectionXml(array $sectionData, string $destinationDir): void |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Create the inforef.xml file for the section. |
||
341 | */ |
||
342 | private function createInforefXml(array $sectionData, string $destinationDir): void |
||
343 | { |
||
357 |