| Total Complexity | 40 |
| Total Lines | 355 |
| 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 |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed |
||
| 338 | */ |
||
| 339 | public static function getReleases() |
||
| 340 | { |
||
| 341 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
| 342 | $releases = Cache::get(md5('releases')); |
||
| 343 | if ($releases !== null) { |
||
| 344 | return $releases; |
||
| 345 | } |
||
| 346 | |||
| 347 | $releases = self::query() |
||
| 348 | |||
| 349 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
| 350 | ->select(['releases.*', 'g.name as group_name', 'c.title as category_name']) |
||
| 351 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
| 352 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id') |
||
| 353 | ->get(); |
||
| 354 | |||
| 355 | Cache::put(md5('releases'), $releases, $expiresAt); |
||
| 356 | |||
| 357 | return $releases; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Used for admin page release-list. |
||
| 362 | * |
||
| 363 | * |
||
| 364 | * @return LengthAwarePaginator|mixed |
||
| 365 | */ |
||
| 366 | public static function getReleasesRange() |
||
| 367 | { |
||
| 368 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
| 369 | $releases = Cache::get(md5('releasesRange')); |
||
| 370 | if ($releases !== null) { |
||
| 371 | return $releases; |
||
| 372 | } |
||
| 373 | |||
| 374 | $releases = self::query() |
||
| 375 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
| 376 | ->select( |
||
| 377 | [ |
||
| 378 | 'releases.id', |
||
| 379 | 'releases.name', |
||
| 380 | 'releases.searchname', |
||
| 381 | 'releases.size', |
||
| 382 | 'releases.guid', |
||
| 383 | 'releases.totalpart', |
||
| 384 | 'releases.postdate', |
||
| 385 | 'releases.adddate', |
||
| 386 | 'releases.grabs', |
||
| 387 | 'cp.title as parent_category', |
||
| 388 | 'c.title as sub_category', |
||
| 389 | DB::raw('CONCAT(cp.title, ' > ', c.title) AS category_name'), |
||
| 390 | ] |
||
| 391 | ) |
||
| 392 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
| 393 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
| 394 | ->orderByDesc('releases.postdate') |
||
| 395 | ->paginate(config('nntmux.items_per_page')); |
||
| 396 | |||
| 397 | Cache::put(md5('releasesRange'), $releases, $expiresAt); |
||
| 398 | |||
| 399 | return $releases; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null|static|static[] |
||
| 404 | */ |
||
| 405 | public static function getByGuid($guid) |
||
| 406 | { |
||
| 407 | $sql = self::query()->select( |
||
| 408 | [ |
||
| 409 | 'releases.*', |
||
| 410 | 'g.name as group_name', |
||
| 411 | 'v.title as showtitle', |
||
| 412 | 'v.tvdb', |
||
| 413 | 'v.trakt', |
||
| 414 | 'v.tvrage', |
||
| 415 | 'v.tvmaze', |
||
| 416 | 'v.source', |
||
| 417 | 'tvi.summary', |
||
| 418 | 'tvi.image', |
||
| 419 | 'tve.title', |
||
| 420 | 'tve.firstaired', |
||
| 421 | 'tve.se_complete', |
||
| 422 | 'cp.title as parent_category', |
||
| 423 | 'c.title as sub_category', |
||
| 424 | 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"), |
||
| 425 | ] |
||
| 426 | ) |
||
| 427 | ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id') |
||
| 428 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
| 429 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
| 430 | ->leftJoin('videos as v', 'v.id', '=', 'releases.videos_id') |
||
| 431 | ->leftJoin('tv_info as tvi', 'tvi.videos_id', '=', 'releases.videos_id') |
||
| 432 | ->leftJoin('tv_episodes as tve', 'tve.id', '=', 'releases.tv_episodes_id') |
||
| 433 | ->leftJoin('releases_groups as rg', 'rg.releases_id', '=', 'releases.id') |
||
| 434 | ->leftJoin('usenet_groups as g2', 'rg.groups_id', '=', 'g2.id'); |
||
| 435 | |||
| 436 | if (\is_array($guid)) { |
||
| 437 | $tempGuids = []; |
||
| 438 | foreach ($guid as $identifier) { |
||
| 439 | $tempGuids[] = $identifier; |
||
| 440 | } |
||
| 441 | $sql->whereIn('releases.guid', $tempGuids); |
||
| 442 | } else { |
||
| 443 | $sql->where('releases.guid', $guid); |
||
| 444 | } |
||
| 445 | $sql->groupBy('releases.id'); |
||
| 446 | |||
| 447 | return \is_array($guid) ? $sql->get() : $sql->first(); |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get a range of releases. used in admin manage list. |
||
| 452 | */ |
||
| 453 | public static function getFailedRange(): LengthAwarePaginator |
||
| 454 | { |
||
| 455 | $failedList = self::query() |
||
| 456 | ->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")]) |
||
| 457 | ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id') |
||
| 458 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
| 459 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
| 460 | ->orderByDesc('postdate'); |
||
| 461 | |||
| 462 | return $failedList->paginate(config('nntmux.items_per_page')); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @return Release|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null |
||
| 467 | */ |
||
| 468 | public static function getAlternate(string $guid, int $userid) |
||
| 469 | { |
||
| 470 | $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']); |
||
| 471 | |||
| 472 | if ($rel === null) { |
||
| 473 | return false; |
||
| 474 | } |
||
| 475 | DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]); |
||
| 476 | |||
| 477 | preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', $rel['searchname'], $similar); |
||
| 478 | |||
| 479 | if (! empty($similar)) { |
||
| 480 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
| 481 | $searchResult = (new ElasticSearchSiteSearch)->indexSearch($similar[1], 10); |
||
| 482 | } else { |
||
| 483 | $searchResult = (new ManticoreSearch)->searchIndexes('releases_rt', $similar[1]); |
||
| 484 | if (! empty($searchResult)) { |
||
| 485 | $searchResult = Arr::wrap(Arr::get($searchResult, 'id')); |
||
| 486 | } |
||
| 487 | } |
||
| 488 | |||
| 489 | if (empty($searchResult)) { |
||
| 490 | return false; |
||
| 491 | } |
||
| 492 | |||
| 493 | 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']); |
||
| 494 | } |
||
| 495 | |||
| 496 | return false; |
||
| 497 | } |
||
| 498 | |||
| 499 | public static function checkGuidForApi($guid): bool |
||
| 504 | } |
||
| 505 | } |
||
| 506 |