Completed
Push — dev ( dafd8a...7a3759 )
by Darko
08:41
created

Release::updateRelease()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 15
dl 0
loc 28
rs 9.568
c 0
b 0
f 0
ccs 0
cts 13
cp 0
crap 20

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\Support\Facades\DB;
9
use Watson\Rememberable\Rememberable;
10
use Illuminate\Database\Eloquent\Model;
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
 */
130
class Release extends Model
131
{
132
    use Rememberable, Taggable;
0 ignored issues
show
introduced by
The trait Watson\Rememberable\Rememberable requires some properties which are not provided by App\Models\Release: $rememberFor, $rememberCacheTag, $rememberCachePrefix, $rememberCacheDriver
Loading history...
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...
133
134
    /**
135
     * @var bool
136
     */
137
    protected $dateFormat = false;
138
139
    /**
140
     * @var bool
141
     */
142
    public $timestamps = false;
143
144
    /**
145
     * @var array
146
     */
147
    protected $guarded = [];
148
149
    /**
150
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
151
     */
152
    public function group()
153
    {
154
        return $this->belongsTo(UsenetGroup::class, 'groups_id');
155
    }
156
157
    /**
158
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
159
     */
160
    public function download()
161
    {
162
        return $this->hasMany(UserDownload::class, 'releases_id');
163
    }
164
165
    /**
166
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
167
     */
168
    public function userRelease()
169
    {
170
        return $this->hasMany(UsersRelease::class, 'releases_id');
171
    }
172
173
    public function file()
174
    {
175
        return $this->hasMany(ReleaseFile::class, 'releases_id');
176
    }
177
178
    /**
179
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
180
     */
181
    public function category()
182
    {
183
        return $this->belongsTo(Category::class, 'categories_id');
184
    }
185
186
    /**
187
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
188
     */
189
    public function predb()
190
    {
191
        return $this->belongsTo(Predb::class, 'predb_id');
192
    }
193
194
    /**
195
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
196
     */
197
    public function failed()
198
    {
199
        return $this->hasMany(DnzbFailure::class, 'release_id');
200
    }
201
202
    /**
203
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
204
     */
205
    public function releaseExtra()
206
    {
207
        return $this->hasMany(ReleaseExtraFull::class, 'releases_id');
208
    }
209
210
    /**
211
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
212
     */
213
    public function nfo()
214
    {
215
        return $this->hasOne(ReleaseNfo::class, 'releases_id');
216
    }
217
218
    /**
219
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
220
     */
221
    public function comment()
222
    {
223
        return $this->hasMany(ReleaseComment::class, 'releases_id');
224
    }
225
226
    /**
227
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
228
     */
229
    public function releaseGroup()
230
    {
231
        return $this->hasMany(ReleasesGroups::class, 'releases_id');
232
    }
233
234
    /**
235
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
236
     */
237
    public function video()
238
    {
239
        return $this->belongsTo(Video::class, 'videos_id');
240
    }
241
242
    /**
243
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
244
     */
245
    public function episode()
246
    {
247
        return $this->belongsTo(TvEpisode::class, 'tv_episodes_id');
248
    }
249
250
    /**
251
     * Insert a single release returning the ID on success or false on failure.
252
     *
253
     * @param array $parameters Insert parameters, must be escaped if string.
254
     *
255
     * @return bool|int
256
     * @throws \Exception
257
     */
258
    public static function insertRelease(array $parameters = [])
259
    {
260
        $passwordStatus = ((int) Settings::settingValue('..checkpasswordedrar') === 1 ? -1 : 0);
261
        $parameters['id'] = self::query()
262
            ->insertGetId(
263
                [
264
                    'name' => $parameters['name'],
265
                    'searchname' => $parameters['searchname'],
266
                    'totalpart' => $parameters['totalpart'],
267
                    'groups_id' => $parameters['groups_id'],
268
                    'adddate' => now(),
269
                    'guid' => $parameters['guid'],
270
                    'leftguid' => $parameters['guid'][0],
271
                    'postdate' => $parameters['postdate'],
272
                    'fromname' => $parameters['fromname'],
273
                    'size' => $parameters['size'],
274
                    'passwordstatus' => $passwordStatus,
275
                    'haspreview' => -1,
276
                    'categories_id' => $parameters['categories_id'],
277
                    'nfostatus' => -1,
278
                    'nzbstatus' => $parameters['nzbstatus'],
279
                    'isrenamed' => $parameters['isrenamed'],
280
                    'iscategorized' => 1,
281
                    'predb_id' => $parameters['predb_id'],
282
                ]
283
            );
284
285
        (new SphinxSearch())->insertRelease($parameters);
286
287
        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...
288
    }
289
290
    /**
291
     * Used for release edit page on site.
292
     *
293
     * @param int $ID
294
     * @param string $name
295
     * @param string $searchName
296
     * @param string $fromName
297
     * @param int $categoryID
298
     * @param int $parts
299
     * @param int $grabs
300
     * @param int $size
301
     * @param string $postedDate
302
     * @param string $addedDate
303
     * @param        $videoId
304
     * @param        $episodeId
305
     * @param int $imDbID
306
     * @param int $aniDbID
307
     * @param string $tags
308
     * @throws \Exception
309
     */
310
    public static function updateRelease($ID, $name, $searchName, $fromName, $categoryID, $parts, $grabs, $size, $postedDate, $addedDate, $videoId, $episodeId, $imDbID, $aniDbID, string $tags = ''): void
311
    {
312
        $movieInfoId = null;
313
        if (! empty($imDbID)) {
314
            $movieInfoId = MovieInfo::whereImdbid($imDbID)->first(['id']);
315
        }
316
        self::whereId($ID)->update(
317
            [
318
                'name' => $name,
319
                'searchname' => $searchName,
320
                'fromname' => $fromName,
321
                'categories_id' => $categoryID,
322
                'totalpart' => $parts,
323
                'grabs' => $grabs,
324
                'size' => $size,
325
                'postdate' => $postedDate,
326
                'adddate' => $addedDate,
327
                'videos_id' => $videoId,
328
                'tv_episodes_id' => $episodeId,
329
                'imdbid' => $imDbID,
330
                'anidbid' => $aniDbID,
331
                'movieinfo_id' => $movieInfoId !== null ? $movieInfoId->id : $movieInfoId,
332
            ]
333
        );
334
        (new SphinxSearch())->updateRelease($ID);
335
        if (! empty($tags)) {
336
            $newTags = explode(',', $tags);
337
            self::find($ID)->retag($newTags);
338
        }
339
    }
340
341
    /**
342
     * @param string $guid
343
     * @throws \Exception
344
     */
345
    public static function updateGrab($guid): void
346
    {
347
        $updateGrabs = ((int) Settings::settingValue('..grabstatus') !== 0);
348
        if ($updateGrabs) {
349
            self::whereGuid($guid)->increment('grabs');
350
        }
351
    }
352
353
    /**
354
     * @param $id
355
     * @return \Illuminate\Database\Eloquent\Model|null|static
356
     */
357
    public static function getCatByRelId($id)
358
    {
359
        return self::whereId($id)->first(['categories_id']);
360
    }
361
362
    /**
363
     * @param $videoId
364
     * @return int
365
     */
366
    public static function removeVideoIdFromReleases($videoId): int
367
    {
368
        return self::whereVideosId($videoId)->update(['videos_id' => 0, 'tv_episodes_id' => 0]);
369
    }
370
371
    /**
372
     * @param $anidbID
373
     * @return int
374
     */
375
    public static function removeAnidbIdFromReleases($anidbID): int
376
    {
377
        return self::whereAnidbid($anidbID)->update(['anidbid' => -1]);
378
    }
379
380
    /**
381
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
382
     */
383
    public static function getTopDownloads()
384
    {
385
        return self::query()
386
            ->remember(config('nntmux.cache_expiry_long'))
387
            ->where('grabs', '>', 0)
388
            ->select(['id', 'searchname', 'guid', 'adddate'])
389
            ->selectRaw('SUM(grabs) as grabs')
390
            ->groupBy('id', 'searchname', 'adddate')
391
            ->havingRaw('SUM(grabs) > 0')
392
            ->orderBy('grabs', 'desc')
393
            ->limit(10)
394
            ->get();
395
    }
396
397
    /**
398
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[]
399
     */
400
    public static function getTopComments()
401
    {
402
        return self::query()
403
            ->remember(config('nntmux.cache_expiry_long'))
404
            ->where('comments', '>', 0)
405
            ->select(['id', 'guid', 'searchname'])
406
            ->selectRaw('SUM(comments) AS comments')
407
            ->groupBy('id', 'searchname', 'adddate')
408
            ->havingRaw('SUM(comments) > 0')
409
            ->orderBy('comments', 'desc')
410
            ->limit(10)
411
            ->get();
412
    }
413
414
    /**
415
     * @return array
416
     */
417
    public static function getReleases(): array
418
    {
419
        return self::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::query()->re...ases.groups_id')->get() could return the type Illuminate\Database\Eloquent\Collection which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
420
            ->remember(config('nntmux.cache_expiry_long'))
421
            ->where('nzbstatus', '=', NZB::NZB_ADDED)
422
            ->select(['releases.*', 'g.name as group_name', 'c.title as category_name'])
423
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
424
            ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
425
            ->get();
426
    }
427
428
    /**
429
     * Used for admin page release-list.
430
     *
431
     *
432
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
433
     */
434
    public static function getReleasesRange()
435
    {
436
        return self::query()
437
           ->remember(config('nntmux.cache_expiry_medium'))
438
           ->where('nzbstatus', '=', NZB::NZB_ADDED)
439
           ->select(
440
                [
441
                    'releases.id',
442
                    'releases.name',
443
                    'releases.searchname',
444
                    'releases.size',
445
                    'releases.guid',
446
                    'releases.totalpart',
447
                    'releases.postdate',
448
                    'releases.adddate',
449
                    'releases.grabs',
450
                    'cp.title as parent_category',
451
                    'c.title as sub_category',
452
                    DB::raw('CONCAT(cp.title, ' > ', c.title) AS category_name'),
453
                ]
454
            )
455
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
456
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
457
            ->orderByDesc('releases.postdate')
458
            ->paginate(config('nntmux.items_per_page'));
459
    }
460
461
    /**
462
     * @param $guid
463
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null|static|static[]
464
     */
465
    public static function getByGuid($guid)
466
    {
467
        $sql = self::query()->with('tagged')
468
            ->select(
469
                [
470
                    'releases.*',
471
                    'g.name as group_name',
472
                    'v.title as showtitle',
473
                    'v.tvdb',
474
                    'v.trakt',
475
                    'v.tvrage',
476
                    'v.tvmaze',
477
                    'v.source',
478
                    'tvi.summary',
479
                    'tvi.image',
480
                    'tve.title',
481
                    'tve.firstaired',
482
                    'tve.se_complete',
483
                    'cp.title as parent_category',
484
                    'c.title as sub_category',
485
                    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"),
486
                ]
487
            )
488
            ->leftJoin('usenet_groups as g', 'g.id', '=', 'releases.groups_id')
489
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
490
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
491
            ->leftJoin('videos as v', 'v.id', '=', 'releases.videos_id')
492
            ->leftJoin('tv_info as tvi', 'tvi.videos_id', '=', 'releases.videos_id')
493
            ->leftJoin('tv_episodes as tve', 'tve.id', '=', 'releases.tv_episodes_id')
494
            ->leftJoin('releases_groups as rg', 'rg.releases_id', '=', 'releases.id')
495
            ->leftJoin('usenet_groups as g2', 'rg.groups_id', '=', 'g2.id');
496
497
        if (\is_array($guid)) {
498
            $tempGuids = [];
499
            foreach ($guid as $identifier) {
500
                $tempGuids[] = $identifier;
501
            }
502
            $sql->whereIn('releases.guid', $tempGuids);
503
        } else {
504
            $sql->where('releases.guid', $guid);
505
        }
506
        $sql->groupBy('releases.id');
507
508
        return \is_array($guid) ? $sql->get() : $sql->first();
509
    }
510
511
    /**
512
     * Get a range of releases. used in admin manage list.
513
     *
514
     *
515
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
516
     */
517
    public static function getFailedRange()
518
    {
519
        $failedList = self::query()
520
            ->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")])
521
            ->rightJoin('dnzb_failures', 'dnzb_failures.release_id', '=', 'releases.id')
522
            ->leftJoin('categories as c', 'c.id', '=', 'releases.categories_id')
523
            ->leftJoin('root_categories as cp', 'cp.id', '=', 'c.root_categories_id')
524
            ->orderBy('postdate', 'desc');
525
526
        return $failedList->paginate(config('nntmux.items_per_page'));
527
    }
528
529
    /**
530
     * Retrieve alternate release with same or similar searchname.
531
     *
532
     *
533
     * @param string $guid
534
     * @param int $userid
535
     * @return bool|\Illuminate\Database\Eloquent\Model|null|static
536
     */
537
    public static function getAlternate($guid, $userid)
538
    {
539
        $rel = self::whereGuid($guid)->first(['id', 'searchname', 'categories_id']);
540
541
        if ($rel === null) {
542
            return false;
543
        }
544
        DnzbFailure::insertOrIgnore(['release_id' => $rel['id'], 'users_id' => $userid, 'failed' => 1]);
545
546
        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

546
        preg_match('/(^\w+[-_. ].+?\.(\d+p)).+/i', /** @scrutinizer ignore-type */ $rel['searchname'], $similar);
Loading history...
547
548
        if (! empty($similar)) {
549
            $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']);
550
551
            return $alternate;
552
        }
553
554
        return false;
555
    }
556
557
    /**
558
     * @param $guid
559
     * @return bool
560
     */
561
    public static function checkGuidForApi($guid): bool
562
    {
563
        $check = self::whereGuid($guid)->first();
564
565
        return $check !== null;
566
    }
567
}
568