Conditions | 25 |
Paths | > 20000 |
Total Lines | 139 |
Code Lines | 82 |
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 |
||
243 | public function processNfoFiles($nntp, string $groupID = '', string $guidChar = '', int $processImdb = 1, int $processTv = 1): int |
||
244 | { |
||
245 | $ret = 0; |
||
246 | |||
247 | $qry = Release::query() |
||
248 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
249 | ->whereBetween('nfostatus', [$this->maxRetries, self::NFO_UNPROC]); |
||
250 | |||
251 | if ($guidChar !== '') { |
||
252 | $qry->where('leftguid', $guidChar); |
||
253 | } |
||
254 | if ($groupID !== '') { |
||
255 | $qry->where('groups_id', $groupID); |
||
256 | } |
||
257 | |||
258 | if ($this->maxSize > 0) { |
||
259 | $qry->where('size', '<', $this->maxSize * 1073741824); |
||
260 | } |
||
261 | |||
262 | if ($this->minSize > 0) { |
||
263 | $qry->where('size', '>', $this->minSize * 1048576); |
||
264 | } |
||
265 | |||
266 | $res = $qry |
||
267 | ->orderBy('nfostatus') |
||
268 | ->orderByDesc('postdate') |
||
269 | ->limit($this->nzbs) |
||
270 | ->get(['id', 'guid', 'groups_id', 'name']); |
||
271 | |||
272 | $nfoCount = $res->count(); |
||
273 | |||
274 | if ($nfoCount > 0) { |
||
275 | $this->colorCli->primary( |
||
276 | PHP_EOL. |
||
277 | ($guidChar === '' ? '' : '['.$guidChar.'] '). |
||
278 | ($groupID === '' ? '' : '['.$groupID.'] '). |
||
279 | 'Processing '.$nfoCount. |
||
280 | ' NFO(s), starting at '.$this->nzbs. |
||
281 | ' * = hidden NFO, + = NFO, - = no NFO, f = download failed.' |
||
282 | ); |
||
283 | |||
284 | if ($this->echo) { |
||
285 | // Get count of releases per nfo status |
||
286 | $qry = Release::query() |
||
287 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
288 | ->whereBetween('nfostatus', [$this->maxRetries, self::NFO_UNPROC]) |
||
289 | ->select(['nfostatus as status', DB::raw('COUNT(id) as count')]) |
||
290 | ->groupBy(['nfostatus']) |
||
291 | ->orderBy('nfostatus'); |
||
292 | |||
293 | if ($guidChar !== '') { |
||
294 | $qry->where('leftguid', $guidChar); |
||
295 | } |
||
296 | if ($groupID !== '') { |
||
297 | $qry->where('groups_id', $groupID); |
||
298 | } |
||
299 | |||
300 | if ($this->maxSize > 0) { |
||
301 | $qry->where('size', '<', $this->maxSize * 1073741824); |
||
302 | } |
||
303 | |||
304 | if ($this->minSize > 0) { |
||
305 | $qry->where('size', '>', $this->minSize * 1048576); |
||
306 | } |
||
307 | |||
308 | $nfoStats = $qry->get(); |
||
309 | |||
310 | if ($nfoStats instanceof \Traversable) { |
||
311 | $outString = PHP_EOL.'Available to process'; |
||
312 | foreach ($nfoStats as $row) { |
||
313 | $outString .= ', '.$row['status'].' = '.number_format($row['count']); |
||
314 | } |
||
315 | $this->colorCli->header($outString.'.'); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | $nzbContents = new NZBContents( |
||
320 | [ |
||
321 | 'Echo' => $this->echo, |
||
322 | 'NNTP' => $nntp, |
||
323 | 'Nfo' => $this, |
||
324 | 'Settings' => null, |
||
325 | 'PostProcess' => new PostProcess(['Echo' => $this->echo, 'Nfo' => $this]), |
||
326 | ] |
||
327 | ); |
||
328 | $movie = new Movie(['Echo' => $this->echo]); |
||
329 | |||
330 | foreach ($res as $arr) { |
||
331 | $fetchedBinary = $nzbContents->getNfoFromNZB($arr['guid'], $arr['id'], $arr['groups_id'], UsenetGroup::getNameByID($arr['groups_id'])); |
||
332 | if ($fetchedBinary !== false) { |
||
333 | // Insert nfo into database. |
||
334 | |||
335 | $ckReleaseId = ReleaseNfo::whereReleasesId($arr['id'])->first(['releases_id']); |
||
336 | if ($ckReleaseId === null) { |
||
337 | ReleaseNfo::query()->insert(['releases_id' => $arr['id'], 'nfo' => "\x1f\x8b\x08\x00".gzcompress($fetchedBinary)]); |
||
338 | } |
||
339 | Release::whereId($arr['id'])->update(['nfostatus' => self::NFO_FOUND]); |
||
340 | $ret++; |
||
341 | $movie->doMovieUpdate($fetchedBinary, 'nfo', $arr['id'], $processImdb); |
||
342 | |||
343 | // If set scan for tv info. |
||
344 | if ($processTv === 1) { |
||
345 | (new PostProcess(['Echo' => $this->echo]))->processTv($groupID, $guidChar, $processTv); |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | |||
351 | // Remove nfo that we cant fetch after 5 attempts. |
||
352 | $qry = Release::query() |
||
353 | ->where('nzbstatus', NZB::NZB_ADDED) |
||
354 | ->where('nfostatus', '<', $this->maxRetries) |
||
355 | ->where('nfostatus', '>', self::NFO_FAILED); |
||
356 | |||
357 | if ($guidChar !== '') { |
||
358 | $qry->where('leftguid', $guidChar); |
||
359 | } |
||
360 | if ($groupID !== '') { |
||
361 | $qry->where('groups_id', $groupID); |
||
362 | } |
||
363 | |||
364 | foreach ($qry->get(['id']) as $release) { |
||
365 | // remove any releasenfo for failed |
||
366 | ReleaseNfo::whereReleasesId($release['id'])->delete(); |
||
367 | |||
368 | // set release.nfostatus to failed |
||
369 | Release::whereId($release['id'])->update(['nfostatus' => self::NFO_FAILED]); |
||
370 | } |
||
371 | |||
372 | if ($this->echo) { |
||
373 | if ($nfoCount > 0) { |
||
374 | echo PHP_EOL; |
||
375 | } |
||
376 | if ($ret > 0) { |
||
377 | $this->colorCli->primary($ret.' NFO file(s) found/processed.'); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | return $ret; |
||
382 | } |
||
411 |