Passed
Push — master ( 04c44e...059b28 )
by Darko
10:01 queued 04:00
created

RSS::getShowsRss()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 41
rs 9.0648
c 0
b 0
f 0
cc 5
nc 1
nop 4
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use App\Models\UserMovie;
8
use App\Models\UserSerie;
9
use Blacklight\NZB;
10
use Blacklight\Releases;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Facades\Cache;
13
use Illuminate\Support\Facades\DB;
14
15
/**
16
 * Class RSS -- contains specific functions for RSS.
17
 */
18
class RSS extends ApiController
19
{
20
    public Releases $releases;
21
22
    /**
23
     * @throws \Exception
24
     */
25
    public function __construct()
26
    {
27
        parent::__construct();
28
        $this->releases = new Releases;
29
    }
30
31
    public function getRss($cat, $videosId, $aniDbID, int $userID = 0, int $airDate = -1, int $limit = 100, int $offset = 0): mixed
32
    {
33
        $catSearch = $cartSearch = '';
34
        $catLimit = 'AND r.categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER;
35
        if (\count($cat)) {
36
            if ((int) $cat[0] === -2) {
37
                $cartSearch = sprintf(
38
                    'INNER JOIN users_releases ON users_releases.users_id = %d AND users_releases.releases_id = r.id',
39
                    $userID
40
                );
41
            } elseif ((int) $cat[0] !== -1) {
42
                $catSearch = Category::getCategorySearch($cat);
43
            }
44
        }
45
        $sql =
46
            sprintf(
47
                "SELECT r.searchname, r.guid, r.postdate, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id,
48
					m.cover, m.imdbid, m.rating, m.plot, m.year, m.genre, m.director, m.actors,
49
					g.name AS group_name,
50
					CONCAT(cp.title, ' > ', c.title) AS category_name,
51
					COALESCE(cp.id,0) AS parentid,
52
					mu.title AS mu_title, mu.url AS mu_url, mu.artist AS mu_artist,
53
					mu.publisher AS mu_publisher, mu.releasedate AS mu_releasedate,
54
					mu.review AS mu_review, mu.tracks AS mu_tracks, mu.cover AS mu_cover,
55
					mug.title AS mu_genre, co.title AS co_title, co.url AS co_url,
56
					co.publisher AS co_publisher, co.releasedate AS co_releasedate,
57
					co.review AS co_review, co.cover AS co_cover, cog.title AS co_genre,
58
					bo.cover AS bo_cover
59
				FROM releases r
60
				LEFT JOIN categories c ON c.id = r.categories_id
61
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
62
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
63
				LEFT OUTER JOIN musicinfo mu ON mu.id = r.musicinfo_id
64
				LEFT OUTER JOIN genres mug ON mug.id = mu.genres_id
65
				LEFT OUTER JOIN consoleinfo co ON co.id = r.consoleinfo_id
66
				LEFT JOIN movieinfo m ON m.id = r.movieinfo_id
67
				LEFT OUTER JOIN genres cog ON cog.id = co.genres_id %s
68
				LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id
69
				LEFT OUTER JOIN bookinfo bo ON bo.id = r.bookinfo_id
70
				WHERE r.passwordstatus %s
71
				AND r.nzbstatus = %d
72
				%s %s %s %s
73
				ORDER BY postdate DESC %s",
74
                $cartSearch,
75
                $this->releases->showPasswords(),
76
                NZB::NZB_ADDED,
77
                $catSearch,
78
                ($videosId > 0 ? sprintf('AND r.videos_id = %d %s', $videosId, ($catSearch === '' ? $catLimit : '')) : ''),
79
                ($aniDbID > 0 ? sprintf('AND r.anidbid = %d %s', $aniDbID, ($catSearch === '' ? $catLimit : '')) : ''),
80
                ($airDate > -1 ? sprintf('AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY)', $airDate) : ''),
81
                $limit === -1 ? '' : ' LIMIT '.$limit.' OFFSET '.$offset
82
            );
83
84
        return Cache::flexible(md5($sql), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql) {
85
            return Release::fromQuery($sql);
86
        });
87
    }
88
89
    public function getShowsRss(int $limit, int $userID = 0, array $excludedCats = [], int $airDate = -1): mixed
90
    {
91
        $sql = sprintf(
92
            "
93
				SELECT r.searchname, r.guid, r.postdate, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id, v.id, v.title, g.name AS group_name,
94
					CONCAT(cp.title, '-', c.title) AS category_name,
95
					COALESCE(cp.id,0) AS parentid
96
				FROM releases r
97
				LEFT JOIN categories c ON c.id = r.categories_id
98
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
99
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
100
				LEFT OUTER JOIN videos v ON v.id = r.videos_id
101
				LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id
102
				WHERE %s %s %s
103
				AND r.nzbstatus = %d
104
				AND r.categories_id BETWEEN %d AND %d
105
				AND r.passwordstatus %s
106
				ORDER BY postdate DESC %s",
107
            $this->releases->uSQL(
108
                UserSerie::fromQuery(
0 ignored issues
show
Bug introduced by
It seems like App\Models\UserSerie::fr...ers_id = %d', $userID)) can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $userQuery of Blacklight\Releases::uSQL() does only seem to accept Illuminate\Database\Eloquent\Collection|array, 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

108
                /** @scrutinizer ignore-type */ UserSerie::fromQuery(
Loading history...
109
                    sprintf(
110
                        '
111
							SELECT videos_id, categories
112
							FROM user_series
113
							WHERE users_id = %d',
114
                        $userID
115
                    )
116
                ),
117
                'videos_id'
118
            ),
119
            (\count($excludedCats) ? 'AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
120
            ($airDate > -1 ? sprintf('AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY) ', $airDate) : ''),
121
            NZB::NZB_ADDED,
122
            Category::TV_ROOT,
123
            Category::TV_OTHER,
124
            $this->releases->showPasswords(),
125
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', $limit > 100 ? 100 : $limit) : ''
126
        );
127
128
        return Cache::flexible(md5($sql), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql) {
129
            return Release::fromQuery($sql);
130
        });
131
    }
132
133
    public function getMyMoviesRss(int $limit, int $userID = 0, array $excludedCats = []): mixed
134
    {
135
        $sql = sprintf(
136
            "
137
				SELECT r.searchname, r.guid, r.postdate, r.categories_id, r.size, r.totalpart, r.fromname, r.passwordstatus, r.grabs, r.comments, r.adddate, r.videos_id, r.tv_episodes_id, mi.title AS releasetitle, g.name AS group_name,
138
					CONCAT(cp.title, '-', c.title) AS category_name,
139
					COALESCE(cp.id,0) AS parentid
140
				FROM releases r
141
				LEFT JOIN categories c ON c.id = r.categories_id
142
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
143
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
144
				LEFT JOIN movieinfo mi ON mi.id = r.movieinfo_id
145
				WHERE %s %s
146
				AND r.nzbstatus = %d
147
				AND r.categories_id BETWEEN %d AND %d
148
				AND r.passwordstatus %s
149
				ORDER BY postdate DESC %s",
150
            $this->releases->uSQL(
151
                UserMovie::fromQuery(
0 ignored issues
show
Bug introduced by
It seems like App\Models\UserMovie::fr...ers_id = %d', $userID)) can also be of type Illuminate\Database\Eloq...gHasThroughRelationship; however, parameter $userQuery of Blacklight\Releases::uSQL() does only seem to accept Illuminate\Database\Eloquent\Collection|array, 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

151
                /** @scrutinizer ignore-type */ UserMovie::fromQuery(
Loading history...
152
                    sprintf(
153
                        '
154
							SELECT imdbid, categories
155
							FROM user_movies
156
							WHERE users_id = %d',
157
                        $userID
158
                    )
159
                ),
160
                'imdbid'
161
            ),
162
            (\count($excludedCats) > 0 ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
163
            NZB::NZB_ADDED,
164
            Category::MOVIE_ROOT,
165
            Category::MOVIE_OTHER,
166
            $this->releases->showPasswords(),
167
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', $limit > 100 ? 100 : $limit) : ''
168
        );
169
170
        return Cache::flexible(md5($sql), [config('nntmux.cache_expiry_medium'), config('nntmux.cache_expiry_long')], function () use ($sql) {
171
            return Release::fromQuery($sql);
172
        });
173
    }
174
175
    /**
176
     * @return Model|\Illuminate\Database\Query\Builder|null
177
     */
178
    public function getFirstInstance($column, $table, $order)
179
    {
180
        return DB::table($table)
181
            ->select([$column])
182
            ->where($column, '>', 0)
183
            ->orderBy($order)
184
            ->first();
185
    }
186
}
187