Completed
Push — dev ( e5a31e...27c800 )
by Darko
06:50
created

Release::updateRelease()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 47
ccs 0
cts 21
cp 0
rs 9.0648
cc 5
nc 8
nop 15
crap 30

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Models;
4
5
use Blacklight\NZB;
6
use Blacklight\SphinxSearch;
7
use Conner\Tagging\Taggable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\Cache;
10
use Illuminate\Support\Facades\DB;
11
12
/**
13
 * App\Models\Release.
14
 *
15
 * @property int $id
16
 * @property string $name
17
 * @property string $searchname
18
 * @property int|null $totalpart
19
 * @property int $groups_id FK to groups.id
20
 * @property int $size
21
 * @property string|null $postdate
22
 * @property string|null $adddate
23
 * @property string $updatetime
24
 * @property string|null $gid
25
 * @property string $guid
26
 * @property string $leftguid The first letter of the release guid
27
 * @property string|null $fromname
28
 * @property float $completion
29
 * @property int $categories_id
30
 * @property int $videos_id FK to videos.id of the parent series.
31
 * @property int $tv_episodes_id FK to tv_episodes.id for the episode.
32
 * @property int|null $imdbid
33
 * @property int $xxxinfo_id
34
 * @property int|null $musicinfo_id FK to musicinfo.id
35
 * @property int|null $consoleinfo_id FK to consoleinfo.id
36
 * @property int $gamesinfo_id
37
 * @property int|null $bookinfo_id FK to bookinfo.id
38
 * @property int|null $anidbid FK to anidb_titles.anidbid
39
 * @property int $predb_id FK to predb.id
40
 * @property int $grabs
41
 * @property int $comments
42
 * @property bool $passwordstatus
43
 * @property int $rarinnerfilecount
44
 * @property bool $haspreview
45
 * @property bool $nfostatus
46
 * @property bool $jpgstatus
47
 * @property bool $videostatus
48
 * @property bool $audiostatus
49
 * @property bool $dehashstatus
50
 * @property bool $reqidstatus
51
 * @property bool $nzbstatus
52
 * @property bool $iscategorized
53
 * @property bool $isrenamed
54
 * @property bool $ishashed
55
 * @property bool $proc_pp
56
 * @property bool $proc_sorter
57
 * @property bool $proc_par2
58
 * @property bool $proc_nfo
59
 * @property bool $proc_files
60
 * @property bool $proc_uid
61
 * @property bool $proc_srr Has the release been srr
62
 * processed
63
 * @property bool $proc_hash16k Has the release been hash16k
64
 * processed
65
 * @property mixed|null $nzb_guid
66
 * @property-read \App\Models\Category                                                    $category
67
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseComment[]   $comment
68
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserDownload[]     $download
69
 * @property-read \App\Models\TvEpisode                                                   $episode
70
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DnzbFailure[]      $failed
71
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseFile[]      $file
72
 * @property-read \App\Models\UsenetGroup                                                 $group
73
 * @property-read \App\Models\ReleaseNfo                                                  $nfo
74
 * @property-read \App\Models\Predb                                                       $predb
75
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleaseExtraFull[] $releaseExtra
76
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ReleasesGroups[]   $releaseGroup
77
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UsersRelease[]     $userRelease
78
 * @property-read \App\Models\Video                                                       $video
79
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAdddate($value)
80
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAnidbid($value)
81
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereAudiostatus($value)
82
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereBookinfoId($value)
83
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereCategoriesId($value)
84
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereComments($value)
85
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereCompletion($value)
86
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereConsoleinfoId($value)
87
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereDehashstatus($value)
88
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereFromname($value)
89
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGamesinfoId($value)
90
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGid($value)
91
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGrabs($value)
92
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGroupsId($value)
93
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereGuid($value)
94
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereHaspreview($value)
95
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereId($value)
96
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereImdbid($value)
97
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIscategorized($value)
98
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIshashed($value)
99
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereIsrenamed($value)
100
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereJpgstatus($value)
101
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereLeftguid($value)
102
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereMusicinfoId($value)
103
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereName($value)
104
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNfostatus($value)
105
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNzbGuid($value)
106
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereNzbstatus($value)
107
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePasswordstatus($value)
108
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePostdate($value)
109
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release wherePredbId($value)
110
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcFiles($value)
111
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcHash16k($value)
112
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcNfo($value)
113
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcPar2($value)
114
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcPp($value)
115
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcSorter($value)
116
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcSrr($value)
117
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcUid($value)
118
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereRarinnerfilecount($value)
119
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereReqidstatus($value)
120
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereSearchname($value)
121
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereSize($value)
122
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTotalpart($value)
123
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereTvEpisodesId($value)
124
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereUpdatetime($value)
125
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereVideosId($value)
126
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereVideostatus($value)
127
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereXxxinfoId($value)
128
 * @mixin \Eloquent
129
 * @property int|null $movieinfo_id FK to movieinfo.id
130
 * @property int $proc_crc32 Has the release been crc32 processed
131
 * @property mixed $tag_names
132
 * @property-read \Illuminate\Database\Eloquent\Collection|\Tagged[] $tags
133
 * @property-read \Illuminate\Database\Eloquent\Collection|\Conner\Tagging\Model\Tagged[] $tagged
134
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release newModelQuery()
135
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release newQuery()
136
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release query()
137
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereMovieinfoId($value)
138
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release whereProcCrc32($value)
139
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release withAllTags($tagNames)
140
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release withAnyTag($tagNames)
141
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Release withoutTags($tagNames)
142
 */
143
class Release extends Model
144
{
145
    use Taggable;
0 ignored issues
show
introduced by
The trait Conner\Tagging\Taggable requires some properties which are not provided by App\Models\Release: $tag, $tag_slug, $tag_name
Loading history...
146
147
    /**
148
     * @var bool
149
     */
150
    protected $dateFormat = false;
151
152
    /**
153
     * @var bool
154
     */
155
    public $timestamps = false;
156
157
    /**
158
     * @var array
159
     */
160
    protected $guarded = [];
161
162
    /**
163
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
164
     */
165
    public function group()
166
    {
167
        return $this->belongsTo(UsenetGroup::class, 'groups_id');
168
    }
169
170
    /**
171
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
172
     */
173
    public function download()
174
    {
175
        return $this->hasMany(UserDownload::class, 'releases_id');
176
    }
177
178
    /**
179
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
180
     */
181
    public function userRelease()
182
    {
183
        return $this->hasMany(UsersRelease::class, 'releases_id');
184
    }
185
186
    public function file()
187
    {
188
        return $this->hasMany(ReleaseFile::class, 'releases_id');
189
    }
190
191
    /**
192
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
193
     */
194
    public function category()
195
    {
196
        return $this->belongsTo(Category::class, 'categories_id');
197
    }
198
199
    /**
200
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
201
     */
202
    public function predb()
203
    {
204
        return $this->belongsTo(Predb::class, 'predb_id');
205
    }
206
207
    /**
208
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
209
     */
210
    public function failed()
211
    {
212
        return $this->hasMany(DnzbFailure::class, 'release_id');
213
    }
214
215
    /**
216
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
217
     */
218
    public function releaseExtra()
219
    {
220
        return $this->hasMany(ReleaseExtraFull::class, 'releases_id');
221
    }
222
223
    /**
224
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
225
     */
226
    public function nfo()
227
    {
228
        return $this->hasOne(ReleaseNfo::class, 'releases_id');
229
    }
230
231
    /**
232
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
233
     */
234
    public function comment()
235
    {
236
        return $this->hasMany(ReleaseComment::class, 'releases_id');
237
    }
238
239
    /**
240
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
241
     */
242
    public function releaseGroup()
243
    {
244
        return $this->hasMany(ReleasesGroups::class, 'releases_id');
245
    }
246
247
    /**
248
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
249
     */
250
    public function video()
251
    {
252
        return $this->belongsTo(Video::class, 'videos_id');
253
    }
254
255
    /**
256
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
257
     */
258
    public function episode()
259
    {
260
        return $this->belongsTo(TvEpisode::class, 'tv_episodes_id');
261
    }
262
263
    /**
264
     * Insert a single release returning the ID on success or false on failure.
265
     *
266
     * @param array $parameters Insert parameters, must be escaped if string.
267
     *
268
     * @return bool|int
269
     * @throws \Exception
270
     */
271
    public static function insertRelease(array $parameters = [])
272
    {
273
        $passwordStatus = ((int) Settings::settingValue('..checkpasswordedrar') === 1 ? -1 : 0);
274
        $parameters['id'] = self::query()
275
            ->insertGetId(
276
                [
277
                    'name' => $parameters['name'],
278
                    'searchname' => $parameters['searchname'],
279
                    'totalpart' => $parameters['totalpart'],
280
                    'groups_id' => $parameters['groups_id'],
281
                    'adddate' => now(),
282
                    'guid' => $parameters['guid'],
283
                    'leftguid' => $parameters['guid'][0],
284
                    'postdate' => $parameters['postdate'],
285
                    'fromname' => $parameters['fromname'],
286
                    'size' => $parameters['size'],
287
                    'passwordstatus' => $passwordStatus,
288
                    'haspreview' => -1,
289
                    'categories_id' => $parameters['categories_id'],
290
                    'nfostatus' => -1,
291
                    'nzbstatus' => $parameters['nzbstatus'],
292
                    'isrenamed' => $parameters['isrenamed'],
293
                    'iscategorized' => 1,
294
                    'predb_id' => $parameters['predb_id'],
295
                ]
296
            );
297
298
        if (config('nntmux.elasticsearch_enabled') === true) {
299
            $data = [
300
                'body' => [
301
                    'id' => $parameters['id'],
302
                    'name' => $parameters['name'],
303
                    'searchname' => $parameters['searchname'],
304
                    'fromname' => $parameters['fromname'],
305
                    'filename' => $parameters['filename'],
306
                ],
307
                'index' => 'releases',
308
                'id' => $parameters['id'],
309
            ];
310
311
            Elasticsearch::index($data);
0 ignored issues
show
Bug introduced by
The type App\Models\Elasticsearch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
312
        } else {
313
            (new SphinxSearch())->insertRelease($parameters);
314
        }
315
316
        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...
317
    }
318
319
    /**
320
     * Used for release edit page on site.
321
     *
322
     * @param int $ID
323
     * @param string $name
324
     * @param string $searchName
325
     * @param string $fromName
326
     * @param int $categoryID
327
     * @param int $parts
328
     * @param int $grabs
329
     * @param int $size
330
     * @param string $postedDate
331
     * @param string $addedDate
332
     * @param        $videoId
333
     * @param        $episodeId
334
     * @param int $imDbID
335
     * @param int $aniDbID
336
     * @param string $tags
337
     * @throws \Exception
338
     */
339
    public static function updateRelease($ID, $name, $searchName, $fromName, $categoryID, $parts, $grabs, $size, $postedDate, $addedDate, $videoId, $episodeId, $imDbID, $aniDbID, string $tags = ''): void
340
    {
341
        $movieInfoId = null;
342
        if (! empty($imDbID)) {
343
            $movieInfoId = MovieInfo::whereImdbid($imDbID)->first(['id']);
344
        }
345
        self::whereId($ID)->update(
346
            [
347
                'name' => $name,
348
                'searchname' => $searchName,
349
                'fromname' => $fromName,
350
                'categories_id' => $categoryID,
351
                'totalpart' => $parts,
352
                'grabs' => $grabs,
353
                'size' => $size,
354
                'postdate' => $postedDate,
355
                'adddate' => $addedDate,
356
                'videos_id' => $videoId,
357
                'tv_episodes_id' => $episodeId,
358
                'imdbid' => $imDbID,
359
                'anidbid' => $aniDbID,
360
                'movieinfo_id' => $movieInfoId !== null ? $movieInfoId->id : $movieInfoId,
361
            ]
362
        );
363
364
        if (config('nntmux.elasticsearch_enabled') === true) {
365
            $data = [
366
                'body' => [
367
                    'doc' => [
368
                        'id' => $ID,
369
                        'name' => $name,
370
                        'searchname' => $searchName,
371
                        'fromname' => $fromName,
372
                    ],
373
                ],
374
375
                'index' => 'releases',
376
                'id' => $ID,
377
            ];
378
379
            Elasticsearch::update($data);
380
        } else {
381
            (new SphinxSearch())->updateRelease($ID);
382
        }
383
        if (! empty($tags)) {
384
            $newTags = explode(',', $tags);
385
            self::find($ID)->retag($newTags);
386
        }
387
    }
388
389
    /**
390
     * @param string $guid
391
     * @throws \Exception
392
     */
393
    public static function updateGrab($guid): void
394
    {
395
        $updateGrabs = ((int) Settings::settingValue('..grabstatus') !== 0);
396
        if ($updateGrabs) {
397
            self::whereGuid($guid)->increment('grabs');
398
        }
399
    }
400
401
    /**
402
     * @param $id
403
     * @return \Illuminate\Database\Eloquent\Model|null|static
404
     */
405
    public static function getCatByRelId($id)
406
    {
407
        return self::whereId($id)->first(['categories_id']);
408
    }
409
410
    /**
411
     * @param $videoId
412
     * @return int
413
     */
414
    public static function removeVideoIdFromReleases($videoId): int
415
    {
416
        return self::whereVideosId($videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]);
417
    }
418
419
    /**
420
     * @param $anidbID
421
     * @return int
422
     */
423
    public static function removeAnidbIdFromReleases($anidbID): int
424
    {
425
        return self::whereAnidbid($anidbID)->update(['anidbid' => -1]);
426
    }
427
428
    /**
429
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
430
     */
431
    public static function getTopDownloads()
432
    {
433
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
434
        $releases = Cache::get(md5('topDownloads'));
435
        if ($releases !== null) {
436
            return $releases;
437
        }
438
        $releases = self::query()
439
            ->where('grabs', '>', 0)
440
            ->select(['id', 'searchname', 'guid', 'adddate'])
441
            ->selectRaw('SUM(grabs) as grabs')
442
            ->groupBy('id', 'searchname', 'adddate')
443
            ->havingRaw('SUM(grabs) > 0')
444
            ->orderBy('grabs', 'desc')
445
            ->limit(10)
446
            ->get();
447
448
        Cache::put(md5('topDownloads'), $releases, $expiresAt);
449
450
        return $releases;
451
    }
452
453
    /**
454
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
455
     */
456
    public static function getTopComments()
457
    {
458
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
459
        $releases = Cache::get(md5('topComments'));
460
        if ($releases !== null) {
461
            return $releases;
462
        }
463
        $releases = self::query()
464
            ->where('comments', '>', 0)
465
            ->select(['id', 'guid', 'searchname'])
466
            ->selectRaw('SUM(comments) AS comments')
467
            ->groupBy('id', 'searchname', 'adddate')
468
            ->havingRaw('SUM(comments) > 0')
469
            ->orderBy('comments', 'desc')
470
            ->limit(10)
471
            ->get();
472
473
        Cache::put(md5('topComments'), $releases, $expiresAt);
474
475
        return $releases;
476
    }
477
478
    /**
479
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|mixed
480
     */
481
    public static function getReleases()
482
    {
483
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
484
        $releases = Cache::get(md5('releases'));
485
        if ($releases !== null) {
486
            return $releases;
487
        }
488
489
        $releases = self::query()
490
491
            ->where('nzbstatus', '=', NZB::NZB_ADDED)
492
            ->select(['releases.*', 'g.name as group_name', 'c.title as category_name'])
493
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
494
            ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
495
            ->get();
496
497
        Cache::put(md5('releases'), $releases, $expiresAt);
498
499
        return $releases;
500
    }
501
502
    /**
503
     * Used for admin page release-list.
504
     *
505
     *
506
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
507
     */
508
    public static function getReleasesRange()
509
    {
510
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_long'));
511
        $releases = Cache::get(md5('releasesRange'));
512
        if ($releases !== null) {
513
            return $releases;
514
        }
515
516
        $releases = self::query()
517
           ->where('nzbstatus', '=', NZB::NZB_ADDED)
518
           ->select(
519
                [
520
                    'releases.id',
521
                    'releases.name',
522
                    'releases.searchname',
523
                    'releases.size',
524
                    'releases.guid',
525
                    'releases.totalpart',
526
                    'releases.postdate',
527
                    'releases.adddate',
528
                    'releases.grabs',
529
                    'cp.title as parent_category',
530
                    'c.title as sub_category',
531
                    DB::raw('CONCAT(cp.title, ' > ', c.title) AS category_name'),
532
                ]
533
            )
534
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
535
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
536
            ->orderByDesc('releases.postdate')
537
            ->paginate(config('nntmux.items_per_page'));
538
539
        Cache::put(md5('releasesRange'), $releases, $expiresAt);
540
541
        return $releases;
542
    }
543
544
    /**
545
     * @param $guid
546
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null|static|static[]
547
     */
548
    public static function getByGuid($guid)
549
    {
550
        $sql = self::query()->with('tagged')
551
            ->select(
552
                [
553
                    'releases.*',
554
                    'g.name as group_name',
555
                    'v.title as showtitle',
556
                    'v.tvdb',
557
                    'v.trakt',
558
                    'v.tvrage',
559
                    'v.tvmaze',
560
                    'v.source',
561
                    'tvi.summary',
562
                    'tvi.image',
563
                    'tve.title',
564
                    'tve.firstaired',
565
                    'tve.se_complete',
566
                    'cp.title as parent_category',
567
                    'c.title as sub_category',
568
                    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"),
569
                ]
570
            )
571
            ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
572
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
573
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
574
            ->leftJoin('videos as v', 'v.id', '=', 'releases.videos_id')
575
            ->leftJoin('tv_info as tvi', 'tvi.videos_id', '=', 'releases.videos_id')
576
            ->leftJoin('tv_episodes as tve', 'tve.id', '=', 'releases.tv_episodes_id')
577
            ->leftJoin('releases_groups as rg', 'rg.releases_id', '=', 'releases.id')
578
            ->leftJoin('usenet_groups as g2', 'rg.groups_id', '=', 'g2.id');
579
580
        if (\is_array($guid)) {
581
            $tempGuids = [];
582
            foreach ($guid as $identifier) {
583
                $tempGuids[] = $identifier;
584
            }
585
            $sql->whereIn('releases.guid', $tempGuids);
586
        } else {
587
            $sql->where('releases.guid', $guid);
588
        }
589
        $sql->groupBy('releases.id');
590
591
        return \is_array($guid) ? $sql->get() : $sql->first();
592
    }
593
594
    /**
595
     * Get a range of releases. used in admin manage list.
596
     *
597
     *
598
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
599
     */
600
    public static function getFailedRange()
601
    {
602
        $failedList = self::query()
603
            ->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")])
604
            ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id')
605
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
606
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
607
            ->orderBy('postdate', 'desc');
608
609
        return $failedList->paginate(config('nntmux.items_per_page'));
610
    }
611
612
    /**
613
     * Retrieve alternate release with same or similar searchname.
614
     *
615
     *
616
     * @param string $guid
617
     * @param int $userid
618
     * @return bool|\Illuminate\Database\Eloquent\Model|null|static
619
     */
620
    public static function getAlternate($guid, $userid)
621
    {
622
        $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']);
623
624
        if ($rel === null) {
625
            return false;
626
        }
627
        DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]);
628
629
        preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', $rel['searchname'], $similar);
0 ignored issues
show
Bug introduced by
It seems like $rel['searchname'] can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

629
        preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', /** @scrutinizer ignore-type */ $rel['searchname'], $similar);
Loading history...
630
631
        if (! empty($similar)) {
632
            $alternate = self::query()->leftJoin('dnzb_failures as df', 'df.release_id', '=', 'releases.id')->where('releases.searchname', 'like', $rel['searchname'])->orWhere('releases.searchname', 'like', $similar[1].'%')->where('df.release_id', '=', null)->where('releases.categories_id', $rel['categories_id'])->where('id', '<>', $rel['id'])->orderBy('releases.postdate', 'desc')->first(['guid']);
633
634
            return $alternate;
635
        }
636
637
        return false;
638
    }
639
640
    /**
641
     * @param $guid
642
     * @return bool
643
     */
644
    public static function checkGuidForApi($guid): bool
645
    {
646
        $check = self::whereGuid($guid)->first();
647
648
        return $check !== null;
649
    }
650
}
651