Total Complexity | 43 |
Total Lines | 240 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | private function replaceOldURLsWithNew($itemDataText, $courseDirectory, $courseId, $documentRepo): array|string|null |
||
166 | } |
||
167 | |||
168 | private function replaceDocumentLinks($documents, $documentRepo, $matches, $index, $videoPath, $courseId, &$contentText): void |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private function createNewDocument($videoPath, $courseId) |
||
185 | { |
||
186 | try { |
||
187 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
188 | $kernel = $this->container->get('kernel'); |
||
189 | $rootPath = $kernel->getProjectDir(); |
||
190 | $appCourseOldPath = $rootPath.'/app'.$videoPath; |
||
191 | $title = basename($appCourseOldPath); |
||
192 | |||
193 | $courseRepo = $this->container->get(CourseRepository::class); |
||
194 | $course = $courseRepo->find($courseId); |
||
195 | if (!$course) { |
||
196 | throw new Exception("Course with ID $courseId not found."); |
||
197 | } |
||
198 | |||
199 | $document = $documentRepo->findCourseResourceByTitle($title, $course->getResourceNode(), $course); |
||
200 | if (null !== $document) { |
||
201 | return $document; |
||
202 | } |
||
203 | |||
204 | if (file_exists($appCourseOldPath) && !is_dir($appCourseOldPath)) { |
||
205 | $document = new CDocument(); |
||
206 | $document->setFiletype('file') |
||
207 | ->setTitle($title) |
||
208 | ->setComment(null) |
||
209 | ->setReadonly(false) |
||
210 | ->setCreator($this->getAdmin()) |
||
211 | ->setParent($course) |
||
212 | ->addCourseLink($course) |
||
213 | ; |
||
214 | |||
215 | $this->entityManager->persist($document); |
||
216 | $this->entityManager->flush(); |
||
217 | |||
218 | $documentRepo->addFileFromPath($document, $title, $appCourseOldPath); |
||
219 | |||
220 | return $document; |
||
221 | } |
||
222 | $generalCoursesPath = $rootPath.'/app/courses/'; |
||
223 | $foundPath = $this->recursiveFileSearch($generalCoursesPath, $title); |
||
224 | if ($foundPath) { |
||
225 | $document = new CDocument(); |
||
226 | $document->setFiletype('file') |
||
227 | ->setTitle($title) |
||
228 | ->setComment(null) |
||
229 | ->setReadonly(false) |
||
230 | ->setCreator($this->getAdmin()) |
||
231 | ->setParent($course) |
||
232 | ->addCourseLink($course) |
||
233 | ; |
||
234 | |||
235 | $this->entityManager->persist($document); |
||
236 | $this->entityManager->flush(); |
||
237 | |||
238 | $documentRepo->addFileFromPath($document, $title, $foundPath); |
||
239 | error_log('File found in new location: '.$foundPath); |
||
240 | |||
241 | return $document; |
||
242 | } |
||
243 | |||
244 | throw new Exception('File not found in any location.'); |
||
245 | } catch (Exception $e) { |
||
246 | error_log('Migration error: '.$e->getMessage()); |
||
247 | |||
248 | return null; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | private function recursiveFileSearch($directory, $title) |
||
262 | } |
||
263 | } |
||
264 |
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.