Completed
Push — dev ( 6124df...e0bdc6 )
by Darko
09:04
created

RssController::myMoviesRss()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 1
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\User;
6
use App\Models\Category;
7
use Blacklight\http\RSS;
8
use App\Models\UserRequest;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Auth;
11
12
class RssController extends BasePageController
13
{
14
    /**
15
     * @param \Illuminate\Http\Request $request
16
     * @return void
17
     * @throws \Exception
18
     * @throws \Throwable
19
     */
20
    public function myMoviesRss(Request $request)
21
    {
22
        $this->setPrefs();
23
        $rss = new RSS(['Settings' => $this->settings]);
24
        $offset = 0;
25
26
        $user = $this->userCheck($request->all());
0 ignored issues
show
Bug introduced by
$request->all() of type array is incompatible with the type Illuminate\Http\Request expected by parameter $request of App\Http\Controllers\RssController::userCheck(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
27
28
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
29
30
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
31
32
        $relData = $rss->getMyMoviesRss($userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']));
0 ignored issues
show
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $limit of Blacklight\http\RSS::getMyMoviesRss() does only seem to accept integer, 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

32
        $relData = $rss->getMyMoviesRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']));
Loading history...
33
34
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
35
    }
36
37
    /**
38
     * @param \Illuminate\Http\Request $request
39
     * @throws \Exception
40
     * @throws \Throwable
41
     */
42
    public function myShowsRss(Request $request)
43
    {
44
        $this->setPrefs();
45
        $rss = new RSS(['Settings' => $this->settings]);
46
        $offset = 0;
47
        $user = $this->userCheck($request->all());
0 ignored issues
show
Bug introduced by
$request->all() of type array is incompatible with the type Illuminate\Http\Request expected by parameter $request of App\Http\Controllers\RssController::userCheck(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
48
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
49
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
50
        $relData = $rss->getShowsRss($userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
0 ignored issues
show
Bug introduced by
It seems like $userAirDate can also be of type double; however, parameter $airDate of Blacklight\http\RSS::getShowsRss() does only seem to accept integer, 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

50
        $relData = $rss->getShowsRss($userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), /** @scrutinizer ignore-type */ $userAirDate);
Loading history...
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $limit of Blacklight\http\RSS::getShowsRss() does only seem to accept integer, 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

50
        $relData = $rss->getShowsRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
Loading history...
51
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
52
53
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
54
    }
55
56
    /**
57
     * @param \Illuminate\Http\Request $request
58
     * @throws \Exception
59
     * @throws \Throwable
60
     */
61
    public function fullFeedRss(Request $request)
62
    {
63
        $this->setPrefs();
64
        $rss = new RSS(['Settings' => $this->settings]);
65
        $offset = 0;
66
        $user = $this->userCheck($request->all());
0 ignored issues
show
Bug introduced by
$request->all() of type array is incompatible with the type Illuminate\Http\Request expected by parameter $request of App\Http\Controllers\RssController::userCheck(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
67
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
68
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
69
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
70
        $userShow = $userAnidb = -1;
71
        if ($request->has('show')) {
72
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
73
        } elseif ($request->has('anidb')) {
74
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
75
        }
76
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
77
        $relData = $rss->getRss([0], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, $userNum);
0 ignored issues
show
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $offset of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

77
        $relData = $rss->getRss([0], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
Bug introduced by
It seems like $userAirDate can also be of type double; however, parameter $airDate of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

77
        $relData = $rss->getRss([0], $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
78
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
79
    }
80
81
    /**
82
     * @throws \Exception
83
     */
84
    public function showRssDesc()
85
    {
86
        $this->setPrefs();
87
        $rss = new RSS(['Settings' => $this->settings]);
88
89
        $title = 'Rss Info';
90
        $meta_title = 'Rss Nzb Info';
91
        $meta_keywords = 'view,nzb,description,details,rss,atom';
92
        $meta_description = 'View information about NNTmux RSS Feeds.';
93
94
        $firstShow = $rss->getFirstInstance('videos_id', 'releases', 'id');
95
        $firstAni = $rss->getFirstInstance('anidbid', 'releases', 'id');
96
97
        if ($firstShow !== null) {
98
            $this->smarty->assign('show', $firstShow->videos_id);
0 ignored issues
show
Bug introduced by
The property videos_id does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            $this->smarty->/** @scrutinizer ignore-call */ 
99
                           assign('show', $firstShow->videos_id);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
        } else {
100
            $this->smarty->assign('show', 1);
101
        }
102
103
        if ($firstAni !== null) {
104
            $this->smarty->assign('anidb', $firstAni->anidbid);
0 ignored issues
show
Bug introduced by
The property anidbid does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
105
        } else {
106
            $this->smarty->assign('anidb', 1);
107
        }
108
109
        $this->smarty->assign(
110
            [
111
                'categorylist'       => Category::getCategories(true, $this->userdata['categoryexclusions']),
112
                'parentcategorylist' => Category::getForMenu($this->userdata['categoryexclusions']),
113
            ]
114
        );
115
116
        $content = $this->smarty->fetch('rssdesc.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        /** @scrutinizer ignore-call */ 
117
        $content = $this->smarty->fetch('rssdesc.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
        $this->smarty->assign(
118
            [
119
                'content' => $content,
120
                'title' => $title,
121
                'meta_title' => $meta_title,
122
                'meta_keywords' => $meta_keywords,
123
                'meta_description' => $meta_description,
124
            ]
125
        );
126
        $this->pagerender();
127
    }
128
129
    public function cartRss(Request $request)
130
    {
131
        $this->setPrefs();
132
        $rss = new RSS(['Settings' => $this->settings]);
133
        $offset = 0;
134
        $user = $this->userCheck($request->all());
0 ignored issues
show
Bug introduced by
$request->all() of type array is incompatible with the type Illuminate\Http\Request expected by parameter $request of App\Http\Controllers\RssController::userCheck(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
135
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
136
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
137
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
138
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
139
        $userShow = $userAnidb = -1;
140
        if ($request->has('show')) {
141
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
142
        } elseif ($request->has('anidb')) {
143
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
144
        }
145
146
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, $userNum);
0 ignored issues
show
Bug introduced by
It seems like $userAirDate can also be of type double; however, parameter $airDate of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

146
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $offset of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

146
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
147
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
148
    }
149
150
    /**
151
     * @param \Illuminate\Http\Request $request
152
     * @return \Illuminate\Http\JsonResponse
153
     * @throws \Exception
154
     * @throws \Throwable
155
     */
156
    public function categoryFeedRss(Request $request)
157
    {
158
        $this->setPrefs();
159
        $rss = new RSS(['Settings' => $this->settings]);
160
        $offset = 0;
161
        if (! $request->has('id')) {
162
            return response()->json(['error' => 'Category ID is missing'], '403');
163
        }
164
165
        $user = $this->userCheck($request->all());
0 ignored issues
show
Bug introduced by
$request->all() of type array is incompatible with the type Illuminate\Http\Request expected by parameter $request of App\Http\Controllers\RssController::userCheck(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
166
        $categoryId = (int) $request->input('id');
167
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
168
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
169
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
170
        $userShow = $userAnidb = -1;
171
        if ($request->has('show')) {
172
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
173
        } elseif ($request->has('anidb')) {
174
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
175
        }
176
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
177
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, $userNum);
0 ignored issues
show
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $offset of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

177
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
Bug introduced by
It seems like $userAirDate can also be of type double; however, parameter $airDate of Blacklight\http\RSS::getRss() does only seem to accept integer, 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

177
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
178
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
179
    }
180
181
    /**
182
     * @param \Illuminate\Http\Request $request
183
     * @return array|\Illuminate\Http\JsonResponse
184
     * @throws \Throwable
185
     */
186
    private function userCheck(Request $request)
187
    {
188
        // User requested a feed, ensure user is passing a valid api_token.
189
        if (Auth::check()) {
190
            $uid = $this->userdata->id;
191
            $rssToken = $this->userdata['api_token'];
192
            $maxRequests = $this->userdata->role->apirequests;
0 ignored issues
show
Bug introduced by
The property apirequests does not seem to exist on Spatie\Permission\Models\Role. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
193
        } else {
194
            if (! $request->has('api_token')) {
195
                return response()->json(['error' => 'API key is required for viewing the RSS!'], 403);
196
            }
197
198
            $res = User::getByRssToken($request->input('api_token'));
199
200
            if ($res === null) {
201
                return response()->json(['error' => 'Invalid RSS token'], 403);
202
            }
203
204
            $uid = $res['id'];
205
            $rssToken = $res['api_token'];
206
            $maxRequests = $res->role->apirequests;
207
208
            if ($res->hasRole('Disabled')) {
209
                return response()->json(['error' => 'Your account is disabled'], 403);
210
            }
211
        }
212
213
        if (UserRequest::getApiRequests($uid) > $maxRequests) {
214
            return response()->json(['error' => 'You have reached your daily limit for API requests!'], 403);
215
        } else {
216
            UserRequest::addApiRequest($rssToken, $request->getRequestUri());
0 ignored issues
show
Bug introduced by
It seems like $rssToken can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $token of App\Models\UserRequest::addApiRequest() does only seem to accept 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

216
            UserRequest::addApiRequest(/** @scrutinizer ignore-type */ $rssToken, $request->getRequestUri());
Loading history...
217
        }
218
        $params =
219
            [
220
                'dl'       => $request->has('dl') && $request->input('dl') === '1' ? '1' : '0',
221
                'del'      => $request->has('del') && $request->input('del') === '1' ? '1' : '0',
222
                'extended' => 1,
223
                'uid'      => $uid,
224
                'token'    => $rssToken,
225
            ];
226
227
        return ['user_id' => $uid, 'rss_token' => $rssToken, 'max_requests' => $maxRequests, 'params' => $params];
228
    }
229
}
230