| Conditions | 13 |
| Paths | 67 |
| Total Lines | 69 |
| Code Lines | 41 |
| 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 |
||
| 17 | public function import(Request $request): void |
||
| 18 | { |
||
| 19 | $this->setAdminPrefs(); |
||
| 20 | |||
| 21 | $filesToProcess = []; |
||
| 22 | $uploadMessages = []; |
||
| 23 | |||
| 24 | if ($this->isPostBack($request)) { |
||
| 25 | $useNzbName = false; |
||
| 26 | $deleteNZB = true; |
||
| 27 | |||
| 28 | // Get the list of NZB files from php /tmp folder if nzb files were uploaded. |
||
| 29 | if (isset($_FILES['uploadedfiles']) && ! empty($_FILES['uploadedfiles']['name'][0])) { |
||
| 30 | $maxFileSize = min( |
||
| 31 | $this->convertToBytes(ini_get('upload_max_filesize')), |
||
| 32 | $this->convertToBytes(ini_get('post_max_size')) |
||
| 33 | ); |
||
| 34 | |||
| 35 | foreach ($_FILES['uploadedfiles']['error'] as $key => $error) { |
||
| 36 | $fileName = $_FILES['uploadedfiles']['name'][$key]; |
||
| 37 | |||
| 38 | if ($error === UPLOAD_ERR_OK) { |
||
| 39 | $tmp_name = $_FILES['uploadedfiles']['tmp_name'][$key]; |
||
| 40 | $filesToProcess[] = $tmp_name; |
||
| 41 | $uploadMessages[] = "File '{$fileName}' uploaded successfully."; |
||
| 42 | } else { |
||
| 43 | // Handle specific upload errors |
||
| 44 | $errorMessage = $this->getUploadErrorMessage($error, $fileName, $maxFileSize); |
||
| 45 | $uploadMessages[] = $errorMessage; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | } else { |
||
| 49 | // Check if the user wants to use the file name as the release name. |
||
| 50 | $useNzbName = ($request->has('usefilename') && $request->input('usefilename') === 'on'); |
||
| 51 | |||
| 52 | // Check if the user wants to delete the NZB file when done importing. |
||
| 53 | $deleteNZB = ($request->has('deleteNZB') && $request->input('deleteNZB') === 'on'); |
||
| 54 | |||
| 55 | // Get the path the user set in the browser if he put one. |
||
| 56 | $path = ($request->has('folder') ? $request->input('folder') : ''); |
||
| 57 | if (! Str::endsWith($path, '/')) { |
||
| 58 | $path .= '/'; |
||
| 59 | } |
||
| 60 | |||
| 61 | // Get the files from the user specified path. |
||
| 62 | $nzbFiles = glob($path.'*.nzb', GLOB_NOSORT); |
||
| 63 | if (empty($nzbFiles)) { |
||
| 64 | $uploadMessages[] = "No NZB files found in the specified path: {$path}"; |
||
| 65 | } else { |
||
| 66 | $filesToProcess = $nzbFiles; |
||
| 67 | $uploadMessages[] = 'Found '.count($nzbFiles).' NZB file(s) in the specified path.'; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | $importResults = []; |
||
| 72 | if (\count($filesToProcess) > 0) { |
||
| 73 | // Create a new instance of NZBImport and send it the file locations. |
||
| 74 | $NZBImport = new NZBImport(['Browser' => true, 'Settings' => null]); |
||
| 75 | $importResults = $NZBImport->beginImport($filesToProcess, $useNzbName, $deleteNZB); |
||
| 76 | } |
||
| 77 | |||
| 78 | $output = implode('<br>', array_merge($uploadMessages, is_array($importResults) ? $importResults : [$importResults])); |
||
| 79 | $this->smarty->assign('output', $output); |
||
| 80 | } |
||
| 81 | |||
| 82 | $meta_title = $title = 'Import Nzbs'; |
||
| 83 | $content = $this->smarty->fetch('nzb-import.tpl'); |
||
| 84 | $this->smarty->assign(compact('title', 'meta_title', 'content')); |
||
| 85 | $this->adminrender(); |
||
| 86 | } |
||
| 199 |