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