| Conditions | 10 |
| Paths | 41 |
| Total Lines | 95 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 26 | public function up(Schema $schema): void |
||
| 27 | { |
||
| 28 | $container = $this->getContainer(); |
||
| 29 | $doctrine = $container->get('doctrine'); |
||
| 30 | $em = $doctrine->getManager(); |
||
| 31 | /** @var Connection $connection */ |
||
| 32 | $connection = $em->getConnection(); |
||
| 33 | $courseRepo = $container->get(CourseRepository::class); |
||
| 34 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 35 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 36 | $groupCategoryRepo = $container->get(CGroupCategoryRepository::class); |
||
| 37 | |||
| 38 | $batchSize = self::BATCH_SIZE; |
||
| 39 | $admin = $this->getAdmin(); |
||
| 40 | |||
| 41 | // Migrating c_tool. |
||
| 42 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 43 | /** @var Course $course */ |
||
| 44 | foreach ($q->toIterable() as $course) { |
||
| 45 | // Categories |
||
| 46 | $counter = 1; |
||
| 47 | $courseId = $course->getId(); |
||
| 48 | $sql = "SELECT * FROM c_group_category |
||
| 49 | WHERE c_id = $courseId "; |
||
| 50 | $result = $connection->executeQuery($sql); |
||
| 51 | $categories = $result->fetchAllAssociative(); |
||
| 52 | |||
| 53 | foreach ($categories as $categoryData) { |
||
| 54 | /** @var CGroupCategory $category */ |
||
| 55 | $category = $groupCategoryRepo->find($categoryData['iid']); |
||
| 56 | if ($category->hasResourceNode()) { |
||
| 57 | continue; |
||
| 58 | } |
||
| 59 | |||
| 60 | $course = $courseRepo->find($courseId); |
||
| 61 | $session = null; |
||
| 62 | /*if (!empty($groupData['session_id'])) { |
||
| 63 | $session = $sessionRepo->find($groupData['session_id']); |
||
| 64 | }*/ |
||
| 65 | |||
| 66 | $admin = $this->getAdmin(); |
||
| 67 | $category->setParent($course); |
||
| 68 | $groupRepo->addResourceNode($category, $admin, $course); |
||
| 69 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 70 | $category->addCourseLink($course, $session, null, $newVisibility); |
||
| 71 | $em->persist($category); |
||
| 72 | if (0 === $counter % $batchSize) { |
||
| 73 | $em->flush(); |
||
| 74 | $em->clear(); |
||
| 75 | } |
||
| 76 | $counter++; |
||
| 77 | } |
||
| 78 | |||
| 79 | $em->flush(); |
||
| 80 | $em->clear(); |
||
| 81 | |||
| 82 | // Groups |
||
| 83 | $counter = 1; |
||
| 84 | $courseId = $course->getId(); |
||
| 85 | $sql = "SELECT * FROM c_group_info |
||
| 86 | WHERE c_id = $courseId "; |
||
| 87 | $result = $connection->executeQuery($sql); |
||
| 88 | $groups = $result->fetchAllAssociative(); |
||
| 89 | |||
| 90 | foreach ($groups as $groupData) { |
||
| 91 | /** @var CGroup $group */ |
||
| 92 | $group = $groupRepo->find($groupData['iid']); |
||
| 93 | if ($group->hasResourceNode()) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | $course = $courseRepo->find($courseId); |
||
| 98 | $session = null; |
||
| 99 | if (!empty($groupData['session_id'])) { |
||
| 100 | $session = $sessionRepo->find($groupData['session_id']); |
||
| 101 | } |
||
| 102 | |||
| 103 | $admin = $this->getAdmin(); |
||
| 104 | $group->setParent($course); |
||
| 105 | $groupRepo->addResourceNode($group, $admin, $course); |
||
| 106 | $newVisibility = ResourceLink::VISIBILITY_PENDING; |
||
| 107 | if (1 === $group->getStatus()) { |
||
| 108 | $newVisibility = ResourceLink::VISIBILITY_PUBLISHED; |
||
| 109 | } |
||
| 110 | $group->addCourseLink($course, $session, null, $newVisibility); |
||
| 111 | $em->persist($group); |
||
| 112 | if (0 === $counter % $batchSize) { |
||
| 113 | $em->flush(); |
||
| 114 | $em->clear(); |
||
| 115 | } |
||
| 116 | $counter++; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | $em->flush(); |
||
| 120 | $em->clear(); |
||
| 121 | } |
||
| 123 |