Total Complexity | 45 |
Total Lines | 413 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
18 | class Release extends Model |
||
19 | { |
||
20 | use HasFactory; |
||
|
|||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | protected $dateFormat = false; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | public $timestamps = false; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $guarded = []; |
||
36 | |||
37 | public function group(): BelongsTo |
||
38 | { |
||
39 | return $this->belongsTo(UsenetGroup::class, 'groups_id'); |
||
40 | } |
||
41 | |||
42 | public function download(): HasMany |
||
43 | { |
||
44 | return $this->hasMany(UserDownload::class, 'releases_id'); |
||
45 | } |
||
46 | |||
47 | public function userRelease(): HasMany |
||
48 | { |
||
49 | return $this->hasMany(UsersRelease::class, 'releases_id'); |
||
50 | } |
||
51 | |||
52 | public function file(): HasMany |
||
53 | { |
||
54 | return $this->hasMany(ReleaseFile::class, 'releases_id'); |
||
55 | } |
||
56 | |||
57 | public function category(): BelongsTo |
||
58 | { |
||
59 | return $this->belongsTo(Category::class, 'categories_id'); |
||
60 | } |
||
61 | |||
62 | public function predb(): BelongsTo |
||
63 | { |
||
64 | return $this->belongsTo(Predb::class, 'predb_id'); |
||
65 | } |
||
66 | |||
67 | public function failed(): HasMany |
||
68 | { |
||
69 | return $this->hasMany(DnzbFailure::class, 'release_id'); |
||
70 | } |
||
71 | |||
72 | public function nfo(): HasOne |
||
73 | { |
||
74 | return $this->hasOne(ReleaseNfo::class, 'releases_id'); |
||
75 | } |
||
76 | |||
77 | public function comment(): HasMany |
||
78 | { |
||
79 | return $this->hasMany(ReleaseComment::class, 'releases_id'); |
||
80 | } |
||
81 | |||
82 | public function releaseGroup(): HasMany |
||
83 | { |
||
84 | return $this->hasMany(ReleasesGroups::class, 'releases_id'); |
||
85 | } |
||
86 | |||
87 | public function video(): BelongsTo |
||
88 | { |
||
89 | return $this->belongsTo(Video::class, 'videos_id'); |
||
90 | } |
||
91 | |||
92 | public function videoData(): HasOne |
||
93 | { |
||
94 | return $this->hasOne(VideoData::class, 'releases_id'); |
||
95 | } |
||
96 | |||
97 | public function episode(): BelongsTo |
||
98 | { |
||
99 | return $this->belongsTo(TvEpisode::class, 'tv_episodes_id'); |
||
100 | } |
||
101 | |||
102 | public function movieInfo(): BelongsTo |
||
103 | { |
||
104 | return $this->belongsTo(MovieInfo::class, 'movieinfo_id'); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Insert a single release returning the ID on success or false on failure. |
||
109 | * |
||
110 | * @param array $parameters Insert parameters, must be escaped if string. |
||
111 | * @return bool|int |
||
112 | * |
||
113 | * @throws \Exception |
||
114 | */ |
||
115 | public static function insertRelease(array $parameters = []) |
||
116 | { |
||
117 | $passwordStatus = config('nntmux_settings.check_passworded_rars') === true ? -1 : 0; |
||
118 | $parameters['id'] = self::query() |
||
119 | ->insertGetId( |
||
120 | [ |
||
121 | 'name' => $parameters['name'], |
||
122 | 'searchname' => $parameters['searchname'], |
||
123 | 'totalpart' => $parameters['totalpart'], |
||
124 | 'groups_id' => $parameters['groups_id'], |
||
125 | 'adddate' => now(), |
||
126 | 'guid' => $parameters['guid'], |
||
127 | 'leftguid' => $parameters['guid'][0], |
||
128 | 'postdate' => $parameters['postdate'], |
||
129 | 'fromname' => $parameters['fromname'], |
||
130 | 'size' => $parameters['size'], |
||
131 | 'passwordstatus' => $passwordStatus, |
||
132 | 'haspreview' => -1, |
||
133 | 'categories_id' => $parameters['categories_id'], |
||
134 | 'nfostatus' => -1, |
||
135 | 'nzbstatus' => $parameters['nzbstatus'], |
||
136 | 'isrenamed' => $parameters['isrenamed'], |
||
137 | 'iscategorized' => 1, |
||
138 | 'predb_id' => $parameters['predb_id'], |
||
139 | 'ishashed' => $parameters['ishashed'] ?? 0, |
||
140 | ] |
||
141 | ); |
||
142 | |||
143 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
144 | (new ElasticSearchSiteSearch)->insertRelease($parameters); |
||
145 | } else { |
||
146 | (new ManticoreSearch)->insertRelease($parameters); |
||
147 | } |
||
148 | |||
149 | return $parameters['id']; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @throws \Exception |
||
154 | */ |
||
155 | public static function updateRelease($id, $name, $searchName, $fromName, $categoryId, $parts, $grabs, $size, $postedDate, $addedDate, $videoId, $episodeId, $imDbId, $aniDbId): void |
||
156 | { |
||
157 | $movieInfoId = null; |
||
158 | if (! empty($imDbId)) { |
||
159 | $movieInfoId = MovieInfo::whereImdbid($imDbId)->first(['id']); |
||
160 | } |
||
161 | self::whereId($id)->update( |
||
162 | [ |
||
163 | 'name' => $name, |
||
164 | 'searchname' => $searchName, |
||
165 | 'fromname' => $fromName, |
||
166 | 'categories_id' => $categoryId, |
||
167 | 'totalpart' => $parts, |
||
168 | 'grabs' => $grabs, |
||
169 | 'size' => $size, |
||
170 | 'postdate' => $postedDate, |
||
171 | 'adddate' => $addedDate, |
||
172 | 'videos_id' => $videoId, |
||
173 | 'tv_episodes_id' => $episodeId, |
||
174 | 'imdbid' => $imDbId, |
||
175 | 'anidbid' => $aniDbId, |
||
176 | 'movieinfo_id' => $movieInfoId !== null ? $movieInfoId->id : $movieInfoId, |
||
177 | ] |
||
178 | ); |
||
179 | |||
180 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
181 | (new ElasticSearchSiteSearch)->updateRelease($id); |
||
182 | } else { |
||
183 | (new ManticoreSearch)->updateRelease($id); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @throws \Exception |
||
189 | */ |
||
190 | public static function updateGrab(string $guid): void |
||
191 | { |
||
192 | $updateGrabs = ((int) Settings::settingValue('grabstatus') !== 0); |
||
193 | if ($updateGrabs) { |
||
194 | self::whereGuid($guid)->increment('grabs'); |
||
195 | } |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return Model|null|static |
||
200 | */ |
||
201 | public static function getCatByRelId($id) |
||
204 | } |
||
205 | |||
206 | public static function removeVideoIdFromReleases($videoId): int |
||
207 | { |
||
208 | return self::whereVideosId($videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]); |
||
209 | } |
||
210 | |||
211 | public static function removeAnidbIdFromReleases($anidbID): int |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
||
218 | */ |
||
219 | public static function getTopDownloads() |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
||
243 | */ |
||
244 | public static function getTopComments() |
||
245 | { |
||
246 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
247 | $releases = Cache::get(md5('topComments')); |
||
248 | if ($releases !== null) { |
||
249 | return $releases; |
||
250 | } |
||
251 | $releases = self::query() |
||
252 | ->where('comments', '>', 0) |
||
253 | ->select(['id', 'guid', 'searchname']) |
||
254 | ->selectRaw('SUM(comments) AS comments') |
||
255 | ->groupBy('id', 'searchname', 'adddate') |
||
256 | ->havingRaw('SUM(comments) > 0') |
||
257 | ->orderByDesc('comments') |
||
258 | ->limit(10) |
||
259 | ->get(); |
||
260 | |||
261 | Cache::put(md5('topComments'), $releases, $expiresAt); |
||
262 | |||
263 | return $releases; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed |
||
268 | */ |
||
269 | public static function getReleases() |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Used for admin page release-list. |
||
292 | * |
||
293 | * @param int $page The page number to retrieve |
||
294 | * @return LengthAwarePaginator|mixed |
||
295 | */ |
||
296 | public static function getReleasesRange(int $page = 1): mixed |
||
297 | { |
||
298 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
299 | $cacheKey = md5('releasesRange_'.$page); |
||
300 | $releases = Cache::get($cacheKey); |
||
301 | if ($releases !== null) { |
||
302 | return $releases; |
||
303 | } |
||
304 | |||
305 | $releases = self::query() |
||
306 | ->where('nzbstatus', '=', NZB::NZB_ADDED) |
||
307 | ->select( |
||
308 | [ |
||
309 | 'releases.id', |
||
310 | 'releases.name', |
||
311 | 'releases.searchname', |
||
312 | 'releases.size', |
||
313 | 'releases.guid', |
||
314 | 'releases.totalpart', |
||
315 | 'releases.postdate', |
||
316 | 'releases.adddate', |
||
317 | 'releases.grabs', |
||
318 | 'cp.title as parent_category', |
||
319 | 'c.title as sub_category', |
||
320 | DB::raw('CONCAT(cp.title, \' > \', c.title) AS category_name'), |
||
321 | ] |
||
322 | ) |
||
323 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
324 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
325 | ->orderByDesc('releases.postdate') |
||
326 | ->paginate(config('nntmux.items_per_page'), ['*'], 'page', $page); |
||
327 | |||
328 | Cache::put($cacheKey, $releases, $expiresAt); |
||
329 | |||
330 | return $releases; |
||
331 | } |
||
332 | |||
333 | public static function getByGuid($guid) |
||
334 | { |
||
335 | $query = self::with([ |
||
336 | 'group:id,name', |
||
337 | 'category:id,title,root_categories_id', |
||
338 | 'category.parent:id,title', |
||
339 | 'video:id,title,tvdb,trakt,tvrage,tvmaze,source', |
||
340 | 'video.tvInfo:videos_id,summary,image', |
||
341 | 'episode:id,title,firstaired,se_complete', |
||
342 | ]); |
||
343 | |||
344 | if (is_array($guid)) { |
||
345 | $query->whereIn('guid', $guid); |
||
346 | } else { |
||
347 | $query->where('guid', $guid); |
||
348 | } |
||
349 | |||
350 | $releases = $query->get(); |
||
351 | |||
352 | $releases->each(function ($release) { |
||
353 | $release->group_name = $release->group->name ?? null; |
||
354 | $release->showtitle = $release->video->title ?? null; |
||
355 | $release->tvdb = $release->video->tvdb ?? null; |
||
356 | $release->trakt = $release->video->trakt ?? null; |
||
357 | $release->tvrage = $release->video->tvrage ?? null; |
||
358 | $release->tvmaze = $release->video->tvmaze ?? null; |
||
359 | $release->source = $release->video->source ?? null; |
||
360 | $release->summary = $release->video->tvInfo->summary ?? null; |
||
361 | $release->image = $release->video->tvInfo->image ?? null; |
||
362 | $release->title = $release->episode->title ?? null; |
||
363 | $release->firstaired = $release->episode->firstaired ?? null; |
||
364 | $release->se_complete = $release->episode->se_complete ?? null; |
||
365 | $release->parent_category = $release->category->parent->title ?? null; |
||
366 | $release->sub_category = $release->category->title ?? null; |
||
367 | $release->category_name = $release->parent_category.' > '.$release->sub_category; |
||
368 | $release->category_ids = $release->category->parentid.','.$release->category->id; |
||
369 | $release->group_names = $release->releaseGroup->map(function ($relGroup) { |
||
370 | return $relGroup->group->name; |
||
371 | })->implode(','); |
||
372 | }); |
||
373 | |||
374 | return is_array($guid) ? $releases : $releases->first(); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Get a range of releases. used in admin manage list. |
||
379 | */ |
||
380 | public static function getFailedRange(): LengthAwarePaginator |
||
381 | { |
||
382 | $failedList = self::query() |
||
383 | ->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")]) |
||
384 | ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id') |
||
385 | ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id') |
||
386 | ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id') |
||
387 | ->orderByDesc('postdate'); |
||
388 | |||
389 | return $failedList->paginate(config('nntmux.items_per_page')); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * @return Release|false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null |
||
394 | */ |
||
395 | public static function getAlternate(string $guid, int $userid) |
||
396 | { |
||
397 | $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']); |
||
398 | |||
399 | if ($rel === null) { |
||
400 | return false; |
||
401 | } |
||
402 | DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]); |
||
403 | |||
404 | preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', $rel['searchname'], $similar); |
||
405 | |||
406 | if (! empty($similar)) { |
||
407 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
408 | $searchResult = (new ElasticSearchSiteSearch)->indexSearch($similar[1], 10); |
||
409 | } else { |
||
410 | $searchResult = (new ManticoreSearch)->searchIndexes('releases_rt', $similar[1]); |
||
411 | if (! empty($searchResult)) { |
||
412 | $searchResult = Arr::wrap(Arr::get($searchResult, 'id')); |
||
413 | } |
||
414 | } |
||
415 | |||
416 | if (empty($searchResult)) { |
||
417 | return false; |
||
418 | } |
||
419 | |||
420 | 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']); |
||
421 | } |
||
422 | |||
423 | return false; |
||
424 | } |
||
425 | |||
426 | public static function checkGuidForApi($guid): bool |
||
431 | } |
||
432 | } |
||
433 |