| Conditions | 12 |
| Paths | 60 |
| Total Lines | 84 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 23 | public function up(Schema $schema): void |
||
| 24 | { |
||
| 25 | $container = $this->getContainer(); |
||
| 26 | $doctrine = $container->get('doctrine'); |
||
| 27 | $em = $doctrine->getManager(); |
||
| 28 | /** @var Connection $connection */ |
||
| 29 | $connection = $em->getConnection(); |
||
| 30 | $courseRepo = $container->get(CourseRepository::class); |
||
| 31 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 32 | $toolRepo = $container->get(CToolRepository::class); |
||
| 33 | $urlRepo = $em->getRepository(AccessUrl::class); |
||
| 34 | |||
| 35 | $batchSize = self::BATCH_SIZE; |
||
| 36 | $admin = $this->getAdmin(); |
||
| 37 | |||
| 38 | // Adding courses to the resource node tree. |
||
| 39 | $urls = $urlRepo->findAll(); |
||
| 40 | /** @var AccessUrl $url */ |
||
| 41 | foreach ($urls as $url) { |
||
| 42 | $counter = 1; |
||
| 43 | $url = $urlRepo->find($url->getId()); |
||
| 44 | $accessUrlRelCourses = $url->getCourses(); |
||
| 45 | /** @var AccessUrlRelCourse $accessUrlRelCourse */ |
||
| 46 | foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
||
| 47 | $course = $accessUrlRelCourse->getCourse(); |
||
| 48 | $course = $courseRepo->find($course->getId()); |
||
| 49 | if ($course->hasResourceNode()) { |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | $courseRepo->addResourceNode($course, $admin, $url); |
||
| 53 | $em->persist($course); |
||
| 54 | |||
| 55 | // Add groups. |
||
| 56 | //$course = $course->getGroups(); |
||
| 57 | if (0 === $counter % $batchSize) { |
||
| 58 | $em->flush(); |
||
| 59 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 60 | } |
||
| 61 | $counter++; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | $em->flush(); |
||
| 65 | $em->clear(); |
||
| 66 | |||
| 67 | foreach ($urls as $url) { |
||
| 68 | $accessUrlRelCourses = $url->getCourses(); |
||
| 69 | /** @var AccessUrlRelCourse $accessUrlRelCourse */ |
||
| 70 | foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
||
| 71 | $counter = 1; |
||
| 72 | $course = $accessUrlRelCourse->getCourse(); |
||
| 73 | $courseId = $course->getId(); |
||
| 74 | $sql = "SELECT * FROM c_tool |
||
| 75 | WHERE c_id = $courseId "; |
||
| 76 | $result = $connection->executeQuery($sql); |
||
| 77 | $tools = $result->fetchAllAssociative(); |
||
| 78 | foreach ($tools as $toolData) { |
||
| 79 | /** @var CTool $tool */ |
||
| 80 | $tool = $toolRepo->find($toolData['iid']); |
||
| 81 | if ($tool->hasResourceNode()) { |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | $course = $courseRepo->find($course->getId()); |
||
| 85 | $session = null; |
||
| 86 | if (!empty($toolData['session_id'])) { |
||
| 87 | $session = $sessionRepo->find($toolData['session_id']); |
||
| 88 | } |
||
| 89 | |||
| 90 | $admin = $this->getAdmin(); |
||
| 91 | $tool->setParent($course); |
||
| 92 | $toolRepo->addResourceNode($tool, $admin, $course); |
||
| 93 | $newVisibility = 1 === $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_PENDING; |
||
| 94 | $tool->addCourseLink($course, $session, null, $newVisibility); |
||
| 95 | $em->persist($tool); |
||
| 96 | if (0 === $counter % $batchSize) { |
||
| 97 | $em->flush(); |
||
| 98 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 99 | } |
||
| 100 | $counter++; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $em->flush(); |
||
| 106 | $em->clear(); |
||
| 107 | } |
||
| 109 |