Total Complexity | 44 |
Total Lines | 405 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Release often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Release, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
149 | class Release extends Model |
||
150 | { |
||
151 | use HasFactory; |
||
152 | |||
153 | /** |
||
154 | * @var bool |
||
155 | */ |
||
156 | protected $dateFormat = false; |
||
157 | |||
158 | /** |
||
159 | * @var bool |
||
160 | */ |
||
161 | public $timestamps = false; |
||
162 | |||
163 | /** |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $guarded = []; |
||
167 | |||
168 | public function group(): BelongsTo |
||
169 | { |
||
170 | return $this->belongsTo(UsenetGroup::class, 'groups_id'); |
||
171 | } |
||
172 | |||
173 | public function download(): HasMany |
||
176 | } |
||
177 | |||
178 | public function userRelease(): HasMany |
||
179 | { |
||
180 | return $this->hasMany(UsersRelease::class, 'releases_id'); |
||
181 | } |
||
182 | |||
183 | public function file(): HasMany |
||
184 | { |
||
185 | return $this->hasMany(ReleaseFile::class, 'releases_id'); |
||
186 | } |
||
187 | |||
188 | public function category(): BelongsTo |
||
189 | { |
||
190 | return $this->belongsTo(Category::class, 'categories_id'); |
||
191 | } |
||
192 | |||
193 | public function predb(): BelongsTo |
||
194 | { |
||
195 | return $this->belongsTo(Predb::class, 'predb_id'); |
||
196 | } |
||
197 | |||
198 | public function failed(): HasMany |
||
201 | } |
||
202 | |||
203 | public function nfo(): HasOne |
||
204 | { |
||
205 | return $this->hasOne(ReleaseNfo::class, 'releases_id'); |
||
206 | } |
||
207 | |||
208 | public function comment(): HasMany |
||
209 | { |
||
210 | return $this->hasMany(ReleaseComment::class, 'releases_id'); |
||
211 | } |
||
212 | |||
213 | public function releaseGroup(): HasMany |
||
216 | } |
||
217 | |||
218 | public function video(): BelongsTo |
||
219 | { |
||
220 | return $this->belongsTo(Video::class, 'videos_id'); |
||
221 | } |
||
222 | |||
223 | public function episode(): BelongsTo |
||
224 | { |
||
225 | return $this->belongsTo(TvEpisode::class, 'tv_episodes_id'); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Insert a single release returning the ID on success or false on failure. |
||
230 | * |
||
231 | * @param array $parameters Insert parameters, must be escaped if string. |
||
232 | * @return bool|int |
||
233 | * |
||
234 | * @throws \Exception |
||
235 | */ |
||
236 | public static function insertRelease(array $parameters = []) |
||
237 | { |
||
238 | $passwordStatus = ((int) Settings::settingValue('..checkpasswordedrar') === 1 ? -1 : 0); |
||
239 | $parameters['id'] = self::query() |
||
240 | ->insertGetId( |
||
241 | [ |
||
242 | 'name' => $parameters['name'], |
||
243 | 'searchname' => $parameters['searchname'], |
||
244 | 'totalpart' => $parameters['totalpart'], |
||
245 | 'groups_id' => $parameters['groups_id'], |
||
246 | 'adddate' => now(), |
||
247 | 'guid' => $parameters['guid'], |
||
248 | 'leftguid' => $parameters['guid'][0], |
||
249 | 'postdate' => $parameters['postdate'], |
||
250 | 'fromname' => $parameters['fromname'], |
||
251 | 'size' => $parameters['size'], |
||
252 | 'passwordstatus' => $passwordStatus, |
||
253 | 'haspreview' => -1, |
||
254 | 'categories_id' => $parameters['categories_id'], |
||
255 | 'nfostatus' => -1, |
||
256 | 'nzbstatus' => $parameters['nzbstatus'], |
||
257 | 'isrenamed' => $parameters['isrenamed'], |
||
258 | 'iscategorized' => 1, |
||
259 | 'predb_id' => $parameters['predb_id'], |
||
260 | ] |
||
261 | ); |
||
262 | |||
263 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
264 | (new ElasticSearchSiteSearch())->insertRelease($parameters); |
||
265 | } else { |
||
266 | (new ManticoreSearch())->insertRelease($parameters); |
||
267 | } |
||
268 | |||
269 | return $parameters['id']; |
||
|
|||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @throws \Exception |
||
274 | */ |
||
275 | public static function updateRelease($id, $name, $searchName, $fromName, $categoryId, $parts, $grabs, $size, $postedDate, $addedDate, $videoId, $episodeId, $imDbId, $aniDbId): void |
||
276 | { |
||
277 | $movieInfoId = null; |
||
278 | if (! empty($imDbId)) { |
||
279 | $movieInfoId = MovieInfo::whereImdbid($imDbId)->first(['id']); |
||
280 | } |
||
281 | self::whereId($id)->update( |
||
282 | [ |
||
283 | 'name' => $name, |
||
284 | 'searchname' => $searchName, |
||
285 | 'fromname' => $fromName, |
||
286 | 'categories_id' => $categoryId, |
||
287 | 'totalpart' => $parts, |
||
288 | 'grabs' => $grabs, |
||
289 | 'size' => $size, |
||
290 | 'postdate' => $postedDate, |
||
291 | 'adddate' => $addedDate, |
||
292 | 'videos_id' => $videoId, |
||
293 | 'tv_episodes_id' => $episodeId, |
||
294 | 'imdbid' => $imDbId, |
||
295 | 'anidbid' => $aniDbId, |
||
296 | 'movieinfo_id' => $movieInfoId !== null ? $movieInfoId->id : $movieInfoId, |
||
297 | ] |
||
298 | ); |
||
299 | |||
300 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
301 | (new ElasticSearchSiteSearch())->updateRelease($id); |
||
302 | } else { |
||
303 | (new ManticoreSearch())->updateRelease($id); |
||
304 | } |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @throws \Exception |
||
309 | */ |
||
310 | public static function updateGrab(string $guid): void |
||
311 | { |
||
312 | $updateGrabs = ((int) Settings::settingValue('..grabstatus') !== 0); |
||
313 | if ($updateGrabs) { |
||
314 | self::whereGuid($guid)->increment('grabs'); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return Model|null|static |
||
320 | */ |
||
321 | public static function getCatByRelId($id) |
||
324 | } |
||
325 | |||
326 | public static function removeVideoIdFromReleases($videoId): int |
||
327 | { |
||
328 | return self::whereVideosId($videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]); |
||
329 | } |
||
330 | |||
331 | public static function removeAnidbIdFromReleases($anidbID): int |
||
332 | { |
||
333 | return self::whereAnidbid($anidbID)->update(['anidbid' => -1]); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
||
338 | */ |
||
339 | public static function getTopDownloads() |
||
340 | { |
||
341 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
342 | $releases = Cache::get(md5('topDownloads')); |
||
343 | if ($releases !== null) { |
||
344 | return $releases; |
||
345 | } |
||
346 | $releases = self::query() |
||
347 | ->where('grabs', '>', 0) |
||
348 | ->select(['id', 'searchname', 'guid', 'adddate']) |
||
349 | ->selectRaw('SUM(grabs) as grabs') |
||
350 | ->groupBy('id', 'searchname', 'adddate') |
||
351 | ->havingRaw('SUM(grabs) > 0') |
||
352 | ->orderByDesc('grabs') |
||
353 | ->limit(10) |
||
354 | ->get(); |
||
355 | |||
356 | Cache::put(md5('topDownloads'), $releases, $expiresAt); |
||
357 | |||
358 | return $releases; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
||
363 | */ |
||
364 | public static function getTopComments() |
||
365 | { |
||
366 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
367 | $releases = Cache::get(md5('topComments')); |
||
368 | if ($releases !== null) { |
||
369 | return $releases; |
||
370 | } |
||
371 | $releases = self::query() |
||
372 | ->where('comments', '>', 0) |
||
373 | ->select(['id', 'guid', 'searchname']) |
||
374 | ->selectRaw('SUM(comments) AS comments') |
||
375 | ->groupBy('id', 'searchname', 'adddate') |
||
376 | ->havingRaw('SUM(comments) > 0') |
||
377 | ->orderByDesc('comments') |
||
378 | ->limit(10) |
||
379 | ->get(); |
||
380 | |||
381 | Cache::put(md5('topComments'), $releases, $expiresAt); |
||
382 | |||
383 | return $releases; |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed |
||
388 | */ |
||
389 | public static function getReleases() |
||
390 | { |
||
391 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
392 | $releases = Cache::get(md5('releases')); |
||
393 | if ($releases !== null) { |
||
394 | return $releases; |
||
395 | } |
||
396 | |||
397 | $releases = self::query() |
||
398 | |||
399 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
400 | ->select(['releases.*', 'g.name as group_name', 'c.title as category_name']) |
||
401 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
402 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id') |
||
403 | ->get(); |
||
404 | |||
405 | Cache::put(md5('releases'), $releases, $expiresAt); |
||
406 | |||
407 | return $releases; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Used for admin page release-list. |
||
412 | * |
||
413 | * |
||
414 | * @return LengthAwarePaginator|mixed |
||
415 | */ |
||
416 | public static function getReleasesRange() |
||
417 | { |
||
418 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
419 | $releases = Cache::get(md5('releasesRange')); |
||
420 | if ($releases !== null) { |
||
421 | return $releases; |
||
422 | } |
||
423 | |||
424 | $releases = self::query() |
||
425 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
426 | ->select( |
||
427 | [ |
||
428 | 'releases.id', |
||
429 | 'releases.name', |
||
430 | 'releases.searchname', |
||
431 | 'releases.size', |
||
432 | 'releases.guid', |
||
433 | 'releases.totalpart', |
||
434 | 'releases.postdate', |
||
435 | 'releases.adddate', |
||
436 | 'releases.grabs', |
||
437 | 'cp.title as parent_category', |
||
438 | 'c.title as sub_category', |
||
439 | DB::raw('CONCAT(cp.title, ' > ', c.title) AS category_name'), |
||
440 | ] |
||
441 | ) |
||
442 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
443 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
444 | ->orderByDesc('releases.postdate') |
||
445 | ->paginate(config('nntmux.items_per_page')); |
||
446 | |||
447 | Cache::put(md5('releasesRange'), $releases, $expiresAt); |
||
448 | |||
449 | return $releases; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null|static|static[] |
||
454 | */ |
||
455 | public static function getByGuid($guid) |
||
456 | { |
||
457 | $sql = self::query()->select( |
||
458 | [ |
||
459 | 'releases.*', |
||
460 | 'g.name as group_name', |
||
461 | 'v.title as showtitle', |
||
462 | 'v.tvdb', |
||
463 | 'v.trakt', |
||
464 | 'v.tvrage', |
||
465 | 'v.tvmaze', |
||
466 | 'v.source', |
||
467 | 'tvi.summary', |
||
468 | 'tvi.image', |
||
469 | 'tve.title', |
||
470 | 'tve.firstaired', |
||
471 | 'tve.se_complete', |
||
472 | 'cp.title as parent_category', |
||
473 | 'c.title as sub_category', |
||
474 | DB::raw("CONCAT(cp.title, ' > ', c.title) AS category_name, CONCAT(cp.id, ',', c.id) AS category_ids,GROUP_CONCAT(g2.name ORDER BY g2.name ASC SEPARATOR ',') AS group_names"), |
||
475 | ] |
||
476 | ) |
||
477 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id') |
||
478 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
479 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
480 | ->leftJoin('videos as v', 'v.id', '=', 'releases.videos_id') |
||
481 | ->leftJoin('tv_info as tvi', 'tvi.videos_id', '=', 'releases.videos_id') |
||
482 | ->leftJoin('tv_episodes as tve', 'tve.id', '=', 'releases.tv_episodes_id') |
||
483 | ->leftJoin('releases_groups as rg', 'rg.releases_id', '=', 'releases.id') |
||
484 | ->leftJoin('usenet_groups as g2', 'rg.groups_id', '=', 'g2.id'); |
||
485 | |||
486 | if (\is_array($guid)) { |
||
487 | $tempGuids = []; |
||
488 | foreach ($guid as $identifier) { |
||
489 | $tempGuids[] = $identifier; |
||
490 | } |
||
491 | $sql->whereIn('releases.guid', $tempGuids); |
||
492 | } else { |
||
493 | $sql->where('releases.guid', $guid); |
||
494 | } |
||
495 | $sql->groupBy('releases.id'); |
||
496 | |||
497 | return \is_array($guid) ? $sql->get() : $sql->first(); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Get a range of releases. used in admin manage list. |
||
502 | */ |
||
503 | public static function getFailedRange(): LengthAwarePaginator |
||
504 | { |
||
505 | $failedList = self::query() |
||
506 | ->select(['name', 'searchname', 'size', 'guid', 'totalpart', 'postdate', 'adddate', 'grabs', 'cp.title as parent_category', 'c.title as sub_category', DB::raw("CONCAT(cp.title, ' > ', c.title) AS category_name")]) |
||
507 | ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id') |
||
508 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
509 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
510 | ->orderByDesc('postdate'); |
||
511 | |||
512 | return $failedList->paginate(config('nntmux.items_per_page')); |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @return Release|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null |
||
517 | */ |
||
518 | public static function getAlternate(string $guid, int $userid) |
||
519 | { |
||
520 | $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']); |
||
521 | |||
522 | if ($rel === null) { |
||
523 | return false; |
||
524 | } |
||
525 | DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]); |
||
526 | |||
527 | preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', $rel['searchname'], $similar); |
||
528 | |||
529 | if (! empty($similar)) { |
||
530 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
531 | $searchResult = (new ElasticSearchSiteSearch())->indexSearch($similar[1], 10); |
||
532 | } else { |
||
533 | $searchResult = (new ManticoreSearch())->searchIndexes('releases_rt', $similar[1]); |
||
534 | if (! empty($searchResult)) { |
||
535 | $searchResult = Arr::wrap(Arr::get($searchResult, 'id')); |
||
536 | } |
||
537 | } |
||
538 | |||
539 | if (empty($searchResult)) { |
||
540 | return false; |
||
541 | } |
||
542 | |||
543 | return self::query()->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'releases.id')->whereIn('releases.id', $searchResult)->where('df.release_id', '=', null)->where('releases.categories_id', $rel['categories_id'])->where('id', '<>', $rel['id'])->orderByDesc('releases.postdate')->first(['guid']); |
||
544 | } |
||
545 | |||
546 | return false; |
||
547 | } |
||
548 | |||
549 | public static function checkGuidForApi($guid): bool |
||
554 | } |
||
555 | } |
||
556 |