| Conditions | 27 |
| Paths | 140 |
| Total Lines | 153 |
| Code Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 22 | class GetNzbController extends BasePageController |
||
| 23 | { |
||
| 24 | private const int BUFFER_SIZE = 1000000; |
||
|
|
|||
| 25 | |||
| 26 | private const string NZB_SUFFIX = '.nzb'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Download NZB file(s) for authenticated users |
||
| 30 | * |
||
| 31 | * @return Application|ResponseFactory|\Illuminate\Foundation\Application|JsonResponse|Response|ZipStream|StreamedResponse |
||
| 32 | * |
||
| 33 | * @throws Exception |
||
| 34 | */ |
||
| 35 | public function getNzb(Request $request, ?string $guid = null) |
||
| 36 | { |
||
| 37 | // Normalize guid parameter |
||
| 38 | $this->normalizeGuidParameter($request, $guid); |
||
| 39 | |||
| 40 | // Authenticate and authorize user |
||
| 41 | $userData = $this->authenticateUser($request); |
||
| 42 | if (! \is_array($userData)) { |
||
| 43 | return $userData; // Return error response |
||
| 44 | } |
||
| 45 | |||
| 46 | ['uid' => $uid, 'userName' => $userName, 'maxDownloads' => $maxDownloads, 'rssToken' => $rssToken] = $userData; |
||
| 47 | |||
| 48 | // Check download limits |
||
| 49 | $downloadLimitError = $this->checkDownloadLimit($uid, $maxDownloads); |
||
| 50 | if ($downloadLimitError !== null) { |
||
| 51 | return $downloadLimitError; |
||
| 52 | } |
||
| 53 | |||
| 54 | // Validate and sanitize ID parameter |
||
| 55 | $releaseId = $this->validateAndSanitizeId($request); |
||
| 56 | if (! \is_string($releaseId)) { |
||
| 57 | return $releaseId; // Return error response |
||
| 58 | } |
||
| 59 | |||
| 60 | // Handle zip download request |
||
| 61 | if ($this->isZipRequest($request)) { |
||
| 62 | return $this->handleZipDownload($request, $uid, $userName, $maxDownloads, $releaseId); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Handle single NZB download |
||
| 66 | return $this->handleSingleNzbDownload($request, $uid, $rssToken, $releaseId); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Normalize the guid parameter into the request |
||
| 71 | */ |
||
| 72 | private function normalizeGuidParameter(Request $request, ?string $guid): void |
||
| 73 | { |
||
| 74 | if ($guid !== null && ! $request->has('id')) { |
||
| 75 | $request->merge(['id' => $guid]); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Authenticate user via session or RSS token |
||
| 81 | * |
||
| 82 | * @return array<string, mixed>|Response |
||
| 83 | */ |
||
| 84 | private function authenticateUser(Request $request) |
||
| 85 | { |
||
| 86 | // Try session authentication first |
||
| 87 | if ($request->user()) { |
||
| 88 | return $this->getUserDataFromSession(); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Try RSS token authentication |
||
| 92 | return $this->getUserDataFromRssToken($request); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get user data from authenticated session |
||
| 97 | * |
||
| 98 | * @return array<string, mixed>|Response |
||
| 99 | */ |
||
| 100 | private function getUserDataFromSession() |
||
| 101 | { |
||
| 102 | if ($this->userdata->hasRole('Disabled')) { |
||
| 103 | return Utility::showApiError(101); |
||
| 104 | } |
||
| 105 | |||
| 106 | return [ |
||
| 107 | 'uid' => $this->userdata->id, |
||
| 108 | 'userName' => $this->userdata->username, |
||
| 109 | 'maxDownloads' => $this->userdata->role->downloadrequests, |
||
| 110 | 'rssToken' => $this->userdata->api_token, |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get user data from RSS token |
||
| 116 | * |
||
| 117 | * @return array<string, mixed>|Response |
||
| 118 | */ |
||
| 119 | private function getUserDataFromRssToken(Request $request) |
||
| 120 | { |
||
| 121 | if ($request->missing('r')) { |
||
| 122 | return Utility::showApiError(200); |
||
| 123 | } |
||
| 124 | |||
| 125 | $user = User::getByRssToken($request->input('r')); |
||
| 126 | if (! $user) { |
||
| 127 | return Utility::showApiError(100); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($user->hasRole('Disabled')) { |
||
| 131 | return Utility::showApiError(101); |
||
| 132 | } |
||
| 133 | |||
| 134 | return [ |
||
| 135 | 'uid' => $user->id, |
||
| 136 | 'userName' => $user->username, |
||
| 137 | 'maxDownloads' => $user->role->downloadrequests, |
||
| 138 | 'rssToken' => $user->api_token, |
||
| 139 | ]; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if user has exceeded download limits |
||
| 144 | * |
||
| 145 | * @return Response|null |
||
| 146 | * |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | private function checkDownloadLimit(int $uid, int $maxDownloads): mixed |
||
| 150 | { |
||
| 151 | $requests = UserDownload::getDownloadRequests($uid); |
||
| 152 | if ($requests > $maxDownloads) { |
||
| 153 | return Utility::showApiError(501); |
||
| 154 | } |
||
| 155 | |||
| 156 | return null; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Validate and sanitize the release ID parameter |
||
| 161 | * |
||
| 162 | * @return string|Response |
||
| 163 | */ |
||
| 164 | private function validateAndSanitizeId(Request $request) |
||
| 165 | { |
||
| 166 | $id = $request->input('id'); |
||
| 167 | |||
| 168 | if (empty($id)) { |
||
| 169 | return Utility::showApiError(200, 'Parameter id is required'); |
||
| 170 | } |
||
| 171 | |||
| 172 | // Remove .nzb suffix if present |
||
| 173 | $sanitizedId = str_ireplace(self::NZB_SUFFIX, '', $id); |
||
| 174 | $request->merge(['id' => $sanitizedId]); |
||
| 175 | |||
| 385 |