Passed
Push — dev ( 9d591d...c59ba0 )
by Darko
22:01
created

Video::episode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
/**
8
 * App\Models\Video.
9
 *
10
 * @property int $id Show ID to be used in other tables as reference
11
 * @property bool $type 0 = TV, 1 = Film, 2 = Anime
12
 * @property string $title Name of the video.
13
 * @property string $countries_id Two character country code (FK to countries table).
14
 * @property string $started Date (UTC) of production's first airing.
15
 * @property int $anidb ID number for anidb site
16
 * @property int $imdb ID number for IMDB site (without the 'tt' prefix).
17
 * @property int $tmdb ID number for TMDB site.
18
 * @property int $trakt ID number for TraktTV site.
19
 * @property int $tvdb ID number for TVDB site
20
 * @property int $tvmaze ID number for TVMaze site.
21
 * @property int $tvrage ID number for TVRage site.
22
 * @property bool $source Which site did we use for info?
23
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\VideoAlias[] $alias
24
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TvEpisode[] $episode
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Release[] $release
26
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereAnidb($value)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereCountriesId($value)
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereId($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereImdb($value)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereSource($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereStarted($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTitle($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTmdb($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTrakt($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvdb($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvmaze($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereTvrage($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video whereType($value)
39
 * @mixin \Eloquent
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video newModelQuery()
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video newQuery()
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Video query()
43
 */
44
class Video extends Model
45
{
46
    /**
47
     * @var bool
48
     */
49
    protected $dateFormat = false;
50
51
    /**
52
     * @var array
53
     */
54
    protected $guarded = [];
55
56
    /**
57
     * @var bool
58
     */
59
    public $timestamps = false;
60
61
    /**
62
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
63
     */
64
    public function alias()
65
    {
66
        return $this->hasMany(VideoAlias::class, 'videos_id');
67
    }
68
69
    /**
70
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
71
     */
72
    public function release()
73
    {
74
        return $this->hasMany(Release::class, 'videos_id');
75
    }
76
77
    /**
78
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
79
     */
80
    public function episode()
81
    {
82
        return $this->hasMany(TvEpisode::class, 'videos_id');
83
    }
84
85
    /**
86
     * Get info from tables for the provided ID.
87
     *
88
     *
89
     * @param $id
90
     *
91
     * @return \Illuminate\Database\Eloquent\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
     *
106
     * @param string $showname
107
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
108
     */
109
    public static function getRange($showname = '')
110
    {
111
        $sql = self::query()
112
            ->select(['videos.*', 'tv_info.summary', 'tv_info.publisher', 'tv_info.image'])
113
            ->join('tv_info', 'videos.id', '=', 'tv_info.videos_id');
114
115
        if ($showname !== '') {
116
            $sql->where('videos.title', 'like', '%'.$showname.'%');
117
        }
118
119
        return $sql->paginate(config('nntmux.items_per_page'));
120
    }
121
122
    /**
123
     * Returns a count of all shows -- usually used by pager.
124
     *
125
     *
126
     * @param string $showname
127
     * @return int
128
     */
129
    public static function getCount($showname = ''): int
130
    {
131
        $res = self::query()->join('tv_info', 'videos.id', '=', 'tv_info.videos_id');
132
133
        if ($showname !== '') {
134
            $res->where('videos.title', 'like', '%'.$showname.'%');
135
        }
136
137
        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...
138
    }
139
140
    /**
141
     * Retrieves and returns a list of shows with eligible releases.
142
     *
143
     * @param        $uid
144
     * @param string $letter
145
     * @param string $showname
146
     *
147
     * @return array
148
     */
149
    public static function getSeriesList($uid, $letter = '', $showname = ''): array
150
    {
151
        if (($letter !== '') && $letter === '0-9') {
152
            $letter = '[0-9]';
153
        }
154
155
        $qry = self::query()
156
            ->select(['videos.*', 'tve.firstaired as prevdate', 'tve.title as previnfo', 'tvi.publisher', 'us.id as userseriesid'])
157
            ->join('tv_info as tvi', 'videos.id', '=', 'tvi.videos_id')
158
            ->join('tv_episodes as tve', 'videos.id', '=', 'tve.videos_id')
159
            ->leftJoin('user_series as us', function ($join) use ($uid) {
160
                $join->on('videos.id', '=', 'us.videos_id')->where('us.users_id', '=', $uid);
161
            })
162
            ->whereBetween('r.categories_id', [Category::TV_ROOT, Category::TV_OTHER])
163
            ->where('tve.firstaired', '<', now())
164
            ->leftJoin('releases as r', 'r.videos_id', '=', 'videos.id')
165
            ->orderBy('videos.title')
166
            ->orderByDesc('tve.firstaired')
167
            ->groupBy(['videos.id']);
168
169
        if ($letter !== '') {
170
            $qry->whereRaw('videos.title REGEXP ?', ['^'.$letter]);
171
        }
172
173
        if ($showname !== '') {
174
            $qry->where('videos.title', 'like', '%'.$showname.'%');
175
        }
176
177
        return $qry->get()->toArray();
178
    }
179
}
180