| Total Complexity | 44 |
| Total Lines | 236 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Version20241010111200 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 Version20241010111200, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class Version20241010111200 extends AbstractMigrationChamilo |
||
| 17 | { |
||
| 18 | public function getDescription(): string |
||
| 19 | { |
||
| 20 | return 'Update document and link URLs in HTML content blocks and replace old legacy paths with new resource paths'; |
||
| 21 | } |
||
| 22 | |||
| 23 | public function up(Schema $schema): void |
||
| 24 | { |
||
| 25 | $this->entityManager->clear(); |
||
|
|
|||
| 26 | |||
| 27 | // Define the content fields to update |
||
| 28 | $updateConfigurations = [ |
||
| 29 | ['table' => 'c_tool_intro', 'field' => 'intro_text'], |
||
| 30 | ['table' => 'c_course_description', 'field' => 'content'], |
||
| 31 | ['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']], |
||
| 32 | ['table' => 'c_quiz_question', 'fields' => ['description', 'question']], |
||
| 33 | ['table' => 'c_quiz_answer', 'fields' => ['answer', 'comment']], |
||
| 34 | ['table' => 'c_student_publication', 'field' => 'description'], |
||
| 35 | ['table' => 'c_student_publication_comment', 'field' => 'comment'], |
||
| 36 | ['table' => 'c_forum_post', 'field' => 'post_text'], |
||
| 37 | ['table' => 'c_glossary', 'field' => 'description'], |
||
| 38 | ['table' => 'c_survey', 'fields' => ['title', 'subtitle']], |
||
| 39 | ['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']], |
||
| 40 | ['table' => 'c_survey_question_option', 'field' => 'option_text'], |
||
| 41 | ]; |
||
| 42 | |||
| 43 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
| 44 | |||
| 45 | foreach ($updateConfigurations as $config) { |
||
| 46 | $this->updateContent($config, $documentRepo); |
||
| 47 | } |
||
| 48 | |||
| 49 | $this->updateDocumentLinks(); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Updates content fields with new URLs. |
||
| 54 | */ |
||
| 55 | private function updateContent(array $config, $documentRepo): void |
||
| 56 | { |
||
| 57 | $fields = isset($config['field']) ? [$config['field']] : $config['fields'] ?? []; |
||
| 58 | foreach ($fields as $field) { |
||
| 59 | $sql = "SELECT iid, {$field} FROM {$config['table']}"; |
||
| 60 | $result = $this->connection->executeQuery($sql); |
||
| 61 | $items = $result->fetchAllAssociative(); |
||
| 62 | foreach ($items as $item) { |
||
| 63 | $originalText = $item[$field]; |
||
| 64 | if (is_string($originalText) && trim($originalText) !== '') { |
||
| 65 | $updatedText = $this->replaceOldURLs($originalText, $documentRepo); |
||
| 66 | if ($originalText !== $updatedText) { |
||
| 67 | $updateSql = "UPDATE {$config['table']} SET {$field} = :newText WHERE iid = :id"; |
||
| 68 | $this->connection->executeQuery($updateSql, ['newText' => $updatedText, 'id' => $item['iid']]); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Updates HTML content in document files by replacing old URLs with new resource paths. |
||
| 77 | */ |
||
| 78 | private function updateDocumentLinks(): void |
||
| 79 | { |
||
| 80 | $sql = "SELECT iid, resource_node_id FROM c_document WHERE filetype = 'file'"; |
||
| 81 | $result = $this->connection->executeQuery($sql); |
||
| 82 | $items = $result->fetchAllAssociative(); |
||
| 83 | |||
| 84 | $documentRepo = $this->container->get(CDocumentRepository::class); |
||
| 85 | $resourceNodeRepo = $this->container->get(ResourceNodeRepository::class); |
||
| 86 | |||
| 87 | foreach ($items as $item) { |
||
| 88 | /** @var CDocument $document */ |
||
| 89 | $document = $documentRepo->find($item['iid']); |
||
| 90 | if (!$document) { |
||
| 91 | continue; |
||
| 92 | } |
||
| 93 | |||
| 94 | $resourceNode = $document->getResourceNode(); |
||
| 95 | if (!$resourceNode || !$resourceNode->hasResourceFile()) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | $resourceFile = $resourceNode->getResourceFiles()->first(); |
||
| 100 | if ($resourceFile && $resourceFile->getMimeType() === 'text/html') { |
||
| 101 | try { |
||
| 102 | $content = $resourceNodeRepo->getResourceNodeFileContent($resourceNode); |
||
| 103 | if (is_string($content) && trim($content) !== '') { |
||
| 104 | $updatedContent = $this->replaceOldURLs($content, $documentRepo); |
||
| 105 | if ($content !== $updatedContent) { |
||
| 106 | $documentRepo->updateResourceFileContent($document, $updatedContent); |
||
| 107 | $documentRepo->update($document); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } catch (Exception $e) { |
||
| 111 | // error_log("Error processing file for document ID {$item['iid']}: " . $e->getMessage()); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Replace old URLs with new Vue.js resource paths. |
||
| 119 | */ |
||
| 120 | private function replaceOldURLs(string $content, $documentRepo): string |
||
| 121 | { |
||
| 122 | // Replace document URLs |
||
| 123 | $content = $this->replaceOldDocumentURLs($content, $documentRepo); |
||
| 124 | // Replace link URLs |
||
| 125 | return $this->replaceOldLinkURLs($content); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Replace old document URLs with the new relative paths. |
||
| 130 | */ |
||
| 131 | private function replaceOldDocumentURLs(string $content, $documentRepo): string |
||
| 132 | { |
||
| 133 | $pattern = '/(href|src)=["\'](https?:\/\/[^\/]+)?\/main\/document\/(document\.php|show_content\.php|showinframes\.php)\?([^"\']*)["\']/i'; |
||
| 134 | |||
| 135 | return preg_replace_callback($pattern, function ($matches) use ($documentRepo) { |
||
| 136 | $attribute = $matches[1]; |
||
| 137 | $params = str_replace('&', '&', $matches[4]); |
||
| 138 | |||
| 139 | parse_str($params, $parsedParams); |
||
| 140 | $documentId = $parsedParams['id'] ?? null; |
||
| 141 | $courseId = $parsedParams['cid'] ?? null; |
||
| 142 | $sessionId = $parsedParams['id_session'] ?? $parsedParams['sid'] ?? '0'; |
||
| 143 | $groupId = $parsedParams['gidReq'] ?? $parsedParams['gid'] ?? '0'; |
||
| 144 | |||
| 145 | if (!$courseId && isset($parsedParams['cidReq'])) { |
||
| 146 | $courseCode = $parsedParams['cidReq']; |
||
| 147 | $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1'; |
||
| 148 | $stmt = $this->connection->executeQuery($sql, ['code' => $courseCode]); |
||
| 149 | $course = $stmt->fetch(); |
||
| 150 | |||
| 151 | if ($course) { |
||
| 152 | $courseId = $course['id']; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | if ($documentId && $courseId) { |
||
| 157 | $sql = "SELECT iid, filetype, resource_node_id FROM c_document WHERE iid = :documentId"; |
||
| 158 | $result = $this->connection->executeQuery($sql, ['documentId' => $documentId]); |
||
| 159 | $documents = $result->fetchAllAssociative(); |
||
| 160 | |||
| 161 | if (!empty($documents)) { |
||
| 162 | $documentData = $documents[0]; |
||
| 163 | if ($documentData['filetype'] === 'folder') { |
||
| 164 | $newUrl = $this->generateFolderUrl((int)$documentData['resource_node_id'], (int)$courseId, $sessionId, $groupId); |
||
| 165 | } else { |
||
| 166 | $document = $documentRepo->find($documentId); |
||
| 167 | $newUrl = $documentRepo->getResourceFileUrl($document); |
||
| 168 | } |
||
| 169 | |||
| 170 | return sprintf('%s="%s"', $attribute, $newUrl); |
||
| 171 | } |
||
| 172 | } elseif ($courseId) { |
||
| 173 | $sql = "SELECT resource_node_id FROM course WHERE id = :courseId"; |
||
| 174 | $result = $this->connection->executeQuery($sql, ['courseId' => $courseId]); |
||
| 175 | $course = $result->fetch(); |
||
| 176 | |||
| 177 | if ($course && isset($course['resource_node_id'])) { |
||
| 178 | $newUrl = $this->generateFolderUrl((int)$course['resource_node_id'], (int)$courseId, $sessionId, $groupId); |
||
| 179 | |||
| 180 | return sprintf('%s="%s"', $attribute, $newUrl); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | return $matches[0]; |
||
| 185 | }, $content); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Replace old link URLs. |
||
| 190 | */ |
||
| 191 | private function replaceOldLinkURLs(string $content): string |
||
| 192 | { |
||
| 193 | $pattern = '/(href|src)=["\'](https?:\/\/[^\/]+)?\/main\/link\/link\.php\?([^"\']*)["\']/i'; |
||
| 194 | |||
| 195 | return preg_replace_callback($pattern, function ($matches) { |
||
| 196 | $attribute = $matches[1]; |
||
| 197 | $params = str_replace('&', '&', $matches[3]); |
||
| 198 | parse_str($params, $parsedParams); |
||
| 199 | |||
| 200 | $courseId = isset($parsedParams['cid']) ? (int)$parsedParams['cid'] : null; |
||
| 201 | $sessionId = $parsedParams['id_session'] ?? $parsedParams['sid'] ?? '0'; |
||
| 202 | $groupId = $parsedParams['gidReq'] ?? $parsedParams['gid'] ?? '0'; |
||
| 203 | |||
| 204 | if (!$courseId && isset($parsedParams['cidReq'])) { |
||
| 205 | $courseCode = $parsedParams['cidReq']; |
||
| 206 | $courseId = $this->getCourseIdFromCode($courseCode); |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($courseId) { |
||
| 210 | $sql = "SELECT resource_node_id FROM course WHERE id = :courseId"; |
||
| 211 | $result = $this->connection->executeQuery($sql, ['courseId' => $courseId]); |
||
| 212 | $course = $result->fetch(); |
||
| 213 | |||
| 214 | if ($course && isset($course['resource_node_id'])) { |
||
| 215 | $newUrl = $this->generateLinkUrl((int)$course['resource_node_id'], (int)$courseId, $sessionId, $groupId); |
||
| 216 | |||
| 217 | return sprintf('%s="%s"', $attribute, $newUrl); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return $matches[0]; |
||
| 222 | }, $content); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Generate the relative URL for link-type documents. |
||
| 227 | */ |
||
| 228 | private function generateLinkUrl(int $resourceNodeId, int $courseId, string $sessionId, string $groupId): string |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Retrieves the course ID from the course code. |
||
| 235 | * Returns null if the course is not found. |
||
| 236 | */ |
||
| 237 | private function getCourseIdFromCode(string $courseCode): ?int |
||
| 238 | { |
||
| 239 | $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1'; |
||
| 240 | $stmt = $this->connection->executeQuery($sql, ['code' => $courseCode]); |
||
| 241 | $course = $stmt->fetch(); |
||
| 242 | |||
| 243 | return $course ? (int)$course['id'] : null; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Generate the relative URL for folder-type documents using resource_node_id. |
||
| 248 | */ |
||
| 249 | private function generateFolderUrl(int $resourceNodeId, int $courseId, string $sessionId, string $groupId): string |
||
| 252 | } |
||
| 253 | } |
||
| 254 |
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.