| Conditions | 22 |
| Paths | 5968 |
| Total Lines | 82 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 246 | public function addAudioInfoAndSample( |
||
| 247 | Release $release, |
||
| 248 | string $fileLocation, |
||
| 249 | string $fileExtension, |
||
| 250 | bool $processAudioInfo, |
||
| 251 | bool $processAudioSample, |
||
| 252 | string $audioSavePath |
||
| 253 | ): array { |
||
| 254 | // Mirror original behavior: defaults depend on flags, not file presence |
||
| 255 | $retVal = ! $processAudioInfo ? true : false; |
||
| 256 | $audVal = ! $processAudioSample ? true : false; |
||
| 257 | |||
| 258 | // Only proceed with file-dependent operations if file exists |
||
| 259 | if (File::isFile($fileLocation)) { |
||
| 260 | if ($processAudioInfo) { |
||
| 261 | try { |
||
| 262 | $xmlArray = $this->mediaInfo->getInfo($fileLocation, false); |
||
| 263 | if ($xmlArray !== null) { |
||
| 264 | foreach ($xmlArray->getAudios() as $track) { |
||
| 265 | if ($track->get('album') !== null && $track->get('performer') !== null) { |
||
| 266 | if ((int) $release->predb_id === 0 && config('nntmux.rename_music_mediainfo')) { |
||
| 267 | $ext = strtoupper($fileExtension); |
||
| 268 | if (! empty($track->get('recorded_date')) && preg_match('/(?:19|20)\d\d/', $track->get('recorded_date')->getFullname(), $Year)) { |
||
| 269 | $newName = $track->get('performer')->getFullName().' - '.$track->get('album')->getFullName().' ('.$Year[0].') '.$ext; |
||
| 270 | } else { |
||
| 271 | $newName = $track->get('performer')->getFullName().' - '.$track->get('album')->getFullName().' '.$ext; |
||
| 272 | } |
||
| 273 | if ($ext === 'MP3') { |
||
| 274 | $newCat = Category::MUSIC_MP3; |
||
| 275 | } elseif ($ext === 'FLAC') { |
||
| 276 | $newCat = Category::MUSIC_LOSSLESS; |
||
| 277 | } else { |
||
| 278 | $newCat = $this->categorize->determineCategory($release->groups_id, $newName, $release->fromname); |
||
| 279 | } |
||
| 280 | $newTitle = escapeString(substr($newName, 0, 255)); |
||
| 281 | Release::whereId($release->id)->update([ |
||
| 282 | 'searchname' => $newTitle, |
||
| 283 | 'categories_id' => $newCat['categories_id'] ?? $release->categories_id, |
||
| 284 | 'iscategorized' => 1, |
||
| 285 | 'isrenamed' => 1, |
||
| 286 | 'proc_pp' => 1, |
||
| 287 | ]); |
||
| 288 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
| 289 | $this->elasticsearch->updateRelease($release->id); |
||
| 290 | } else { |
||
| 291 | $this->manticore->updateRelease($release->id); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | $this->releaseExtra->addFromXml($release->id, $xmlArray); |
||
| 295 | $retVal = true; |
||
| 296 | break; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } catch (\Throwable $e) { |
||
| 301 | Log::debug($e->getMessage()); |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | if ($processAudioSample) { |
||
| 306 | $audioFileName = ($release->guid.'.ogg'); |
||
| 307 | if ($this->ffprobe->isValid($fileLocation)) { |
||
| 308 | try { |
||
| 309 | $audioSample = $this->ffmpeg->open($fileLocation); |
||
| 310 | $format = new Vorbis; |
||
| 311 | $audioSample->clip(TimeCode::fromSeconds(30), TimeCode::fromSeconds(30)); |
||
| 312 | $audioSample->save($format, $audioSavePath.$audioFileName); |
||
| 313 | } catch (\Throwable $e) { |
||
| 314 | if (config('app.debug') === true) { |
||
| 315 | Log::error($e->getMessage()); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | if (File::isFile($audioSavePath.$audioFileName)) { |
||
| 320 | @chmod($audioSavePath.$audioFileName, 0764); |
||
| 321 | Release::query()->where('id', $release->id)->update(['audiostatus' => 1]); |
||
| 322 | $audVal = true; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | return ['info' => $retVal, 'sample' => $audVal]; |
||
| 328 | } |
||
| 330 |