| Conditions | 11 |
| Paths | 45 |
| Total Lines | 105 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 28 | public function up(Schema $schema): void |
||
| 29 | { |
||
| 30 | $container = $this->getContainer(); |
||
| 31 | $doctrine = $container->get('doctrine'); |
||
| 32 | $em = $doctrine->getManager(); |
||
| 33 | /** @var Connection $connection */ |
||
| 34 | $connection = $em->getConnection(); |
||
| 35 | |||
| 36 | $urlRepo = $container->get(AccessUrlRepository::class); |
||
| 37 | $eventRepo = $container->get(CCalendarEventRepository::class); |
||
| 38 | $eventAttachmentRepo = $container->get(CCalendarEventAttachmentRepository::class); |
||
| 39 | $courseRepo = $container->get(CourseRepository::class); |
||
| 40 | $sessionRepo = $container->get(SessionRepository::class); |
||
| 41 | $groupRepo = $container->get(CGroupRepository::class); |
||
| 42 | $userRepo = $container->get(UserRepository::class); |
||
| 43 | |||
| 44 | /** @var Kernel $kernel */ |
||
| 45 | $kernel = $container->get('kernel'); |
||
| 46 | $rootPath = $kernel->getProjectDir(); |
||
| 47 | $admin = $this->getAdmin(); |
||
| 48 | $urls = $urlRepo->findAll(); |
||
| 49 | |||
| 50 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
| 51 | foreach ($q->toIterable() as $course) { |
||
| 52 | $counter = 1; |
||
| 53 | $courseId = $course->getId(); |
||
| 54 | $course = $courseRepo->find($courseId); |
||
| 55 | |||
| 56 | $sql = "SELECT * FROM c_calendar_event WHERE c_id = $courseId |
||
| 57 | ORDER BY iid"; |
||
| 58 | $result = $connection->executeQuery($sql); |
||
| 59 | $events = $result->fetchAllAssociative(); |
||
| 60 | foreach ($events as $eventData) { |
||
| 61 | $id = $eventData['iid']; |
||
| 62 | /** @var CCalendarEvent $event */ |
||
| 63 | $event = $eventRepo->find($id); |
||
| 64 | if ($event->hasResourceNode()) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | $sql = "SELECT * FROM c_item_property |
||
| 69 | WHERE tool = 'calendar_event' AND c_id = $courseId AND ref = $id"; |
||
| 70 | $result = $connection->executeQuery($sql); |
||
| 71 | $items = $result->fetchAllAssociative(); |
||
| 72 | |||
| 73 | // For some reason this event doesnt have a c_item_property value, then we added to the main course. |
||
| 74 | if (empty($items)) { |
||
| 75 | $items[] = [ |
||
| 76 | 'visibility' => 1, |
||
| 77 | 'insert_user_id' => $admin->getId(), |
||
| 78 | 'to_group_id' => 0, |
||
| 79 | 'session_id' => $eventData['session_id'], |
||
| 80 | ]; |
||
| 81 | $this->fixItemProperty($eventRepo, $course, $admin, $event, $course, $items); |
||
| 82 | $em->persist($event); |
||
| 83 | $em->flush(); |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | $parent = null; |
||
| 87 | if (!empty($eventData['parent_event_id'])) { |
||
| 88 | $parent = $eventRepo->find($eventData['parent_event_id']); |
||
| 89 | } |
||
| 90 | if (null === $parent) { |
||
| 91 | $parent = $course; |
||
| 92 | } |
||
| 93 | $result = $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $parent); |
||
| 94 | |||
| 95 | if (false === $result) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | $em->persist($event); |
||
| 99 | $em->flush(); |
||
| 100 | } |
||
| 101 | |||
| 102 | $sql = "SELECT * FROM c_calendar_event_attachment WHERE c_id = $courseId |
||
| 103 | ORDER BY iid"; |
||
| 104 | $result = $connection->executeQuery($sql); |
||
| 105 | $attachments = $result->fetchAllAssociative(); |
||
| 106 | foreach ($attachments as $attachmentData) { |
||
| 107 | $id = $attachmentData['iid']; |
||
| 108 | $attachmentPath = $attachmentData['path']; |
||
| 109 | $fileName = $attachmentData['filename']; |
||
| 110 | /** @var CCalendarEventAttachment $attachment */ |
||
| 111 | $attachment = $eventAttachmentRepo->find($id); |
||
| 112 | if ($attachment->hasResourceNode()) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | $parent = $attachment->getEvent(); |
||
| 116 | $result = $this->fixItemProperty( |
||
| 117 | 'calendar_event_attachment', |
||
| 118 | $eventRepo, |
||
| 119 | $course, |
||
| 120 | $admin, |
||
| 121 | $attachment, |
||
| 122 | $parent |
||
| 123 | ); |
||
| 124 | |||
| 125 | if (false === $result) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/calendar/'.$attachmentPath; |
||
| 130 | $this->addLegacyFileToResource($filePath, $eventAttachmentRepo, $attachment, $id, $fileName); |
||
| 131 | $em->persist($attachment); |
||
| 132 | $em->flush(); |
||
| 133 | } |
||
| 141 |