Total Complexity | 43 |
Total Lines | 254 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Version20230913162700 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 Version20230913162700, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | final class Version20230913162700 extends AbstractMigrationChamilo |
||
23 | { |
||
24 | public function getDescription(): string |
||
25 | { |
||
26 | return 'Replace old document path by resource file path'; |
||
27 | } |
||
28 | |||
29 | public function up(Schema $schema): void |
||
30 | { |
||
31 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
|
|||
32 | $resourceNodeRepo = $this->container->get(ResourceNodeRepository::class); |
||
33 | |||
34 | $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
35 | $updateConfigurations = [ |
||
36 | ['table' => 'c_tool_intro', 'field' => 'intro_text'], |
||
37 | ['table' => 'c_course_description', 'field' => 'content'], |
||
38 | ['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']], |
||
39 | ['table' => 'c_quiz_question', 'fields' => ['description', 'question']], |
||
40 | ['table' => 'c_quiz_answer', 'fields' => ['answer', 'comment']], |
||
41 | ['table' => 'c_course_description', 'field' => 'content'], |
||
42 | ['table' => 'c_student_publication', 'field' => 'description'], |
||
43 | ['table' => 'c_student_publication_comment', 'field' => 'comment'], |
||
44 | ['table' => 'c_forum_category', 'field' => 'cat_comment'], |
||
45 | ['table' => 'c_forum_forum', 'field' => 'forum_comment'], |
||
46 | ['table' => 'c_forum_post', 'field' => 'post_text'], |
||
47 | ['table' => 'c_glossary', 'field' => 'description'], |
||
48 | ['table' => 'c_survey', 'fields' => ['title', 'subtitle']], |
||
49 | ['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']], |
||
50 | ['table' => 'c_survey_question_option', 'field' => 'option_text'], |
||
51 | ]; |
||
52 | |||
53 | /** @var Course $course */ |
||
54 | foreach ($q->toIterable() as $course) { |
||
55 | $courseId = $course->getId(); |
||
56 | $courseDirectory = $course->getDirectory(); |
||
57 | |||
58 | if (empty($courseDirectory)) { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | foreach ($updateConfigurations as $config) { |
||
63 | $this->updateContent($config, $courseDirectory, $courseId, $documentRepo); |
||
64 | } |
||
65 | |||
66 | $this->updateHtmlContent($courseDirectory, $courseId, $documentRepo, $resourceNodeRepo); |
||
67 | } |
||
68 | } |
||
69 | |||
70 | private function updateContent($config, $courseDirectory, $courseId, $documentRepo): void |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | private function updateHtmlContent($courseDirectory, $courseId, $documentRepo, $resourceNodeRepo): void |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | } |
||
142 | |||
143 | private function replaceOldURLsWithNew($itemDataText, $courseDirectory, $courseId, $documentRepo): array|string|null |
||
144 | { |
||
145 | $contentText = $itemDataText; |
||
146 | $specificCoursePattern = '/(src|href)=["\']((https?:\/\/[^\/]+)?(\/courses\/([^\/]+)\/document\/[^"\']+\.\w+))["\']/i'; |
||
147 | preg_match_all($specificCoursePattern, $contentText, $matches); |
||
148 | |||
149 | foreach ($matches[2] as $index => $fullUrl) { |
||
150 | $videoPath = parse_url($fullUrl, PHP_URL_PATH) ?: $fullUrl; |
||
151 | $actualCourseDirectory = $matches[5][$index]; |
||
152 | if ($actualCourseDirectory !== $courseDirectory) { |
||
153 | $videoPath = preg_replace("/^\\/courses\\/$actualCourseDirectory\\//i", "/courses/$courseDirectory/", $videoPath); |
||
154 | } |
||
155 | |||
156 | $documentPath = str_replace('/courses/'.$courseDirectory.'/document/', '/', $videoPath); |
||
157 | |||
158 | $sql = "SELECT iid, path, resource_node_id FROM c_document WHERE c_id = $courseId AND path LIKE '$documentPath'"; |
||
159 | $result = $this->connection->executeQuery($sql); |
||
160 | $documents = $result->fetchAllAssociative(); |
||
161 | |||
162 | if (!empty($documents)) { |
||
163 | $this->replaceDocumentLinks($documents, $documentRepo, $matches, $index, $videoPath, $courseId, $contentText); |
||
164 | } else { |
||
165 | $document = $this->createNewDocument($videoPath, $courseId); |
||
166 | if ($document) { |
||
167 | $newUrl = $documentRepo->getResourceFileUrl($document); |
||
168 | if ($newUrl) { |
||
169 | $replacement = $matches[1][$index].'="'.$newUrl.'"'; |
||
170 | $contentText = str_replace($matches[0][$index], $replacement, $contentText); |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $contentText; |
||
177 | } |
||
178 | |||
179 | private function replaceDocumentLinks($documents, $documentRepo, $matches, $index, $videoPath, $courseId, &$contentText): void |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | private function createNewDocument($videoPath, $courseId) |
||
196 | { |
||
197 | try { |
||
198 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
199 | $kernel = $this->container->get('kernel'); |
||
200 | $rootPath = $kernel->getProjectDir(); |
||
201 | $appCourseOldPath = $rootPath.'/app'.$videoPath; |
||
202 | $title = basename($appCourseOldPath); |
||
203 | |||
204 | $courseRepo = $this->container->get(CourseRepository::class); |
||
205 | $course = $courseRepo->find($courseId); |
||
206 | if (!$course) { |
||
207 | throw new Exception("Course with ID $courseId not found."); |
||
208 | } |
||
209 | |||
210 | $existingDocument = $documentRepo->findResourceByTitleInCourse($title, $course); |
||
211 | if ($existingDocument) { |
||
212 | error_log("Document '$title' already exists for course {$course->getId()}. Skipping creation."); |
||
213 | return $existingDocument; |
||
214 | } |
||
215 | |||
216 | if (file_exists($appCourseOldPath) && !is_dir($appCourseOldPath)) { |
||
217 | $document = new CDocument(); |
||
218 | $document->setFiletype('file') |
||
219 | ->setTitle($title) |
||
220 | ->setComment(null) |
||
221 | ->setReadonly(false) |
||
222 | ->setCreator($this->getAdmin()) |
||
223 | ->setParent($course) |
||
224 | ->addCourseLink($course) |
||
225 | ; |
||
226 | |||
227 | $this->entityManager->persist($document); |
||
228 | $this->entityManager->flush(); |
||
229 | |||
230 | $documentRepo->addFileFromPath($document, $title, $appCourseOldPath); |
||
231 | |||
232 | error_log("Document '$title' successfully created for course $courseId."); |
||
233 | return $document; |
||
234 | } |
||
235 | $generalCoursesPath = $this->getUpdateRootPath().'/app/courses/'; |
||
236 | $foundPath = $this->recursiveFileSearch($generalCoursesPath, $title); |
||
237 | if ($foundPath) { |
||
238 | $document = new CDocument(); |
||
239 | $document->setFiletype('file') |
||
240 | ->setTitle($title) |
||
241 | ->setComment(null) |
||
242 | ->setReadonly(false) |
||
243 | ->setCreator($this->getAdmin()) |
||
244 | ->setParent($course) |
||
245 | ->addCourseLink($course) |
||
246 | ; |
||
247 | |||
248 | $this->entityManager->persist($document); |
||
249 | $this->entityManager->flush(); |
||
250 | |||
251 | $documentRepo->addFileFromPath($document, $title, $foundPath); |
||
252 | error_log('File found in new location: '.$foundPath); |
||
253 | |||
254 | return $document; |
||
255 | } |
||
256 | |||
257 | error_log("File '$title' not found for course $courseId. Skipping."); |
||
258 | return null; |
||
259 | } catch (Exception $e) { |
||
260 | error_log('Error in createNewDocument: ' . $e->getMessage()); |
||
261 | |||
262 | return null; |
||
263 | } |
||
264 | } |
||
265 | |||
266 | private function recursiveFileSearch($directory, $title) |
||
276 | } |
||
277 | } |
||
278 |
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.