Total Complexity | 46 |
Total Lines | 527 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Category 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 Category, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Category extends Model |
||
45 | { |
||
46 | /** |
||
47 | * Category constants. |
||
48 | * Do NOT use the values, as they may change, always use the constant - that's what it's for. |
||
49 | */ |
||
50 | public const OTHER_MISC = 10; |
||
51 | |||
52 | public const OTHER_HASHED = 20; |
||
53 | |||
54 | public const GAME_NDS = 1010; |
||
55 | |||
56 | public const GAME_PSP = 1020; |
||
57 | |||
58 | public const GAME_WII = 1030; |
||
59 | |||
60 | public const GAME_XBOX = 1040; |
||
61 | |||
62 | public const GAME_XBOX360 = 1050; |
||
63 | |||
64 | public const GAME_WIIWARE = 1060; |
||
65 | |||
66 | public const GAME_XBOX360DLC = 1070; |
||
67 | |||
68 | public const GAME_PS3 = 1080; |
||
69 | |||
70 | public const GAME_OTHER = 1999; |
||
71 | |||
72 | public const GAME_3DS = 1110; |
||
73 | |||
74 | public const GAME_PSVITA = 1120; |
||
75 | |||
76 | public const GAME_WIIU = 1130; |
||
77 | |||
78 | public const GAME_XBOXONE = 1140; |
||
79 | |||
80 | public const GAME_PS4 = 1180; |
||
81 | |||
82 | public const MOVIE_FOREIGN = 2010; |
||
83 | |||
84 | public const MOVIE_OTHER = 2999; |
||
85 | |||
86 | public const MOVIE_SD = 2030; |
||
87 | |||
88 | public const MOVIE_HD = 2040; |
||
89 | |||
90 | public const MOVIE_UHD = 2045; |
||
91 | |||
92 | public const MOVIE_3D = 2050; |
||
93 | |||
94 | public const MOVIE_BLURAY = 2060; |
||
95 | |||
96 | public const MOVIE_DVD = 2070; |
||
97 | |||
98 | public const MOVIE_WEBDL = 2080; |
||
99 | |||
100 | public const MOVIE_X265 = 2090; |
||
101 | |||
102 | public const MUSIC_MP3 = 3010; |
||
103 | |||
104 | public const MUSIC_VIDEO = 3020; |
||
105 | |||
106 | public const MUSIC_AUDIOBOOK = 3030; |
||
107 | |||
108 | public const MUSIC_LOSSLESS = 3040; |
||
109 | |||
110 | public const MUSIC_PODCAST = 3050; |
||
111 | |||
112 | public const MUSIC_OTHER = 3999; |
||
113 | |||
114 | public const MUSIC_FOREIGN = 3060; |
||
115 | |||
116 | public const PC_0DAY = 4010; |
||
117 | |||
118 | public const PC_ISO = 4020; |
||
119 | |||
120 | public const PC_MAC = 4030; |
||
121 | |||
122 | public const PC_PHONE_OTHER = 4040; |
||
123 | |||
124 | public const PC_GAMES = 4050; |
||
125 | |||
126 | public const PC_PHONE_IOS = 4060; |
||
127 | |||
128 | public const PC_PHONE_ANDROID = 4070; |
||
129 | |||
130 | public const TV_WEBDL = 5010; |
||
131 | |||
132 | public const TV_FOREIGN = 5020; |
||
133 | |||
134 | public const TV_SD = 5030; |
||
135 | |||
136 | public const TV_HD = 5040; |
||
137 | |||
138 | public const TV_UHD = 5045; |
||
139 | |||
140 | public const TV_OTHER = 5999; |
||
141 | |||
142 | public const TV_SPORT = 5060; |
||
143 | |||
144 | public const TV_ANIME = 5070; |
||
145 | |||
146 | public const TV_DOCU = 5080; |
||
147 | |||
148 | public const TV_X265 = 5090; |
||
149 | |||
150 | public const XXX_DVD = 6010; |
||
151 | |||
152 | public const XXX_WMV = 6020; |
||
153 | |||
154 | public const XXX_XVID = 6030; |
||
155 | |||
156 | public const XXX_X264 = 6040; |
||
157 | |||
158 | public const XXX_CLIPHD = 6041; |
||
159 | |||
160 | public const XXX_CLIPSD = 6042; |
||
161 | |||
162 | public const XXX_UHD = 6045; |
||
163 | |||
164 | public const XXX_VR = 6046; |
||
165 | |||
166 | public const XXX_PACK = 6050; |
||
167 | |||
168 | public const XXX_IMAGESET = 6060; |
||
169 | |||
170 | public const XXX_OTHER = 6999; |
||
171 | |||
172 | public const XXX_SD = 6080; |
||
173 | |||
174 | public const XXX_WEBDL = 6090; |
||
175 | |||
176 | public const BOOKS_MAGAZINES = 7010; |
||
177 | |||
178 | public const BOOKS_EBOOK = 7020; |
||
179 | |||
180 | public const BOOKS_COMICS = 7030; |
||
181 | |||
182 | public const BOOKS_TECHNICAL = 7040; |
||
183 | |||
184 | public const BOOKS_FOREIGN = 7060; |
||
185 | |||
186 | public const BOOKS_UNKNOWN = 7999; |
||
187 | |||
188 | public const OTHER_ROOT = 1; |
||
189 | |||
190 | public const GAME_ROOT = 1000; |
||
191 | |||
192 | public const MOVIE_ROOT = 2000; |
||
193 | |||
194 | public const MUSIC_ROOT = 3000; |
||
195 | |||
196 | public const PC_ROOT = 4000; |
||
197 | |||
198 | public const TV_ROOT = 5000; |
||
199 | |||
200 | public const XXX_ROOT = 6000; |
||
201 | |||
202 | public const BOOKS_ROOT = 7000; |
||
203 | |||
204 | public const STATUS_INACTIVE = 0; |
||
205 | |||
206 | public const STATUS_ACTIVE = 1; |
||
207 | |||
208 | public const STATUS_DISABLED = 2; |
||
209 | |||
210 | public const OTHERS_GROUP = |
||
211 | [ |
||
212 | self::BOOKS_UNKNOWN, |
||
213 | self::GAME_OTHER, |
||
214 | self::MOVIE_OTHER, |
||
215 | self::MUSIC_OTHER, |
||
216 | self::PC_PHONE_OTHER, |
||
217 | self::TV_OTHER, |
||
218 | self::OTHER_HASHED, |
||
219 | self::XXX_OTHER, |
||
220 | self::OTHER_MISC, |
||
221 | ]; |
||
222 | |||
223 | public const MOVIES_GROUP = |
||
224 | [ |
||
225 | self::MOVIE_FOREIGN, |
||
226 | self::MOVIE_ROOT, |
||
227 | self::MOVIE_OTHER, |
||
228 | self::MOVIE_SD, |
||
229 | self::MOVIE_HD, |
||
230 | self::MOVIE_UHD, |
||
231 | self::MOVIE_3D, |
||
232 | self::MOVIE_BLURAY, |
||
233 | self::MOVIE_DVD, |
||
234 | self::MOVIE_WEBDL, |
||
235 | self::MOVIE_X265, |
||
236 | ]; |
||
237 | |||
238 | public const TV_GROUP = |
||
239 | [ |
||
240 | self::TV_FOREIGN, |
||
241 | self::TV_ROOT, |
||
242 | self::TV_OTHER, |
||
243 | self::TV_SD, |
||
244 | self::TV_HD, |
||
245 | self::TV_UHD, |
||
246 | self::TV_ANIME, |
||
247 | self::TV_DOCU, |
||
248 | self::TV_SPORT, |
||
249 | self::TV_WEBDL, |
||
250 | self::TV_X265, |
||
251 | ]; |
||
252 | |||
253 | /** |
||
254 | * @var string |
||
255 | */ |
||
256 | |||
257 | /** |
||
258 | * @var bool |
||
259 | */ |
||
260 | public $timestamps = false; |
||
261 | |||
262 | /** |
||
263 | * @var bool |
||
264 | */ |
||
265 | protected $dateFormat = false; |
||
266 | |||
267 | /** |
||
268 | * @var array |
||
269 | */ |
||
270 | protected $guarded = []; |
||
271 | |||
272 | public function releases(): HasMany |
||
275 | } |
||
276 | |||
277 | public function parent(): BelongsTo |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] |
||
284 | */ |
||
285 | public static function getRecentlyAdded() |
||
305 | } |
||
306 | |||
307 | public static function getCategorySearch(array $cat = [], ?string $searchType = null, $builder = false): string|array|null |
||
308 | { |
||
309 | $categories = []; |
||
310 | |||
311 | // if searchType is tv return TV categories |
||
312 | if ($searchType === 'tv') { |
||
313 | $cat = self::TV_GROUP; |
||
314 | } |
||
315 | |||
316 | // is searchType is movies return MOVIES categories |
||
317 | if ($searchType === 'movies') { |
||
318 | $cat = self::MOVIES_GROUP; |
||
319 | } |
||
320 | |||
321 | // If multiple categories were sent in a single array position, slice and add them |
||
322 | if (str_contains($cat[0], ',')) { |
||
323 | $tmpcats = explode(',', $cat[0]); |
||
324 | // Reset the category to the first comma separated value in the string |
||
325 | $cat[0] = $tmpcats[0]; |
||
326 | // Add the remaining categories in the string to the original array |
||
327 | foreach (array_slice($tmpcats, 1) as $tmpcat) { |
||
328 | $cat[] = $tmpcat; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | foreach ($cat as $category) { |
||
333 | if (is_numeric($category) && $category !== -1 && self::isParent($category)) { |
||
334 | $children = RootCategory::find($category)->categories->pluck('id')->toArray(); |
||
335 | $categories = array_merge($categories, $children); |
||
336 | } elseif (is_numeric($category) && $category > 0) { |
||
337 | $categories[] = $category; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | $catCount = count($categories); |
||
342 | |||
343 | if ($builder) { |
||
344 | return match ($catCount) { |
||
345 | 0 => null, |
||
346 | 1 => $categories[0] !== -1 ? $categories : null, |
||
347 | default => $categories, |
||
348 | }; |
||
349 | } |
||
350 | |||
351 | return match ($catCount) { |
||
352 | 0 => 'AND 1=1', |
||
353 | 1 => $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : '', |
||
354 | default => ' AND r.categories_id IN ('.implode(', ', $categories).') ', |
||
355 | }; |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Returns a concatenated list of other categories. |
||
360 | */ |
||
361 | public static function getCategoryOthersGroup(): string |
||
362 | { |
||
363 | return implode( |
||
364 | ',', |
||
365 | self::OTHERS_GROUP |
||
366 | ); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return mixed |
||
371 | */ |
||
372 | public static function getCategoryValue($category) |
||
373 | { |
||
374 | return \constant('self::'.$category); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * Check if category is parent. |
||
379 | */ |
||
380 | public static function isParent($cid): bool |
||
381 | { |
||
382 | $ret = RootCategory::query()->where(['id' => $cid])->first(); |
||
383 | |||
384 | return $ret !== null; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @return \Illuminate\Database\Eloquent\Collection|static[] |
||
389 | */ |
||
390 | public static function getFlat() |
||
391 | { |
||
392 | return self::query()->get(); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Get children of a parent category. |
||
397 | * |
||
398 | * |
||
399 | * @return mixed |
||
400 | */ |
||
401 | public static function getChildren($categoryId) |
||
402 | { |
||
403 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
404 | $result = Cache::get(md5($categoryId)); |
||
405 | if ($result !== null) { |
||
406 | return $result; |
||
407 | } |
||
408 | |||
409 | $result = RootCategory::find($categoryId)->categories; |
||
410 | Cache::put(md5($categoryId), $result, $expiresAt); |
||
411 | |||
412 | return $result; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Get names of enabled parent categories. |
||
417 | * |
||
418 | * @return \Illuminate\Database\Eloquent\Collection|static[] |
||
419 | */ |
||
420 | public static function getEnabledParentNames() |
||
421 | { |
||
422 | return RootCategory::query()->where('status', '=', 1)->get(['title']); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Returns category ID's for site disabled categories. |
||
427 | * |
||
428 | * |
||
429 | * @return \Illuminate\Database\Eloquent\Collection|static[] |
||
430 | */ |
||
431 | public static function getDisabledIDs() |
||
432 | { |
||
433 | return self::query() |
||
434 | ->where('status', '=', 2) |
||
435 | ->get(['id']); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Get multiple categories. |
||
440 | * |
||
441 | * @return bool|\Illuminate\Database\Eloquent\Collection|static[] |
||
442 | */ |
||
443 | public static function getByIds($ids) |
||
444 | { |
||
445 | if (\count($ids) > 0) { |
||
446 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
447 | $result = Cache::get(md5(implode(',', $ids))); |
||
448 | if ($result !== null) { |
||
449 | return $result; |
||
450 | } |
||
451 | $result = self::query()->whereIn('id', $ids)->get(); |
||
452 | |||
453 | Cache::put(md5(md5(implode(',', $ids))), $result, $expiresAt); |
||
454 | |||
455 | return $result; |
||
456 | } |
||
457 | |||
458 | return false; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Return the parent and category name from the supplied categoryID. |
||
463 | */ |
||
464 | public static function getNameByID($categoryId): string |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * @return bool|mixed |
||
473 | */ |
||
474 | public static function getIdByName($title, $parent) |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Update a category. |
||
483 | */ |
||
484 | public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int |
||
485 | { |
||
486 | return self::query()->where('id', $id)->update( |
||
487 | [ |
||
488 | 'disablepreview' => $disablepreview, |
||
489 | 'status' => $status, |
||
490 | 'minsizetoformrelease' => $minsize, |
||
491 | 'maxsizetoformrelease' => $maxsize, |
||
492 | 'description' => $desc, |
||
493 | ] |
||
494 | ); |
||
495 | } |
||
496 | |||
497 | public static function getForMenu(array $excludedCats = []): array |
||
498 | { |
||
499 | $categoriesResult = []; |
||
500 | $categoriesArray = RootCategory::query()->with(['categories' => function ($query) use ($excludedCats) { |
||
501 | if (! empty($excludedCats)) { |
||
502 | $query->whereNotIn('id', $excludedCats); |
||
503 | } |
||
504 | $query->select(['id', 'title', 'root_categories_id', 'description']); |
||
505 | }])->select(['id', 'title'])->get()->toArray(); |
||
506 | |||
507 | foreach ($categoriesArray as $category) { |
||
508 | if (! empty($category['categories'])) { |
||
509 | $categoriesResult[] = $category; |
||
510 | } |
||
511 | } |
||
512 | |||
513 | return $categoriesResult; |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * @return mixed |
||
518 | */ |
||
519 | public static function getForApi() |
||
520 | { |
||
521 | $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long')); |
||
522 | $result = Cache::get(md5('ForApi')); |
||
523 | if ($result !== null) { |
||
524 | return $result; |
||
525 | } |
||
526 | |||
527 | $result = RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get(); |
||
528 | |||
529 | Cache::put(md5('ForApi'), $result, $expiresAt); |
||
530 | |||
531 | return $result; |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * Return a list of categories for use in a dropdown. |
||
536 | */ |
||
537 | public static function getForSelect(bool $blnIncludeNoneSelected = true): array |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection |
||
554 | */ |
||
555 | public static function getCategories(bool $activeOnly = false, array $excludedCats = []): \Illuminate\Database\Eloquent\Collection|array |
||
571 | } |
||
572 | } |
||
573 |