Passed
Push — master ( 7f70db...b3d595 )
by Darko
11:16
created

RSS::getFirstInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Models\Category;
6
use App\Models\Release;
7
use Blacklight\Releases;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Collection;
10
use Illuminate\Support\Facades\Cache;
11
use Illuminate\Support\Facades\DB;
12
13
/**
14
 * Class RSS -- contains specific functions for RSS.
15
 */
16
class RSS extends ApiController
17
{
18
    public Releases $releases;
19
20
    /**
21
     * @throws \Exception
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
        $this->releases = new Releases;
27
    }
28
29
    /**
30
     * @return Release[]|Collection|mixed
31
     */
32
    public function getRss($cat, $videosId, $aniDbID, int $userID = 0, int $airDate = -1, int $limit = 100, int $offset = 0)
33
    {
34
        $catSearch = $cartSearch = '';
35
        $catLimit = 'AND r.categories_id BETWEEN '.Category::TV_ROOT.' AND '.Category::TV_OTHER;
36
        if (\count($cat)) {
37
            if ((int) $cat[0] === -2) {
38
                $cartSearch = sprintf(
39
                    'INNER JOIN users_releases ON users_releases.users_id = %d AND users_releases.releases_id = r.id',
40
                    $userID
41
                );
42
            } elseif ((int) $cat[0] !== -1) {
43
                $catSearch = Category::getCategorySearch($cat);
44
            }
45
        }
46
        $sql =
47
            sprintf(
48
                "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,
49
					m.cover, m.imdbid, m.rating, m.plot, m.year, m.genre, m.director, m.actors,
50
					g.name AS group_name,
51
					CONCAT(cp.title, ' > ', c.title) AS category_name,
52
					COALESCE(cp.id,0) AS parentid,
53
					mu.title AS mu_title, mu.url AS mu_url, mu.artist AS mu_artist,
54
					mu.publisher AS mu_publisher, mu.releasedate AS mu_releasedate,
55
					mu.review AS mu_review, mu.tracks AS mu_tracks, mu.cover AS mu_cover,
56
					mug.title AS mu_genre, co.title AS co_title, co.url AS co_url,
57
					co.publisher AS co_publisher, co.releasedate AS co_releasedate,
58
					co.review AS co_review, co.cover AS co_cover, cog.title AS co_genre,
59
					bo.cover AS bo_cover
60
				FROM releases r
61
				LEFT JOIN categories c ON c.id = r.categories_id
62
				INNER JOIN root_categories cp ON cp.id = c.root_categories_id
63
				LEFT JOIN usenet_groups g ON g.id = r.groups_id
64
				LEFT OUTER JOIN musicinfo mu ON mu.id = r.musicinfo_id
65
				LEFT OUTER JOIN genres mug ON mug.id = mu.genres_id
66
				LEFT OUTER JOIN consoleinfo co ON co.id = r.consoleinfo_id
67
				LEFT JOIN movieinfo m ON m.id = r.movieinfo_id
68
				LEFT OUTER JOIN genres cog ON cog.id = co.genres_id %s
69
				LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id
70
				LEFT OUTER JOIN bookinfo bo ON bo.id = r.bookinfo_id
71
				WHERE r.passwordstatus %s
72
				%s %s %s %s
73
				ORDER BY postdate DESC %s",
74
                $cartSearch,
75
                $this->releases->showPasswords(),
76
                $catSearch,
0 ignored issues
show
Bug introduced by
It seems like $catSearch can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|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

76
                /** @scrutinizer ignore-type */ $catSearch,
Loading history...
77
                ($videosId > 0 ? sprintf('AND r.videos_id = %d %s', $videosId, ($catSearch === '' ? $catLimit : '')) : ''),
78
                ($aniDbID > 0 ? sprintf('AND r.anidbid = %d %s', $aniDbID, ($catSearch === '' ? $catLimit : '')) : ''),
79
                ($airDate > -1 ? sprintf('AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY)', $airDate) : ''),
80
                $limit === -1 ? '' : ' LIMIT '.$limit.' OFFSET '.$offset
81
            );
82
83
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
84
        $result = Cache::get(md5($sql));
85
        if ($result !== null) {
86
            return $result;
87
        }
88
89
        $result = Release::fromQuery($sql);
90
        Cache::put(md5($sql), $result, $expiresAt);
91
92
        return $result;
93
    }
94
95
    /**
96
     * @return Builder|Collection
97
     */
98
    public function getShowsRss(int $limit, int $userID = 0, array $excludedCats = [], int $airDate = -1)
99
    {
100
        $sql = sprintf(
101
            "SELECT DISTINCT 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, CONCAT(cp.title, '-', c.title) AS category_name, COALESCE(cp.id,0) AS parentid FROM releases r INNER JOIN user_series us ON us.videos_id = r.videos_id AND us.users_id = %d LEFT JOIN categories c ON c.id = r.categories_id INNER JOIN root_categories cp ON cp.id = c.root_categories_id LEFT JOIN usenet_groups g ON g.id = r.groups_id LEFT OUTER JOIN videos v ON v.id = r.videos_id LEFT OUTER JOIN tv_episodes tve ON tve.id = r.tv_episodes_id WHERE (us.categories IS NULL OR us.categories = '' OR us.categories = 'NULL' OR FIND_IN_SET(r.categories_id, REPLACE(us.categories,'|',',')) > 0)%s%s AND r.categories_id BETWEEN %d AND %d AND r.passwordstatus %s ORDER BY postdate DESC %s",
102
            $userID,
103
            (\count($excludedCats) ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
104
            ($airDate > -1 ? sprintf(' AND tve.firstaired >= DATE_SUB(CURDATE(), INTERVAL %d DAY)', $airDate) : ''),
105
            Category::TV_ROOT,
106
            Category::TV_OTHER,
107
            $this->releases->showPasswords(),
108
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', min($limit, 100)) : ''
109
        );
110
111
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
112
        $result = Cache::get(md5($sql));
113
        if ($result !== null) {
114
            return $result;
115
        }
116
117
        $result = Release::fromQuery($sql);
118
        Cache::put(md5($sql), $result, $expiresAt);
119
120
        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...
121
    }
122
123
    /**
124
     * @return Release[]|Collection|mixed
125
     */
126
    public function getMyMoviesRss(int $limit, int $userID = 0, array $excludedCats = [])
127
    {
128
        // Use stricter joins: movieinfo joined by imdbid, require non-null imdbid; prevents unrelated titles sharing incorrect links.
129
        $sql = sprintf(
130
            "SELECT DISTINCT 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, CONCAT(cp.title, '-', c.title) AS category_name, COALESCE(cp.id,0) AS parentid FROM releases r INNER JOIN user_movies um ON um.imdbid = r.imdbid AND um.users_id = %d LEFT JOIN categories c ON c.id = r.categories_id INNER JOIN root_categories cp ON cp.id = c.root_categories_id LEFT JOIN usenet_groups g ON g.id = r.groups_id LEFT JOIN movieinfo mi ON mi.imdbid = r.imdbid WHERE r.imdbid IS NOT NULL AND (um.categories IS NULL OR um.categories = '' OR um.categories = 'NULL' OR FIND_IN_SET(r.categories_id, REPLACE(um.categories,'|',',')) > 0)%s AND r.categories_id BETWEEN %d AND %d AND r.passwordstatus %s ORDER BY postdate DESC %s",
131
            $userID,
132
            (\count($excludedCats) > 0 ? ' AND r.categories_id NOT IN ('.implode(',', $excludedCats).')' : ''),
133
            Category::MOVIE_ROOT,
134
            Category::MOVIE_OTHER,
135
            $this->releases->showPasswords(),
136
            ! empty($limit) ? sprintf(' LIMIT %d OFFSET 0', min($limit, 100)) : ''
137
        );
138
139
        $expiresAt = now()->addMinutes(config('nntmux.cache_expiry_medium'));
140
        $result = Cache::get(md5($sql));
141
        if ($result !== null) {
142
            return $result;
143
        }
144
        $result = Release::fromQuery($sql);
145
146
        Cache::put(md5($sql), $result, $expiresAt);
147
148
        return $result;
149
    }
150
151
    /**
152
     * Get the first instance of a column from a table where the column is greater than 0, ordered by a specified column.
153
     *
154
     * @param  string  $column  The column to select and check
155
     * @param  string  $table  The table to query
156
     * @param  string  $order  The column to order by
157
     * @return object|null The first instance found or null if none found
158
     */
159
    public function getFirstInstance(string $column, string $table, string $order)
160
    {
161
        return DB::table($table)
162
            ->select([$column])
163
            ->where($column, '>', 0)
164
            ->orderBy($order)
0 ignored issues
show
Bug introduced by
$order 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

164
            ->orderBy(/** @scrutinizer ignore-type */ $order)
Loading history...
165
            ->first();
166
    }
167
}
168