Passed
Push — master ( 831fef...0af2a4 )
by Darko
07:22
created

Category::getCategorySearch()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 18
c 2
b 1
f 0
dl 0
loc 32
rs 7.6666
cc 10
nc 8
nop 1

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Support\Facades\Cache;
9
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * App\Models\Category.
13
 *
14
 * @property int $id
15
 * @property string $title
16
 * @property int|null $parentid
17
 * @property int $status
18
 * @property string|null $description
19
 * @property bool $disablepreview
20
 * @property int $minsizetoformrelease
21
 * @property int $maxsizetoformrelease
22
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $children
23
 * @property-read Category|null $parent
24
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $releases
25
 *
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDescription($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereDisablepreview($value)
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMaxsizetoformrelease($value)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereMinsizetoformrelease($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereParentid($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereStatus($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTitle($value)
34
 *
35
 * @mixin \Eloquent
36
 *
37
 * @property int|null $root_categories_id
38
 *
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newModelQuery()
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newQuery()
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category query()
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereRootCategoriesId($value)
43
 */
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
273
    {
274
        return $this->hasMany(Release::class, 'categories_id');
275
    }
276
277
    public function parent(): BelongsTo
278
    {
279
        return $this->belongsTo(RootCategory::class, 'root_categories_id');
280
    }
281
282
    /**
283
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
284
     */
285
    public static function getRecentlyAdded()
286
    {
287
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
288
        $result = Cache::get(md5('RecentlyAdded'));
289
        if ($result !== null) {
290
            return $result;
291
        }
292
293
        $result = self::query()
294
            ->with('parent')
295
            ->where('r.adddate', '>', now()->subWeek())
296
            ->select(['root_categories_id', DB::raw('COUNT(r.id) as count'), 'title'])
297
            ->join('releases as r', 'r.categories_id', '=', 'categories.id')
298
            ->groupBy('title')
299
            ->orderByDesc('count')
0 ignored issues
show
Bug introduced by
'count' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderByDesc(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

299
            ->orderByDesc(/** @scrutinizer ignore-type */ 'count')
Loading history...
300
            ->get();
301
302
        Cache::put(md5('RecentlyAdded'), $result, $expiresAt);
303
304
        return $result;
305
    }
306
307
    public static function getCategorySearch(array $cat = []): string
308
    {
309
        $categories = [];
310
311
        // If multiple categories were sent in a single array position, slice and add them
312
        if (strpos($cat[0], ',') !== false) {
313
            $tmpcats = explode(',', $cat[0]);
314
            // Reset the category to the first comma separated value in the string
315
            $cat[0] = $tmpcats[0];
316
            // Add the remaining categories in the string to the original array
317
            foreach (array_slice($tmpcats, 1) as $tmpcat) {
318
                $cat[] = $tmpcat;
319
            }
320
        }
321
322
        foreach ($cat as $category) {
323
            if (is_numeric($category) && $category !== -1 && self::isParent($category)) {
324
                $children = RootCategory::find($category)->categories->pluck('id')->toArray();
0 ignored issues
show
Bug introduced by
The property categories does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
325
                $categories = array_merge($categories, $children);
326
            } elseif (is_numeric($category) && $category > 0) {
327
                $categories[] = $category;
328
            }
329
        }
330
331
        $catCount = count($categories);
332
        $catSearch = match ($catCount) {
333
            0 => 'AND 1=1',
334
            1 => $categories[0] !== -1 ? ' AND r.categories_id = '.$categories[0] : '',
335
            default => ' AND r.categories_id IN ('.implode(', ', $categories).') ',
336
        };
337
338
        return $catSearch;
339
    }
340
341
    /**
342
     * Returns a concatenated list of other categories.
343
     */
344
    public static function getCategoryOthersGroup(): string
345
    {
346
        return implode(
347
            ',',
348
            self::OTHERS_GROUP
349
        );
350
    }
351
352
    /**
353
     * @return mixed
354
     */
355
    public static function getCategoryValue($category)
356
    {
357
        return \constant('self::'.$category);
358
    }
359
360
    /**
361
     * Check if category is parent.
362
     */
363
    public static function isParent($cid): bool
364
    {
365
        $ret = RootCategory::query()->where(['id' => $cid])->first();
366
367
        return $ret !== null;
368
    }
369
370
    /**
371
     * @return \Illuminate\Database\Eloquent\Collection|static[]
372
     */
373
    public static function getFlat()
374
    {
375
        return self::query()->get();
376
    }
377
378
    /**
379
     * Get children of a parent category.
380
     *
381
     *
382
     * @return mixed
383
     */
384
    public static function getChildren($categoryId)
385
    {
386
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
387
        $result = Cache::get(md5($categoryId));
388
        if ($result !== null) {
389
            return $result;
390
        }
391
392
        $result = RootCategory::find($categoryId)->categories;
0 ignored issues
show
Bug introduced by
The property categories does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
393
        Cache::put(md5($categoryId), $result, $expiresAt);
394
395
        return $result;
396
    }
397
398
    /**
399
     * Get names of enabled parent categories.
400
     *
401
     * @return \Illuminate\Database\Eloquent\Collection|static[]
402
     */
403
    public static function getEnabledParentNames()
404
    {
405
        return RootCategory::query()->where('status', '=', 1)->get(['title']);
406
    }
407
408
    /**
409
     * Returns category ID's for site disabled categories.
410
     *
411
     *
412
     * @return \Illuminate\Database\Eloquent\Collection|static[]
413
     */
414
    public static function getDisabledIDs()
415
    {
416
        return self::query()
417
            ->where('status', '=', 2)
418
            ->get(['id']);
419
    }
420
421
    /**
422
     * Get multiple categories.
423
     *
424
     * @return bool|\Illuminate\Database\Eloquent\Collection|static[]
425
     */
426
    public static function getByIds($ids)
427
    {
428
        if (\count($ids) > 0) {
429
            $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
430
            $result = Cache::get(md5(implode(',', $ids)));
431
            if ($result !== null) {
432
                return $result;
433
            }
434
            $result = self::query()->whereIn('id', $ids)->get();
435
436
            Cache::put(md5(md5(implode(',', $ids))), $result, $expiresAt);
437
438
            return $result;
439
        }
440
441
        return false;
442
    }
443
444
    /**
445
     * Return the parent and category name from the supplied categoryID.
446
     */
447
    public static function getNameByID($categoryId): string
448
    {
449
        $cat = self::query()->where('id', $categoryId)->first();
450
451
        return $cat !== null ? $cat->parent->title.' -> '.$cat->title : '';
452
    }
453
454
    /**
455
     * @return bool|mixed
456
     */
457
    public static function getIdByName($title, $parent)
458
    {
459
        $cat = self::query()->where('title', $title)->with('parent.'.$parent)->first(['id']);
460
461
        return $cat !== null ? $cat->id : false;
462
    }
463
464
    /**
465
     * Update a category.
466
     */
467
    public static function updateCategory($id, $status, $desc, $disablepreview, $minsize, $maxsize): int
468
    {
469
        return self::query()->where('id', $id)->update(
470
            [
471
                'disablepreview' => $disablepreview,
472
                'status' => $status,
473
                'minsizetoformrelease' => $minsize,
474
                'maxsizetoformrelease' => $maxsize,
475
                'description' => $desc,
476
            ]
477
        );
478
    }
479
480
    public static function getForMenu(array $excludedCats = []): array
481
    {
482
        $categoriesResult = [];
483
        $categoriesArray = RootCategory::query()->with(['categories' => function ($query) use ($excludedCats) {
484
            if (! empty($excludedCats)) {
485
                $query->whereNotIn('id', $excludedCats);
486
            }
487
            $query->select(['id', 'title', 'root_categories_id', 'description']);
488
        }])->select(['id', 'title'])->get()->toArray();
489
490
        foreach ($categoriesArray as $category) {
491
            if (! empty($category['categories'])) {
492
                $categoriesResult[] = $category;
493
            }
494
        }
495
496
        return $categoriesResult;
497
    }
498
499
    /**
500
     * @return mixed
501
     */
502
    public static function getForApi()
503
    {
504
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
505
        $result = Cache::get(md5('ForApi'));
506
        if ($result !== null) {
507
            return $result;
508
        }
509
510
        $result = RootCategory::query()->select(['id', 'title'])->where('status', '=', self::STATUS_ACTIVE)->get();
511
512
        Cache::put(md5('ForApi'), $result, $expiresAt);
513
514
        return $result;
515
    }
516
517
    /**
518
     * Return a list of categories for use in a dropdown.
519
     */
520
    public static function getForSelect(bool $blnIncludeNoneSelected = true): array
521
    {
522
        $categories = self::getCategories();
523
        $temp_array = [];
524
525
        if ($blnIncludeNoneSelected) {
526
            $temp_array[-1] = '--Please Select--';
527
        }
528
        foreach ($categories as $category) {
529
            $temp_array[$category->id] = $category->parent->title.' > '.$category->title;
530
        }
531
532
        return $temp_array;
533
    }
534
535
    /**
536
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
537
     */
538
    public static function getCategories(bool $activeOnly = false, array $excludedCats = []): \Illuminate\Database\Eloquent\Collection|array
539
    {
540
        $sql = self::query()
541
            ->with('parent')
542
            ->select(['id', 'status', 'title', 'root_categories_id'])
543
            ->orderBy('id');
0 ignored issues
show
Bug introduced by
'id' of type string is incompatible with the type Closure|Illuminate\Datab...\Database\Query\Builder expected by parameter $column of Illuminate\Database\Query\Builder::orderBy(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

543
            ->orderBy(/** @scrutinizer ignore-type */ 'id');
Loading history...
544
545
        if ($activeOnly) {
546
            $sql->where('status', '=', self::STATUS_ACTIVE);
547
        }
548
549
        if (! empty($excludedCats)) {
550
            $sql->whereNotIn('id', $excludedCats);
551
        }
552
553
        return $sql->get();
554
    }
555
}
556