Passed
Push — master ( bd6b2e...6b5d90 )
by Darko
06:14
created

Release   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 405
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 197
dl 0
loc 405
rs 8.8798
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 0 3 1
A failed() 0 3 1
A group() 0 3 1
A userRelease() 0 3 1
A file() 0 3 1
A predb() 0 3 1
A category() 0 3 1
A releaseGroup() 0 3 1
A getCatByRelId() 0 3 1
A checkGuidForApi() 0 5 1
A nfo() 0 3 1
A updateGrab() 0 5 2
A removeAnidbIdFromReleases() 0 3 1
A getAlternate() 0 29 6
A updateRelease() 0 29 4
A getReleases() 0 19 2
A getReleasesRange() 0 34 2
A getTopDownloads() 0 20 2
A comment() 0 3 1
A getTopComments() 0 20 2
A insertRelease() 0 34 3
A getByGuid() 0 43 4
A removeVideoIdFromReleases() 0 3 1
A episode() 0 3 1
A getFailedRange() 0 10 1
A video() 0 3 1

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Models;
4
5
use Blacklight\ElasticSearchSiteSearch;
6
use Blacklight\ManticoreSearch;
7
use Blacklight\NZB;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Database\Eloquent\Factories\HasFactory;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\HasMany;
13
use Illuminate\Database\Eloquent\Relations\HasOne;
14
use Illuminate\Support\Arr;
15
use Illuminate\Support\Facades\Cache;
16
use Illuminate\Support\Facades\DB;
17
18
/**
19
 * App\Models\Release.
20
 *
21
 * @property int $id
22
 * @property string $name
23
 * @property string $searchname
24
 * @property int|null $totalpart
25
 * @property int $groups_id FK to groups.id
26
 * @property int $size
27
 * @property string|null $postdate
28
 * @property string|null $adddate
29
 * @property string $updatetime
30
 * @property string|null $gid
31
 * @property string $guid
32
 * @property string $leftguid The first letter of the release guid
33
 * @property string|null $fromname
34
 * @property float $completion
35
 * @property int $categories_id
36
 * @property int $videos_id FK to videos.id of the parent series.
37
 * @property int $tv_episodes_id FK to tv_episodes.id for the episode.
38
 * @property int|null $imdbid
39
 * @property int $xxxinfo_id
40
 * @property int|null $musicinfo_id FK to musicinfo.id
41
 * @property int|null $consoleinfo_id FK to consoleinfo.id
42
 * @property int $gamesinfo_id
43
 * @property int|null $bookinfo_id FK to bookinfo.id
44
 * @property int|null $anidbid FK to anidb_titles.anidbid
45
 * @property int $predb_id FK to predb.id
46
 * @property int $grabs
47
 * @property int $comments
48
 * @property bool $passwordstatus
49
 * @property int $rarinnerfilecount
50
 * @property bool $haspreview
51
 * @property bool $nfostatus
52
 * @property bool $jpgstatus
53
 * @property bool $videostatus
54
 * @property bool $audiostatus
55
 * @property bool $dehashstatus
56
 * @property bool $reqidstatus
57
 * @property bool $nzbstatus
58
 * @property bool $iscategorized
59
 * @property bool $isrenamed
60
 * @property bool $ishashed
61
 * @property bool $proc_pp
62
 * @property bool $proc_sorter
63
 * @property bool $proc_par2
64
 * @property bool $proc_nfo
65
 * @property bool $proc_files
66
 * @property bool $proc_uid
67
 * @property bool $proc_srr Has the release been srr
68
 *                          processed
69
 * @property bool $proc_hash16k Has the release been hash16k
70
 *                              processed
71
 * @property mixed|null $nzb_guid
72
 * @property-read Category                                                    $category
73
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseComment[]   $comment
74
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDownload[]     $download
75
 * @property-read TvEpisode                                                   $episode
76
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DnzbFailure[]      $failed
77
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseFile[]      $file
78
 * @property-read UsenetGroup                                                 $group
79
 * @property-read ReleaseNfo                                                  $nfo
80
 * @property-read Predb                                                       $predb
81
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseExtraFull[] $releaseExtra
82
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleasesGroups[]   $releaseGroup
83
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UsersRelease[]     $userRelease
84
 * @property-read Video                                                       $video
85
 *
86
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAdddate($value)
87
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAnidbid($value)
88
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAudiostatus($value)
89
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereBookinfoId($value)
90
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereCategoriesId($value)
91
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereComments($value)
92
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereCompletion($value)
93
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereConsoleinfoId($value)
94
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereDehashstatus($value)
95
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereFromname($value)
96
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGamesinfoId($value)
97
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGid($value)
98
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGrabs($value)
99
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGroupsId($value)
100
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGuid($value)
101
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereHaspreview($value)
102
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereId($value)
103
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereImdbid($value)
104
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIscategorized($value)
105
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIshashed($value)
106
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIsrenamed($value)
107
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereJpgstatus($value)
108
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereLeftguid($value)
109
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereMusicinfoId($value)
110
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereName($value)
111
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNfostatus($value)
112
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNzbGuid($value)
113
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNzbstatus($value)
114
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePasswordstatus($value)
115
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePostdate($value)
116
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePredbId($value)
117
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcFiles($value)
118
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcHash16k($value)
119
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcNfo($value)
120
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcPar2($value)
121
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcPp($value)
122
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcSorter($value)
123
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcSrr($value)
124
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcUid($value)
125
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereRarinnerfilecount($value)
126
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereReqidstatus($value)
127
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereSearchname($value)
128
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereSize($value)
129
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTotalpart($value)
130
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTvEpisodesId($value)
131
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereUpdatetime($value)
132
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereVideosId($value)
133
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereVideostatus($value)
134
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereXxxinfoId($value)
135
 *
136
 * @mixin \Eloquent
137
 *
138
 * @property int|null $movieinfo_id FK to movieinfo.id
139
 * @property int $proc_crc32 Has the release been crc32 processed
140
 * @property-read \Illuminate\Database\Eloquent\Collection
141
 * @property-read \Illuminate\Database\Eloquent\Collection
142
 *
143
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release newModelQuery()
144
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release newQuery()
145
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release query()
146
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereMovieinfoId($value)
147
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcCrc32($value)
148
 */
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
174
    {
175
        return $this->hasMany(UserDownload::class, 'releases_id');
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
199
    {
200
        return $this->hasMany(DnzbFailure::class, 'release_id');
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
214
    {
215
        return $this->hasMany(ReleasesGroups::class, 'releases_id');
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'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parameters['id'] also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type boolean|integer.
Loading history...
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)
322
    {
323
        return self::whereId($id)->first(['categories_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
550
    {
551
        $check = self::whereGuid($guid)->first();
552
553
        return $check !== null;
554
    }
555
}
556