| Conditions | 7 |
| Paths | 7 |
| Total Lines | 116 |
| Code Lines | 83 |
| 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 |
||
| 46 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 47 | { |
||
| 48 | $io = new SymfonyStyle($input, $output); |
||
| 49 | $adminUser = $this->getFirstAdmin(); |
||
| 50 | if (!$adminUser) { |
||
| 51 | $io->error('No admin user found in the system.'); |
||
| 52 | return Command::FAILURE; |
||
| 53 | } |
||
| 54 | |||
| 55 | $folder = $input->getArgument('folder'); |
||
| 56 | if (!is_dir($folder)) { |
||
| 57 | $io->error("Invalid folder: $folder"); |
||
| 58 | return Command::FAILURE; |
||
| 59 | } |
||
| 60 | |||
| 61 | $finder = new Finder(); |
||
| 62 | $finder->files()->in($folder); |
||
| 63 | |||
| 64 | foreach ($finder as $file) { |
||
| 65 | $basename = $file->getBasename(); |
||
| 66 | $courseCode = pathinfo($basename, PATHINFO_FILENAME); |
||
| 67 | $filePath = $file->getRealPath(); |
||
| 68 | |||
| 69 | // Skip unsupported extensions |
||
| 70 | $allowedExtensions = ['pdf', 'html', 'htm', 'mp4']; |
||
| 71 | if (!in_array(strtolower($file->getExtension()), $allowedExtensions)) { |
||
| 72 | $io->warning("Skipping unsupported file: $basename"); |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | |||
| 76 | $io->section("Creating course: $courseCode"); |
||
| 77 | |||
| 78 | // Step 1: Create the course |
||
| 79 | $course = $this->courseService->createCourse([ |
||
| 80 | 'title' => $courseCode, |
||
| 81 | 'wanted_code' => $courseCode, |
||
| 82 | 'add_user_as_teacher' => true, |
||
| 83 | 'course_language' => $this->settingsManager->getSetting('language.platform_language'), |
||
| 84 | 'visibility' => Course::OPEN_PLATFORM, |
||
| 85 | 'subscribe' => true, |
||
| 86 | 'unsubscribe' => true, |
||
| 87 | 'disk_quota' => $this->settingsManager->getSetting('document.default_document_quotum'), |
||
| 88 | 'expiration_date' => (new \DateTime('+1 year'))->format('Y-m-d H:i:s') |
||
| 89 | ]); |
||
| 90 | |||
| 91 | if (!$course) { |
||
| 92 | throw new \RuntimeException('Error: Course could not be created.'); |
||
| 93 | } |
||
| 94 | |||
| 95 | // Step 2: Create learning path (CLp) |
||
| 96 | $lp = (new CLp()) |
||
| 97 | ->setLpType(1) |
||
| 98 | ->setTitle($courseCode) |
||
| 99 | ->setDescription('') |
||
| 100 | ->setPublishedOn(null) |
||
| 101 | ->setExpiredOn(null) |
||
| 102 | ->setCategory(null) |
||
| 103 | ->setParent($course) |
||
| 104 | ->addCourseLink($course); |
||
| 105 | $lp->setCreator($adminUser); |
||
| 106 | |||
| 107 | $lpRepo = $this->em->getRepository(CLp::class); |
||
| 108 | $lpRepo->createLp($lp); |
||
| 109 | |||
| 110 | // Step 3: Create CDocument from uploaded file |
||
| 111 | $document = new CDocument(); |
||
| 112 | $document->setFiletype('file') |
||
| 113 | ->setTitle($basename) |
||
| 114 | ->setComment(null) |
||
| 115 | ->setReadonly(false) |
||
| 116 | ->setCreator($adminUser) |
||
| 117 | ->setParent($course) |
||
| 118 | ->addCourseLink($course); |
||
| 119 | |||
| 120 | $this->em->persist($document); |
||
| 121 | $this->em->flush(); |
||
| 122 | |||
| 123 | $documentRepo = $this->em->getRepository(CDocument::class); |
||
| 124 | $documentRepo->addFileFromPath($document, $basename, $filePath); |
||
| 125 | |||
| 126 | // Step 4: Create LP item linked to the document |
||
| 127 | // Ensure root item exists |
||
| 128 | $lpItemRepo = $this->em->getRepository(CLpItem::class); |
||
| 129 | $rootItem = $lpItemRepo->getRootItem((int) $lp->getIid()); |
||
| 130 | |||
| 131 | if (!$rootItem) { |
||
| 132 | $rootItem = (new CLpItem()) |
||
| 133 | ->setTitle('root') |
||
| 134 | ->setPath('root') |
||
| 135 | ->setLp($lp) |
||
| 136 | ->setItemType('root'); |
||
| 137 | $this->em->persist($rootItem); |
||
| 138 | $this->em->flush(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $lpItem = (new CLpItem()) |
||
| 142 | ->setLp($lp) |
||
| 143 | ->setTitle($basename) |
||
| 144 | ->setItemType('document') |
||
| 145 | ->setRef((string) $document->getIid()) |
||
| 146 | ->setPath((string) $document->getIid()) |
||
| 147 | ->setDisplayOrder(1) |
||
| 148 | ->setLaunchData('') |
||
| 149 | ->setMinScore(0) |
||
| 150 | ->setMaxScore(100) |
||
| 151 | ->setParent($rootItem) |
||
| 152 | ->setLvl(1) |
||
| 153 | ->setRoot($rootItem); |
||
| 154 | |||
| 155 | $this->em->persist($lpItem); |
||
| 156 | $this->em->flush(); |
||
| 157 | |||
| 158 | $io->success("Course '$courseCode' created with LP and document item '$basename'"); |
||
| 159 | } |
||
| 160 | |||
| 161 | return Command::SUCCESS; |
||
| 162 | } |
||
| 175 |