Passed
Push — master ( 5d5926...04c44e )
by Darko
16:19
created

RSS::getRss()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 63
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 63
rs 6.8787
c 0
b 0
f 0
cc 11
nc 8
nop 7

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Builder;
12
use Illuminate\Database\Eloquent\Collection;
13
use Illuminate\Database\Eloquent\Model;
14
use Illuminate\Support\Facades\Cache;
15
use Illuminate\Support\Facades\DB;
16
17
/**
18
 * Class RSS -- contains specific functions for RSS.
19
 */
20
class RSS extends ApiController
21
{
22
    public Releases $releases;
23
24
    /**
25
     * @throws \Exception
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $this->releases = new Releases;
31
    }
32
33
    /**
34
     * @return Release[]|Collection|mixed
35
     */
36
    public function getRss($cat, $videosId, $aniDbID, int $userID = 0, int $airDate = -1, int $limit = 100, int $offset = 0)
37
    {
38
        $catSearch = $cartSearch = '';
39
        $catLimit = 'AND r.categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER;
40
        if (\count($cat)) {
41
            if ((int) $cat[0] === -2) {
42
                $cartSearch = sprintf(
43
                    'INNER JOIN users_releases ON users_releases.users_id = %d AND users_releases.releases_id = r.id',
44
                    $userID
45
                );
46
            } elseif ((int) $cat[0] !== -1) {
47
                $catSearch = Category::getCategorySearch($cat);
48
            }
49
        }
50
        $sql =
51
            sprintf(
52
                "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,
53
					m.cover, m.imdbid, m.rating, m.plot, m.year, m.genre, m.director, m.actors,
54
					g.name AS group_name,
55
					CONCAT(cp.title, ' > ', c.title) AS category_name,
56
					COALESCE(cp.id,0) AS parentid,
57
					mu.title AS mu_title, mu.url AS mu_url, mu.artist AS mu_artist,
58
					mu.publisher AS mu_publisher, mu.releasedate AS mu_releasedate,
59
					mu.review AS mu_review, mu.tracks AS mu_tracks, mu.cover AS mu_cover,
60
					mug.title AS mu_genre, co.title AS co_title, co.url AS co_url,
61
					co.publisher AS co_publisher, co.releasedate AS co_releasedate,
62
					co.review AS co_review, co.cover AS co_cover, cog.title AS co_genre,
63
					bo.cover AS bo_cover
64
				FROM releases r
65
				LEFT JOIN categories c ON c.id = r.categories_id
66
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
67
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
68
				LEFT OUTER JOIN musicinfo mu ON mu.id = r.musicinfo_id
69
				LEFT OUTER JOIN genres mug ON mug.id = mu.genres_id
70
				LEFT OUTER JOIN consoleinfo co ON co.id = r.consoleinfo_id
71
				LEFT JOIN movieinfo m ON m.id = r.movieinfo_id
72
				LEFT OUTER JOIN genres cog ON cog.id = co.genres_id %s
73
				LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id
74
				LEFT OUTER JOIN bookinfo bo ON bo.id = r.bookinfo_id
75
				WHERE r.passwordstatus %s
76
				AND r.nzbstatus = %d
77
				%s %s %s %s
78
				ORDER BY postdate DESC %s",
79
                $cartSearch,
80
                $this->releases->showPasswords(),
81
                NZB::NZB_ADDED,
82
                $catSearch,
83
                ($videosId > 0 ? sprintf('AND r.videos_id = %d %s', $videosId, ($catSearch === '' ? $catLimit : '')) : ''),
84
                ($aniDbID > 0 ? sprintf('AND r.anidbid = %d %s', $aniDbID, ($catSearch === '' ? $catLimit : '')) : ''),
85
                ($airDate > -1 ? sprintf('AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY)', $airDate) : ''),
86
                $limit === -1 ? '' : ' LIMIT '.$limit.' OFFSET '.$offset
87
            );
88
89
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
90
        $result = Cache::get(md5($sql));
91
        if ($result !== null) {
92
            return $result;
93
        }
94
95
        $result = Release::fromQuery($sql);
96
        Cache::put(md5($sql), $result, $expiresAt);
97
98
        return $result;
99
    }
100
101
    /**
102
     * @return Builder|Collection
103
     */
104
    public function getShowsRss(int $limit, int $userID = 0, array $excludedCats = [], int $airDate = -1)
105
    {
106
        $sql = sprintf(
107
            "
108
				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,
109
					CONCAT(cp.title, '-', c.title) AS category_name,
110
					COALESCE(cp.id,0) AS parentid
111
				FROM releases r
112
				LEFT JOIN categories c ON c.id = r.categories_id
113
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
114
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
115
				LEFT OUTER JOIN videos v ON v.id = r.videos_id
116
				LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id
117
				WHERE %s %s %s
118
				AND r.nzbstatus = %d
119
				AND r.categories_id BETWEEN %d AND %d
120
				AND r.passwordstatus %s
121
				ORDER BY postdate DESC %s",
122
            $this->releases->uSQL(
123
                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

123
                /** @scrutinizer ignore-type */ UserSerie::fromQuery(
Loading history...
124
                    sprintf(
125
                        '
126
							SELECT videos_id, categories
127
							FROM user_series
128
							WHERE users_id = %d',
129
                        $userID
130
                    )
131
                ),
132
                'videos_id'
133
            ),
134
            (\count($excludedCats) ? 'AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
135
            ($airDate > -1 ? sprintf('AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY) ', $airDate) : ''),
136
            NZB::NZB_ADDED,
137
            Category::TV_ROOT,
138
            Category::TV_OTHER,
139
            $this->releases->showPasswords(),
140
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', $limit > 100 ? 100 : $limit) : ''
141
        );
142
143
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
144
        $result = Cache::get(md5($sql));
145
        if ($result !== null) {
146
            return $result;
147
        }
148
149
        $result = Release::fromQuery($sql);
150
        Cache::put(md5($sql), $result, $expiresAt);
151
152
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the documented return type Illuminate\Database\Eloq...ase\Eloquent\Collection.
Loading history...
153
    }
154
155
    /**
156
     * @return Release[]|Collection|mixed
157
     */
158
    public function getMyMoviesRss(int $limit, int $userID = 0, array $excludedCats = [])
159
    {
160
        $sql = sprintf(
161
            "
162
				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,
163
					CONCAT(cp.title, '-', c.title) AS category_name,
164
					COALESCE(cp.id,0) AS parentid
165
				FROM releases r
166
				LEFT JOIN categories c ON c.id = r.categories_id
167
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
168
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
169
				LEFT JOIN movieinfo mi ON mi.id = r.movieinfo_id
170
				WHERE %s %s
171
				AND r.nzbstatus = %d
172
				AND r.categories_id BETWEEN %d AND %d
173
				AND r.passwordstatus %s
174
				ORDER BY postdate DESC %s",
175
            $this->releases->uSQL(
176
                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

176
                /** @scrutinizer ignore-type */ UserMovie::fromQuery(
Loading history...
177
                    sprintf(
178
                        '
179
							SELECT imdbid, categories
180
							FROM user_movies
181
							WHERE users_id = %d',
182
                        $userID
183
                    )
184
                ),
185
                'imdbid'
186
            ),
187
            (\count($excludedCats) > 0 ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
188
            NZB::NZB_ADDED,
189
            Category::MOVIE_ROOT,
190
            Category::MOVIE_OTHER,
191
            $this->releases->showPasswords(),
192
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', $limit > 100 ? 100 : $limit) : ''
193
        );
194
195
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
196
        $result = Cache::get(md5($sql));
197
        if ($result !== null) {
198
            return $result;
199
        }
200
201
        $result = Release::fromQuery($sql);
202
203
        Cache::put(md5($sql), $result, $expiresAt);
204
205
        return $result;
206
    }
207
208
    /**
209
     * @return Model|\Illuminate\Database\Query\Builder|null
210
     */
211
    public function getFirstInstance($column, $table, $order)
212
    {
213
        return DB::table($table)
214
            ->select([$column])
215
            ->where($column, '>', 0)
216
            ->orderBy($order)
217
            ->first();
218
    }
219
}
220