| Total Complexity | 44 |
| Total Lines | 326 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like AbstractMigrationChamilo 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 AbstractMigrationChamilo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class AbstractMigrationChamilo extends AbstractMigration implements ContainerAwareInterface |
||
| 32 | { |
||
| 33 | public const BATCH_SIZE = 20; |
||
| 34 | |||
| 35 | private ?EntityManager $manager = null; |
||
| 36 | private ?ContainerInterface $container = null; |
||
| 37 | |||
| 38 | public function setEntityManager(EntityManager $manager): void |
||
| 39 | { |
||
| 40 | $this->manager = $manager; |
||
| 41 | } |
||
| 42 | |||
| 43 | public function setContainer(?ContainerInterface $container = null): void |
||
| 44 | { |
||
| 45 | $this->container = $container; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return ContainerInterface |
||
| 50 | */ |
||
| 51 | public function getContainer() |
||
| 52 | { |
||
| 53 | return $this->container; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function adminExist(): bool |
||
| 57 | { |
||
| 58 | $em = $this->getEntityManager(); |
||
| 59 | $connection = $em->getConnection(); |
||
| 60 | |||
| 61 | $sql = 'SELECT user_id FROM admin WHERE user_id IN (SELECT id FROM user) ORDER BY id LIMIT 1'; |
||
| 62 | $result = $connection->executeQuery($sql); |
||
| 63 | $adminRow = $result->fetchAssociative(); |
||
| 64 | |||
| 65 | if (empty($adminRow)) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | |||
| 69 | return true; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function getAdmin(): User |
||
| 73 | { |
||
| 74 | $admin = $this->getEntityManager() |
||
| 75 | ->getRepository(Admin::class) |
||
| 76 | ->findOneBy([], ['id' => 'ASC']) |
||
| 77 | ; |
||
| 78 | |||
| 79 | return $admin->getUser(); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return EntityManager |
||
| 84 | */ |
||
| 85 | public function getEntityManager() |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Speeds up SettingsCurrent creation. |
||
| 92 | * |
||
| 93 | * @param string $variable The variable itself |
||
| 94 | * @param string $subKey The subkey |
||
| 95 | * @param string $type The type of setting (text, radio, select, etc) |
||
| 96 | * @param string $category The category (Platform, User, etc) |
||
| 97 | * @param string $selectedValue The default value |
||
| 98 | * @param string $title The setting title string name |
||
| 99 | * @param string $comment The setting comment string name |
||
| 100 | * @param string $scope The scope |
||
| 101 | * @param string $subKeyText Text if there is a subKey |
||
| 102 | * @param int $accessUrl What URL it is for |
||
| 103 | * @param bool $accessUrlChangeable Whether it can be changed on each url |
||
| 104 | * @param bool $accessUrlLocked Whether the setting for the current URL is |
||
| 105 | * locked to the current value |
||
| 106 | * @param array $options Optional array in case of a radio-type field, |
||
| 107 | * to insert options |
||
| 108 | */ |
||
| 109 | public function addSettingCurrent( |
||
| 110 | $variable, |
||
| 111 | $subKey, |
||
| 112 | $type, |
||
| 113 | $category, |
||
| 114 | $selectedValue, |
||
| 115 | $title, |
||
| 116 | $comment, |
||
| 117 | $scope = '', |
||
| 118 | $subKeyText = '', |
||
| 119 | $accessUrl = 1, |
||
| 120 | $accessUrlChangeable = false, |
||
| 121 | $accessUrlLocked = true, |
||
| 122 | $options = [] |
||
| 123 | ): void { |
||
| 124 | $em = $this->getEntityManager(); |
||
| 125 | |||
| 126 | $accessUrl = $em->find(AccessUrl::class, $accessUrl); |
||
| 127 | |||
| 128 | $setting = new SettingsCurrent(); |
||
| 129 | $setting |
||
| 130 | ->setVariable($variable) |
||
| 131 | ->setSubkey($subKey) |
||
| 132 | ->setType($type) |
||
| 133 | ->setCategory($category) |
||
| 134 | ->setSelectedValue($selectedValue) |
||
| 135 | ->setTitle($title) |
||
| 136 | ->setComment($comment) |
||
| 137 | ->setScope($scope) |
||
| 138 | ->setSubkeytext($subKeyText) |
||
| 139 | ->setUrl($accessUrl) |
||
| 140 | ->setAccessUrlChangeable((int) $accessUrlChangeable) |
||
| 141 | ->setAccessUrlLocked((int) $accessUrlLocked) |
||
| 142 | ; |
||
| 143 | |||
| 144 | $em->persist($setting); |
||
| 145 | |||
| 146 | if (\count($options) > 0) { |
||
| 147 | foreach ($options as $option) { |
||
| 148 | if (empty($option['text'])) { |
||
| 149 | if ('true' === $option['value']) { |
||
| 150 | $option['text'] = 'Yes'; |
||
| 151 | } else { |
||
| 152 | $option['text'] = 'No'; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $settingOption = new SettingsOptions(); |
||
| 157 | $settingOption |
||
| 158 | ->setVariable($variable) |
||
| 159 | ->setValue($option['value']) |
||
| 160 | ->setDisplayText($option['text']) |
||
| 161 | ; |
||
| 162 | |||
| 163 | $em->persist($settingOption); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | $em->flush(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $variable |
||
| 171 | * @param null|mixed $configuration |
||
| 172 | */ |
||
| 173 | public function getConfigurationValue($variable, $configuration = null) |
||
| 174 | { |
||
| 175 | global $_configuration; |
||
| 176 | |||
| 177 | if (isset($configuration)) { |
||
| 178 | $_configuration = $configuration; |
||
| 179 | } |
||
| 180 | |||
| 181 | if (isset($_configuration[$variable])) { |
||
| 182 | return $_configuration[$variable]; |
||
| 183 | } |
||
| 184 | |||
| 185 | return false; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Remove a setting completely. |
||
| 190 | * |
||
| 191 | * @param string $variable The setting variable name |
||
| 192 | */ |
||
| 193 | public function removeSettingCurrent($variable): void |
||
| 194 | { |
||
| 195 | // to be implemented |
||
| 196 | } |
||
| 197 | |||
| 198 | public function addLegacyFileToResource( |
||
| 223 | } |
||
| 224 | |||
| 225 | public function fixItemProperty( |
||
| 226 | $tool, |
||
| 227 | ResourceRepository $repo, |
||
| 228 | $course, |
||
| 229 | $admin, |
||
| 230 | ResourceInterface $resource, |
||
| 231 | $parentResource, |
||
| 232 | array $items = [] |
||
| 233 | ) { |
||
| 234 | $container = $this->getContainer(); |
||
| 235 | $em = $this->getEntityManager(); |
||
| 236 | $connection = $em->getConnection(); |
||
| 237 | |||
| 238 | $courseId = $course->getId(); |
||
| 239 | $id = $resource->getResourceIdentifier(); |
||
| 240 | |||
| 241 | if (empty($items)) { |
||
| 242 | $sql = "SELECT * FROM c_item_property |
||
| 243 | WHERE tool = '{$tool}' AND c_id = {$courseId} AND ref = {$id}"; |
||
| 244 | $result = $connection->executeQuery($sql); |
||
| 245 | $items = $result->fetchAllAssociative(); |
||
| 246 | } |
||
| 247 | |||
| 248 | // For some reason the resource doesn't have a c_item_property value. |
||
| 249 | if (empty($items)) { |
||
| 250 | return false; |
||
| 251 | } |
||
| 252 | |||
| 253 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 254 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 255 | $userRepo = $container->get(UserRepository::class); |
||
| 256 | |||
| 257 | $resource->setParent($parentResource); |
||
|
|
|||
| 258 | $resourceNode = null; |
||
| 259 | $userList = []; |
||
| 260 | $groupList = []; |
||
| 261 | $sessionList = []; |
||
| 262 | foreach ($items as $item) { |
||
| 263 | $visibility = (int) $item['visibility']; |
||
| 264 | $userId = (int) $item['insert_user_id']; |
||
| 265 | $sessionId = $item['session_id'] ?? 0; |
||
| 266 | $groupId = $item['to_group_id'] ?? 0; |
||
| 267 | $lastUpdatedAt = new DateTime($item['lastedit_date'], new DateTimeZone('UTC')); |
||
| 268 | |||
| 269 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
| 270 | |||
| 271 | // Old 1.11.x visibility (item property) is based in this switch: |
||
| 272 | switch ($visibility) { |
||
| 273 | case 0: |
||
| 274 | $newVisibility = ResourceLink::VISIBILITY_DRAFT; |
||
| 275 | |||
| 276 | break; |
||
| 277 | |||
| 278 | case 1: |
||
| 279 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 280 | |||
| 281 | break; |
||
| 282 | } |
||
| 283 | |||
| 284 | // If c_item_property.insert_user_id doesn't exist we use the first admin id. |
||
| 285 | $user = null; |
||
| 286 | if (isset($userList[$userId])) { |
||
| 287 | $user = $userList[$userId]; |
||
| 288 | } else { |
||
| 289 | if (!empty($userId)) { |
||
| 290 | $userFound = $userRepo->find($userId); |
||
| 291 | if ($userFound) { |
||
| 292 | $user = $userList[$userId] = $userRepo->find($userId); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | if (null === $user) { |
||
| 298 | $user = $admin; |
||
| 299 | } |
||
| 300 | |||
| 301 | $session = null; |
||
| 302 | if (!empty($sessionId)) { |
||
| 303 | if (isset($sessionList[$sessionId])) { |
||
| 304 | $session = $sessionList[$sessionId]; |
||
| 305 | } else { |
||
| 306 | $session = $sessionList[$sessionId] = $sessionRepo->find($sessionId); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | $group = null; |
||
| 311 | if (!empty($groupId)) { |
||
| 312 | if (isset($groupList[$groupId])) { |
||
| 313 | $group = $groupList[$groupId]; |
||
| 314 | } else { |
||
| 315 | $group = $groupList[$groupId] = $groupRepo->find($groupId); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | if (null === $resourceNode) { |
||
| 320 | $resourceNode = $repo->addResourceNode($resource, $user, $parentResource); |
||
| 321 | $em->persist($resourceNode); |
||
| 322 | } |
||
| 323 | $resource->addCourseLink($course, $session, $group, $newVisibility); |
||
| 324 | |||
| 325 | if (2 === $visibility) { |
||
| 326 | $link = $resource->getResourceNode()->getResourceLinkByContext($course, $session, $group); |
||
| 327 | $link->setDeletedAt($lastUpdatedAt); |
||
| 328 | } |
||
| 329 | |||
| 330 | $em->persist($resource); |
||
| 331 | } |
||
| 332 | |||
| 333 | return true; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function fileExists($filePath): bool |
||
| 339 | } |
||
| 340 | |||
| 341 | public function findCourse(int $id): ?Course |
||
| 348 | } |
||
| 349 | |||
| 350 | public function findSession(int $id): ?Session |
||
| 357 | } |
||
| 358 | } |
||
| 359 |