Video   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 42
dl 0
loc 114
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getByVideoID() 0 7 1
A tvInfo() 0 3 1
A release() 0 3 1
A episode() 0 3 1
A getCount() 0 9 2
A alias() 0 3 1
A getSeriesList() 0 29 5
A getRange() 0 11 2
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Database\Eloquent\Relations\HasOne;
9
10
/**
11
 * App\Models\Video.
12
 *
13
 * @property int $id Show ID to be used in other tables as reference
14
 * @property bool $type 0 = TV, 1 = Film, 2 = Anime
15
 * @property string $title Name of the video.
16
 * @property string $countries_id Two character country code (FK to countries table).
17
 * @property string $started Date (UTC) of production's first airing.
18
 * @property int $anidb ID number for anidb site
19
 * @property int $imdb ID number for IMDB site (without the 'tt' prefix).
20
 * @property int $tmdb ID number for TMDB site.
21
 * @property int $trakt ID number for TraktTV site.
22
 * @property int $tvdb ID number for TVDB site
23
 * @property int $tvmaze ID number for TVMaze site.
24
 * @property int $tvrage ID number for TVRage site.
25
 * @property bool $source Which site did we use for info?
26
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\VideoAlias[] $alias
27
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TvEpisode[] $episode
28
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $release
29
 *
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereAnidb($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereCountriesId($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereImdb($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereSource($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereStarted($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTitle($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTmdb($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTrakt($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvdb($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvmaze($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvrage($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereType($value)
43
 *
44
 * @mixin \Eloquent
45
 *
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video newModelQuery()
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video newQuery()
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video query()
49
 */
50
class Video extends Model
51
{
52
    /**
53
     * @var bool
54
     */
55
    protected $dateFormat = false;
56
57
    /**
58
     * @var array
59
     */
60
    protected $guarded = [];
61
62
    /**
63
     * @var bool
64
     */
65
    public $timestamps = false;
66
67
    public function alias(): HasMany
68
    {
69
        return $this->hasMany(VideoAlias::class, 'videos_id');
70
    }
71
72
    public function release(): HasMany
73
    {
74
        return $this->hasMany(Release::class, 'videos_id');
75
    }
76
77
    public function episode(): HasMany
78
    {
79
        return $this->hasMany(TvEpisode::class, 'videos_id');
80
    }
81
82
    public function tvInfo(): HasOne
83
    {
84
        return $this->hasOne(TvInfo::class, 'videos_id');
85
    }
86
87
    /**
88
     * Get info from tables for the provided ID.
89
     *
90
     *
91
     * @return Model|null|static
92
     */
93
    public static function getByVideoID($id)
94
    {
95
        return self::query()
96
            ->select(['videos.*', 'tv_info.summary', 'tv_info.publisher', 'tv_info.image'])
97
            ->where('videos.id', $id)
98
            ->join('tv_info', 'videos.id', '=', 'tv_info.videos_id')
99
            ->first();
100
    }
101
102
    /**
103
     * Retrieves a range of all shows for the show-edit admin list.
104
     */
105
    public static function getRange(string $showname = ''): LengthAwarePaginator
106
    {
107
        $sql = self::query()
108
            ->select(['videos.*', 'tv_info.summary', 'tv_info.publisher', 'tv_info.image'])
109
            ->join('tv_info', 'videos.id', '=', 'tv_info.videos_id');
110
111
        if ($showname !== '') {
112
            $sql->where('videos.title', 'like', '%'.$showname.'%');
113
        }
114
115
        return $sql->paginate(config('nntmux.items_per_page'));
116
    }
117
118
    /**
119
     * Returns a count of all shows -- usually used by pager.
120
     */
121
    public static function getCount(string $showname = ''): int
122
    {
123
        $res = self::query()->join('tv_info', 'videos.id', '=', 'tv_info.videos_id');
124
125
        if ($showname !== '') {
126
            $res->where('videos.title', 'like', '%'.$showname.'%');
127
        }
128
129
        return $res->count('videos.id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res->count('videos.id') could return the type Illuminate\Database\Eloq...\Database\Query\Builder which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
130
    }
131
132
    /**
133
     * Retrieves and returns a list of shows with eligible releases.
134
     */
135
    public static function getSeriesList($uid, string $letter = '', string $showname = ''): array
136
    {
137
        if (($letter !== '') && $letter === '0-9') {
138
            $letter = '[0-9]';
139
        }
140
141
        $qry = self::query()
142
            ->select(['videos.*', 'tve.firstaired as prevdate', 'tve.title as previnfo', 'tvi.publisher', 'us.id as userseriesid'])
143
            ->join('tv_info as tvi', 'videos.id', '=', 'tvi.videos_id')
144
            ->join('tv_episodes as tve', 'videos.id', '=', 'tve.videos_id')
145
            ->leftJoin('user_series as us', function ($join) use ($uid) {
146
                $join->on('videos.id', '=', 'us.videos_id')->where('us.users_id', '=', $uid);
147
            })
148
            ->whereBetween('r.categories_id', [Category::TV_ROOT, Category::TV_OTHER])
149
            ->where('tve.firstaired', '<', now())
150
            ->leftJoin('releases as r', 'r.videos_id', '=', 'videos.id')
151
            ->orderBy('videos.title')
0 ignored issues
show
Bug introduced by
'videos.title' 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

151
            ->orderBy(/** @scrutinizer ignore-type */ 'videos.title')
Loading history...
152
            ->orderByDesc('tve.firstaired')
0 ignored issues
show
Bug introduced by
'tve.firstaired' 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

152
            ->orderByDesc(/** @scrutinizer ignore-type */ 'tve.firstaired')
Loading history...
153
            ->groupBy(['videos.id']);
154
155
        if ($letter !== '') {
156
            $qry->whereRaw('videos.title REGEXP ?', ['^'.$letter]);
157
        }
158
159
        if ($showname !== '') {
160
            $qry->where('videos.title', 'like', '%'.$showname.'%');
161
        }
162
163
        return $qry->get()->toArray();
164
    }
165
}
166