| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 194 |
| Code Lines | 126 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 59 | { |
||
| 60 | $io = new SymfonyStyle($input, $output); |
||
| 61 | $io->title('Fast attendances migration'); |
||
| 62 | |||
| 63 | // Accept both flags as the same action (alias) |
||
| 64 | $dropItemProperty = (bool) $input->getOption('drop-c-item-property') |
||
| 65 | || (bool) $input->getOption('drop-c-item-properties'); |
||
| 66 | |||
| 67 | $fallbackAdminId = $this->getFallbackAdminId(); |
||
| 68 | $uuidIsBinary = $this->detectUuidIsBinary(); |
||
| 69 | |||
| 70 | $hasItemProperty = $this->tableExists('c_item_property'); |
||
| 71 | $hasAttendanceCId = $this->tableHasColumn('c_attendance', 'c_id'); |
||
| 72 | $hasAttendanceTitle = $this->tableHasColumn('c_attendance', 'title'); |
||
| 73 | $hasAttendanceName = $this->tableHasColumn('c_attendance', 'name'); |
||
| 74 | |||
| 75 | // At this migration stage, c_attendance.session_id is expected to be removed. |
||
| 76 | // We only rely on c_item_property.session_id when available. |
||
| 77 | $hasItemPropertySessionId = $hasItemProperty && $this->tableHasColumn('c_item_property', 'session_id'); |
||
| 78 | |||
| 79 | if (!$hasItemProperty && !$hasAttendanceCId) { |
||
| 80 | $io->error('Cannot determine attendance->course mapping: c_item_property does not exist and c_attendance.c_id does not exist.'); |
||
| 81 | return Command::FAILURE; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($hasItemProperty && !$hasItemPropertySessionId) { |
||
| 85 | $io->note('c_item_property.session_id is not available. Session context will be stored as NULL in resource_link.'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $courseIds = $this->getCourseIdsToProcess($hasItemProperty, $hasAttendanceCId); |
||
| 89 | |||
| 90 | if (0 === \count($courseIds)) { |
||
| 91 | $io->success('No attendances to migrate (nothing pending).'); |
||
| 92 | if ($dropItemProperty) { |
||
| 93 | $this->maybeDropItemProperty($io); |
||
| 94 | } |
||
| 95 | return Command::SUCCESS; |
||
| 96 | } |
||
| 97 | |||
| 98 | $processedCourses = 0; |
||
| 99 | $processedAttendances = 0; |
||
| 100 | |||
| 101 | foreach ($courseIds as $courseId) { |
||
| 102 | $courseId = (int) $courseId; |
||
| 103 | |||
| 104 | $courseRow = $this->connection->fetchAssociative( |
||
| 105 | 'SELECT id, resource_node_id FROM course WHERE id = :id', |
||
| 106 | ['id' => $courseId] |
||
| 107 | ); |
||
| 108 | |||
| 109 | if (!$courseRow) { |
||
| 110 | $io->warning("Course {$courseId} not found - skipping."); |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | |||
| 114 | $courseResourceNodeId = isset($courseRow['resource_node_id']) ? (int) $courseRow['resource_node_id'] : 0; |
||
| 115 | if ($courseResourceNodeId <= 0) { |
||
| 116 | $io->warning("Course {$courseId} has no resource_node_id - skipping."); |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $courseNode = $this->connection->fetchAssociative( |
||
| 121 | 'SELECT id, path, level FROM resource_node WHERE id = :id', |
||
| 122 | ['id' => $courseResourceNodeId] |
||
| 123 | ); |
||
| 124 | |||
| 125 | if (!$courseNode) { |
||
| 126 | $io->warning("Course {$courseId} resource_node {$courseResourceNodeId} not found - skipping."); |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | $coursePath = rtrim((string) ($courseNode['path'] ?? ''), '/'); |
||
| 131 | |||
| 132 | $attendanceRows = $this->fetchPendingAttendancesForCourse( |
||
| 133 | courseId: $courseId, |
||
| 134 | hasItemProperty: $hasItemProperty, |
||
| 135 | hasAttendanceCId: $hasAttendanceCId, |
||
| 136 | hasAttendanceTitle: $hasAttendanceTitle, |
||
| 137 | hasAttendanceName: $hasAttendanceName, |
||
| 138 | hasItemPropertySessionId: $hasItemPropertySessionId |
||
| 139 | ); |
||
| 140 | |||
| 141 | if (0 === \count($attendanceRows)) { |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | $io->section("Course {$courseId}: migrating ".\count($attendanceRows).' attendances'); |
||
| 146 | |||
| 147 | $displayOrder = 0; |
||
| 148 | $this->connection->beginTransaction(); |
||
| 149 | |||
| 150 | try { |
||
| 151 | foreach ($attendanceRows as $row) { |
||
| 152 | $attendanceId = (int) $row['iid']; |
||
| 153 | $attendanceTitle = $this->pickAttendanceTitle($row, $attendanceId); |
||
| 154 | |||
| 155 | // session_id is read from c_item_property (when available). |
||
| 156 | // Normalize 0 -> NULL as expected by resource_link.session_id. |
||
| 157 | $attendanceSessionId = isset($row['session_id']) ? (int) $row['session_id'] : 0; |
||
| 158 | $attendanceSessionId = 0 === $attendanceSessionId ? null : $attendanceSessionId; |
||
| 159 | |||
| 160 | $ip = []; |
||
| 161 | if ($hasItemProperty) { |
||
| 162 | $ip = $this->connection->fetchAssociative( |
||
| 163 | "SELECT insert_date, lastedit_date, lastedit_user_id, visibility, start_visible, end_visible, to_group_id, to_user_id |
||
| 164 | FROM c_item_property |
||
| 165 | WHERE tool = 'attendance' AND ref = :ref AND c_id = :cid |
||
| 166 | LIMIT 1", |
||
| 167 | ['ref' => $attendanceId, 'cid' => $courseId] |
||
| 168 | ) ?: []; |
||
| 169 | } |
||
| 170 | |||
| 171 | $insertDate = $ip['insert_date'] ?? $this->nowUtc(); |
||
| 172 | $lastEditDate = $ip['lastedit_date'] ?? $insertDate; |
||
| 173 | $lastEditUserId = isset($ip['lastedit_user_id']) ? (int) $ip['lastedit_user_id'] : null; |
||
| 174 | |||
| 175 | $visibility = isset($ip['visibility']) ? (int) $ip['visibility'] : 1; |
||
| 176 | $startVisible = $ip['start_visible'] ?? null; |
||
| 177 | $endVisible = $ip['end_visible'] ?? null; |
||
| 178 | |||
| 179 | $toGroupId = isset($ip['to_group_id']) ? (int) $ip['to_group_id'] : null; |
||
| 180 | $toUserId = isset($ip['to_user_id']) ? (int) $ip['to_user_id'] : null; |
||
| 181 | |||
| 182 | $creatorId = $lastEditUserId ?: $fallbackAdminId; |
||
| 183 | |||
| 184 | $uuid = Uuid::v4(); |
||
| 185 | $uuidValue = $uuidIsBinary ? $uuid->toBinary() : $uuid->toRfc4122(); |
||
| 186 | |||
| 187 | // Keep the slug stable and unique by appending the iid. |
||
| 188 | $slug = (string) $this->slugger->slug($attendanceTitle.'-'.$attendanceId)->lower(); |
||
| 189 | |||
| 190 | $resourceNodeId = $this->insertResourceNode( |
||
| 191 | title: $attendanceTitle, |
||
| 192 | slug: $slug, |
||
| 193 | level: 3, |
||
| 194 | createdAt: $insertDate, |
||
| 195 | updatedAt: $lastEditDate, |
||
| 196 | uuid: $uuidValue, |
||
| 197 | uuidIsBinary: $uuidIsBinary, |
||
| 198 | resourceTypeId: self::RESOURCE_TYPE_ID_ATTENDANCE, |
||
| 199 | creatorId: $creatorId, |
||
| 200 | parentId: $courseResourceNodeId |
||
| 201 | ); |
||
| 202 | |||
| 203 | $this->connection->insert('resource_link', [ |
||
| 204 | 'visibility' => $visibility, |
||
| 205 | 'start_visibility_at' => $startVisible, |
||
| 206 | 'end_visibility_at' => $endVisible, |
||
| 207 | 'display_order' => $displayOrder, |
||
| 208 | 'resource_type_group' => self::RESOURCE_TYPE_GROUP_ATTENDANCE, |
||
| 209 | 'deleted_at' => null, |
||
| 210 | 'created_at' => $insertDate, |
||
| 211 | 'updated_at' => $lastEditDate, |
||
| 212 | 'resource_node_id' => $resourceNodeId, |
||
| 213 | 'parent_id' => null, |
||
| 214 | 'c_id' => $courseId, |
||
| 215 | 'session_id' => $attendanceSessionId, |
||
| 216 | 'usergroup_id' => null, |
||
| 217 | 'group_id' => $toGroupId, |
||
| 218 | 'user_id' => $toUserId, |
||
| 219 | ]); |
||
| 220 | |||
| 221 | // resource_node.path should be aligned with the course tree: <coursePath>-<nodeId>/ |
||
| 222 | $newPath = $coursePath.'-'.$resourceNodeId.'/'; |
||
| 223 | $this->connection->update('resource_node', ['path' => $newPath], ['id' => $resourceNodeId]); |
||
| 224 | |||
| 225 | // Mark attendance as migrated by storing the new resource_node_id. |
||
| 226 | $this->connection->update('c_attendance', ['resource_node_id' => $resourceNodeId], ['iid' => $attendanceId]); |
||
| 227 | |||
| 228 | $displayOrder++; |
||
| 229 | $processedAttendances++; |
||
| 230 | } |
||
| 231 | |||
| 232 | $this->connection->commit(); |
||
| 233 | $processedCourses++; |
||
| 234 | } catch (\Throwable $e) { |
||
| 235 | $this->connection->rollBack(); |
||
| 236 | $io->error("Course {$courseId}: transaction failed - ".$e->getMessage()); |
||
| 237 | return Command::FAILURE; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | $io->success("Done. Courses processed: {$processedCourses}. Attendances migrated: {$processedAttendances}."); |
||
| 242 | |||
| 243 | if ($dropItemProperty) { |
||
| 244 | $this->maybeDropItemProperty($io); |
||
| 245 | } else { |
||
| 246 | if ($this->tableExists('c_item_property')) { |
||
| 247 | $io->note('c_item_property still exists. You can drop it later or rerun this command with --drop-c-item-property once you confirm no pending attendances remain.'); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return Command::SUCCESS; |
||
| 252 | } |
||
| 468 |