| Conditions | 15 |
| Paths | 315 |
| Total Lines | 113 |
| Code Lines | 74 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 30 | public function up(Schema $schema): void |
||
| 31 | { |
||
| 32 | $container = $this->getContainer(); |
||
| 33 | $em = $this->getEntityManager(); |
||
| 34 | $connection = $em->getConnection(); |
||
| 35 | |||
| 36 | $courseRepo = $container->get(CourseRepository::class); |
||
| 37 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 38 | $toolRepo = $container->get(CToolRepository::class); |
||
| 39 | $urlRepo = $em->getRepository(AccessUrl::class); |
||
| 40 | $userRepo = $container->get(UserRepository::class); |
||
| 41 | |||
| 42 | $batchSize = self::BATCH_SIZE; |
||
| 43 | $admin = $this->getAdmin(); |
||
| 44 | $adminId = $admin->getId(); |
||
| 45 | |||
| 46 | // Adding courses to the resource node tree. |
||
| 47 | $urls = $urlRepo->findAll(); |
||
| 48 | |||
| 49 | /** @var AccessUrl $url */ |
||
| 50 | foreach ($urls as $url) { |
||
| 51 | $counter = 1; |
||
| 52 | /** @var AccessUrl $urlEntity */ |
||
| 53 | $urlEntity = $urlRepo->find($url->getId()); |
||
| 54 | $accessUrlRelCourses = $urlEntity->getCourses(); |
||
| 55 | /** @var AccessUrlRelCourse $accessUrlRelCourse */ |
||
| 56 | foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
||
| 57 | $course = $accessUrlRelCourse->getCourse(); |
||
| 58 | $course = $courseRepo->find($course->getId()); |
||
| 59 | if ($course->hasResourceNode()) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | $urlEntity = $urlRepo->find($url->getId()); |
||
| 63 | $adminEntity = $userRepo->find($adminId); |
||
| 64 | $courseRepo->addResourceNode($course, $adminEntity, $urlEntity); |
||
| 65 | $em->persist($course); |
||
| 66 | |||
| 67 | // Add groups. |
||
| 68 | //$course = $course->getGroups(); |
||
| 69 | if (($counter % $batchSize) === 0) { |
||
| 70 | $em->flush(); |
||
| 71 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 72 | } |
||
| 73 | $counter++; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $em->flush(); |
||
| 78 | $em->clear(); |
||
| 79 | |||
| 80 | // Special course. |
||
| 81 | $extraFieldType = ExtraField::COURSE_FIELD_TYPE; |
||
| 82 | $sql = "SELECT id FROM extra_field |
||
| 83 | WHERE extra_field_type = $extraFieldType AND variable = 'special_course'"; |
||
| 84 | $result = $connection->executeQuery($sql); |
||
| 85 | $extraFieldId = $result->fetchOne(); |
||
| 86 | |||
| 87 | $specialCourses = ''; |
||
| 88 | if (!empty($extraFieldId)) { |
||
| 89 | $sql = 'SELECT DISTINCT(item_id) |
||
| 90 | FROM extra_field_values |
||
| 91 | WHERE field_id = '.$extraFieldId." AND value = '1'"; |
||
| 92 | $result = $connection->executeQuery($sql); |
||
| 93 | $specialCourses = $result->fetchAllAssociative(); |
||
| 94 | if (!empty($specialCourses)) { |
||
| 95 | $specialCourses = array_column($specialCourses, 'item_id'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Migrating c_tool. |
||
| 100 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 101 | /** @var Course $course */ |
||
| 102 | foreach ($q->toIterable() as $course) { |
||
| 103 | $counter = 1; |
||
| 104 | $courseId = $course->getId(); |
||
| 105 | |||
| 106 | if (!empty($specialCourses) && in_array($courseId, $specialCourses)) { |
||
| 107 | $this->addSql("UPDATE course SET sticky = 1 WHERE id = $courseId "); |
||
| 108 | } |
||
| 109 | |||
| 110 | $sql = "SELECT * FROM c_tool |
||
| 111 | WHERE c_id = {$courseId} "; |
||
| 112 | $result = $connection->executeQuery($sql); |
||
| 113 | $tools = $result->fetchAllAssociative(); |
||
| 114 | |||
| 115 | foreach ($tools as $toolData) { |
||
| 116 | /** @var CTool $tool */ |
||
| 117 | $tool = $toolRepo->find($toolData['iid']); |
||
| 118 | if ($tool->hasResourceNode()) { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | $course = $courseRepo->find($courseId); |
||
| 123 | $session = null; |
||
| 124 | if (!empty($toolData['session_id'])) { |
||
| 125 | $session = $sessionRepo->find($toolData['session_id']); |
||
| 126 | } |
||
| 127 | |||
| 128 | $admin = $this->getAdmin(); |
||
| 129 | $tool->setParent($course); |
||
| 130 | $toolRepo->addResourceNode($tool, $admin, $course); |
||
| 131 | $newVisibility = 1 === (int) $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_PENDING; |
||
| 132 | $tool->addCourseLink($course, $session, null, $newVisibility); |
||
| 133 | $em->persist($tool); |
||
| 134 | if (($counter % $batchSize) === 0) { |
||
| 135 | $em->flush(); |
||
| 136 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 137 | } |
||
| 138 | $counter++; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $em->flush(); |
||
| 142 | $em->clear(); |
||
| 143 | } |
||
| 145 |