Completed
Push — dev ( f430a1...9d0435 )
by Darko
07:15
created

RssController::categoryFeedRss()   F

Complexity

Conditions 13
Paths 641

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 23
rs 2.9486
c 0
b 0
f 0
cc 13
nc 641
nop 1

How to fix   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;
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 Blacklight\utility\Utility;
11
use Illuminate\Support\Facades\Auth;
12
13
class RssController extends BasePageController
14
{
15
    /**
16
     * @param \Illuminate\Http\Request $request
17
     * @return void
18
     * @throws \Exception
19
     * @throws \Throwable
20
     */
21
    public function MyMoviesRss(Request $request)
22
    {
23
        $this->setPrefs();
24
        $rss = new RSS(['Settings' => $this->settings]);
25
        $offset = 0;
26
27
        $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

27
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
28
29
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
30
31
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
32
33
        $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

33
        $relData = $rss->getMyMoviesRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']));
Loading history...
34
35
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
36
    }
37
38
    /**
39
     * @param \Illuminate\Http\Request $request
40
     * @throws \Exception
41
     * @throws \Throwable
42
     */
43
    public function MyShowsRss(Request $request)
44
    {
45
        $this->setPrefs();
46
        $rss = new RSS(['Settings' => $this->settings]);
47
        $offset = 0;
48
        $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

48
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
49
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
50
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
51
        $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

51
        $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

51
        $relData = $rss->getShowsRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
Loading history...
52
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
53
54
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
55
    }
56
57
    /**
58
     * @param \Illuminate\Http\Request $request
59
     * @throws \Exception
60
     * @throws \Throwable
61
     */
62
    public function fullFeedRss(Request $request)
63
    {
64
        $this->setPrefs();
65
        $rss = new RSS(['Settings' => $this->settings]);
66
        $offset = 0;
67
        $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

67
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
68
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
69
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
70
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
71
        $userShow = $userAnidb = -1;
72
        if ($request->has('show')) {
73
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
74
        } elseif ($request->has('anidb')) {
75
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
76
        }
77
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
78
        $relData = $rss->getRss([0], $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

78
        $relData = $rss->getRss([0], $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

78
        $relData = $rss->getRss([0], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
79
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
80
    }
81
82
    /**
83
     * @throws \Exception
84
     */
85
    public function showRssDesc()
86
    {
87
        $this->setPrefs();
88
        $rss = new RSS(['Settings' => $this->settings]);
89
90
        $title = 'Rss Info';
91
        $meta_title = 'Rss Nzb Info';
92
        $meta_keywords = 'view,nzb,description,details,rss,atom';
93
        $meta_description = 'View information about NNTmux RSS Feeds.';
94
95
        $firstShow = $rss->getFirstInstance('videos_id', 'releases', 'id');
96
        $firstAni = $rss->getFirstInstance('anidbid', 'releases', 'id');
97
98
        if ($firstShow !== null) {
99
            $this->smarty->assign('show', $firstShow->videos_id);
0 ignored issues
show
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

99
            $this->smarty->/** @scrutinizer ignore-call */ 
100
                           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...
Bug introduced by
The property videos_id does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
100
        } else {
101
            $this->smarty->assign('show', 1);
102
        }
103
104
        if ($firstAni !== null) {
105
            $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...
106
        } else {
107
            $this->smarty->assign('anidb', 1);
108
        }
109
110
        $this->smarty->assign(
111
            [
112
                'categorylist'       => Category::getCategories(true, $this->userdata['categoryexclusions']),
113
                'parentcategorylist' => Category::getForMenu($this->userdata['categoryexclusions']),
114
            ]
115
        );
116
117
        $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

117
        /** @scrutinizer ignore-call */ 
118
        $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...
118
        $this->smarty->assign(
119
            [
120
                'content' => $content,
121
                'title' => $title,
122
                'meta_title' => $meta_title,
123
                'meta_keywords' => $meta_keywords,
124
                'meta_description' => $meta_description,
125
            ]
126
        );
127
        $this->pagerender();
128
    }
129
130
    public function cartRss(Request $request)
131
    {
132
        $this->setPrefs();
133
        $rss = new RSS(['Settings' => $this->settings]);
134
        $offset = 0;
135
        $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

135
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
136
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
137
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
138
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
139
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
140
        $userShow = $userAnidb = -1;
141
        if ($request->has('show')) {
142
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
143
        } elseif ($request->has('anidb')) {
144
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
145
        }
146
147
        $relData = $rss->getRss([-2], $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

147
        $relData = $rss->getRss([-2], $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

147
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
148
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
149
    }
150
151
    /**
152
     * @param \Illuminate\Http\Request $request
153
     * @return \Illuminate\Http\JsonResponse
154
     * @throws \Exception
155
     * @throws \Throwable
156
     */
157
    public function categoryFeedRss(Request $request)
158
    {
159
        $this->setPrefs();
160
        $rss = new RSS(['Settings' => $this->settings]);
161
        $offset = 0;
162
        if (! $request->has('id')) {
163
            return response()->json(['error' => 'Category ID is missing'], '403');
164
        }
165
166
        $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

166
        $user = $this->userCheck(/** @scrutinizer ignore-type */ $request->all());
Loading history...
167
        $categoryId = (int) $request->input('id');
168
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
169
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
170
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
171
        $userShow = $userAnidb = -1;
172
        if ($request->has('show')) {
173
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
174
        } elseif ($request->has('anidb')) {
175
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
176
        }
177
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
178
        $relData = $rss->getRss($categoryId, $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

178
        $relData = $rss->getRss($categoryId, $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

178
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
179
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
180
    }
181
182
    /**
183
     * @param \Illuminate\Http\Request $request
184
     * @return array|\Illuminate\Http\JsonResponse
185
     * @throws \Throwable
186
     */
187
    private function userCheck(Request $request)
188
    {
189
        // User requested a feed, ensure user is passing a valid api_token.
190
        if (Auth::check()) {
191
            $uid = $this->userdata->id;
192
            $rssToken = $this->userdata['api_token'];
193
            $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...
194
        } else {
195
            if (! $request->has('api_token')) {
196
                return response()->json(['error' => 'API key is required for viewing the RSS!'], 403);
197
            }
198
199
            $res = User::getByRssToken($request->input('api_token'));
200
201
            if ($res === null) {
202
                return response()->json(['error' => 'Invalid RSS token'], 403);
203
            }
204
205
            $uid = $res['id'];
206
            $rssToken = $res['api_token'];
207
            $maxRequests = $res->role->apirequests;
208
209
            if ($res->hasRole('Disabled')) {
210
                return response()->json(['error' => 'Your account is disabled'], 403);
211
            }
212
        }
213
214
        if (UserRequest::getApiRequests($uid) > $maxRequests) {
215
            return response()->json(['error' => 'You have reached your daily limit for API requests!'], 403);
216
        } else {
217
            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

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