| Conditions | 14 |
| Paths | 30 |
| Total Lines | 146 |
| Code Lines | 99 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 62 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 63 | { |
||
| 64 | $io = new SymfonyStyle($input, $output); |
||
| 65 | |||
| 66 | $expectedUser = $input->getOption('user'); |
||
| 67 | $realUser = get_current_user(); |
||
| 68 | |||
| 69 | if ($realUser !== $expectedUser) { |
||
| 70 | $io->warning("You are running this command as '$realUser', but expected user is '$expectedUser'.If file permissions are too restrictive (e.g. 0660), the web server may not be able to access the files. |
||
| 71 | To avoid this issue, consider running the command like this: sudo -u {$expectedUser} php bin/console app:create-courses-from-structured-file /path/to/folder"); |
||
| 72 | } |
||
| 73 | |||
| 74 | $adminUser = $this->getFirstAdmin(); |
||
| 75 | if (!$adminUser) { |
||
| 76 | $io->error('No admin user found in the system.'); |
||
| 77 | return Command::FAILURE; |
||
| 78 | } |
||
| 79 | |||
| 80 | $folder = $input->getArgument('folder'); |
||
| 81 | if (!is_dir($folder)) { |
||
| 82 | $io->error("Invalid folder: $folder"); |
||
| 83 | return Command::FAILURE; |
||
| 84 | } |
||
| 85 | |||
| 86 | // Retrieve Unix permissions from platform settings |
||
| 87 | $dirPermOct = octdec($this->settingsManager->getSetting('document.permissions_for_new_directories') ?? '0777'); |
||
| 88 | $filePermOct = octdec($this->settingsManager->getSetting('document.permissions_for_new_files') ?? '0666'); |
||
| 89 | |||
| 90 | // Absolute base to /var/upload/resource |
||
| 91 | $uploadBase = $this->parameterBag->get('kernel.project_dir') . '/var/upload/resource'; |
||
| 92 | |||
| 93 | $finder = new Finder(); |
||
| 94 | $finder->files()->in($folder); |
||
| 95 | |||
| 96 | foreach ($finder as $file) { |
||
| 97 | $basename = $file->getBasename(); |
||
| 98 | $courseCode = pathinfo($basename, PATHINFO_FILENAME); |
||
| 99 | $filePath = $file->getRealPath(); |
||
| 100 | |||
| 101 | // 1. Skip unsupported file extensions |
||
| 102 | $allowedExtensions = ['pdf', 'html', 'htm', 'mp4']; |
||
| 103 | if (!in_array(strtolower($file->getExtension()), $allowedExtensions, true)) { |
||
| 104 | $io->warning("Skipping unsupported file: $basename"); |
||
| 105 | continue; |
||
| 106 | } |
||
| 107 | |||
| 108 | $io->section("Creating course: $courseCode"); |
||
| 109 | |||
| 110 | // 2. Create course |
||
| 111 | $course = $this->courseService->createCourse([ |
||
| 112 | 'title' => $courseCode, |
||
| 113 | 'wanted_code' => $courseCode, |
||
| 114 | 'add_user_as_teacher' => true, |
||
| 115 | 'course_language' => $this->settingsManager->getSetting('language.platform_language'), |
||
| 116 | 'visibility' => Course::OPEN_PLATFORM, |
||
| 117 | 'subscribe' => true, |
||
| 118 | 'unsubscribe' => true, |
||
| 119 | 'disk_quota' => $this->settingsManager->getSetting('document.default_document_quotum'), |
||
| 120 | 'expiration_date' => (new \DateTime('+1 year'))->format('Y-m-d H:i:s'), |
||
| 121 | ]); |
||
| 122 | |||
| 123 | if (!$course) { |
||
| 124 | throw new \RuntimeException("Course '$courseCode' could not be created."); |
||
| 125 | } |
||
| 126 | |||
| 127 | // 3. Create learning path |
||
| 128 | $lp = (new CLp()) |
||
| 129 | ->setLpType(1) |
||
| 130 | ->setTitle($courseCode) |
||
| 131 | ->setDescription('') |
||
| 132 | ->setPublishedOn(null) |
||
| 133 | ->setExpiredOn(null) |
||
| 134 | ->setCategory(null) |
||
| 135 | ->setParent($course) |
||
| 136 | ->addCourseLink($course) |
||
| 137 | ->setCreator($adminUser); |
||
| 138 | |||
| 139 | $this->em->getRepository(CLp::class)->createLp($lp); |
||
| 140 | |||
| 141 | // 4. Create document |
||
| 142 | $document = (new CDocument()) |
||
| 143 | ->setFiletype('file') |
||
| 144 | ->setTitle($basename) |
||
| 145 | ->setComment(null) |
||
| 146 | ->setReadonly(false) |
||
| 147 | ->setCreator($adminUser) |
||
| 148 | ->setParent($course) |
||
| 149 | ->addCourseLink($course); |
||
| 150 | |||
| 151 | $this->em->persist($document); |
||
| 152 | $this->em->flush(); |
||
| 153 | |||
| 154 | $documentRepo = $this->em->getRepository(CDocument::class); |
||
| 155 | $resourceFile = $documentRepo->addFileFromPath($document, $basename, $filePath); |
||
| 156 | |||
| 157 | // 4.1 Apply permissions to the real file & its directory |
||
| 158 | if ($resourceFile) { |
||
| 159 | $resourceNodeRepo = $this->em->getRepository(ResourceNode::class); |
||
| 160 | $relativePath = $resourceNodeRepo->getFilename($resourceFile); |
||
| 161 | $fullPath = realpath($uploadBase . $relativePath); |
||
| 162 | |||
| 163 | if ($fullPath && is_file($fullPath)) { |
||
| 164 | @chmod($fullPath, $filePermOct); |
||
| 165 | } |
||
| 166 | $fullDir = dirname($fullPath ?: ''); |
||
| 167 | if ($fullDir && is_dir($fullDir)) { |
||
| 168 | @chmod($fullDir, $dirPermOct); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // 5. Ensure learning path root item exists |
||
| 173 | $lpItemRepo = $this->em->getRepository(CLpItem::class); |
||
| 174 | $rootItem = $lpItemRepo->getRootItem((int) $lp->getIid()); |
||
| 175 | |||
| 176 | if (!$rootItem) { |
||
| 177 | $rootItem = (new CLpItem()) |
||
| 178 | ->setTitle('root') |
||
| 179 | ->setPath('root') |
||
| 180 | ->setLp($lp) |
||
| 181 | ->setItemType('root'); |
||
| 182 | $this->em->persist($rootItem); |
||
| 183 | $this->em->flush(); |
||
| 184 | } |
||
| 185 | |||
| 186 | // 6. Create LP item linked to the document |
||
| 187 | $lpItem = (new CLpItem()) |
||
| 188 | ->setLp($lp) |
||
| 189 | ->setTitle($basename) |
||
| 190 | ->setItemType('document') |
||
| 191 | ->setRef((string) $document->getIid()) |
||
| 192 | ->setPath((string) $document->getIid()) |
||
| 193 | ->setDisplayOrder(1) |
||
| 194 | ->setLaunchData('') |
||
| 195 | ->setMinScore(0) |
||
| 196 | ->setMaxScore(100) |
||
| 197 | ->setParent($rootItem) |
||
| 198 | ->setLvl(1) |
||
| 199 | ->setRoot($rootItem); |
||
| 200 | |||
| 201 | $this->em->persist($lpItem); |
||
| 202 | $this->em->flush(); |
||
| 203 | |||
| 204 | $io->success("Course '$courseCode' created with LP and document '$basename'"); |
||
| 205 | } |
||
| 206 | |||
| 207 | return Command::SUCCESS; |
||
| 208 | } |
||
| 224 |