Completed
Push — dev ( f64915...a2f754 )
by Darko
07:58
created

RssController   F

Complexity

Total Complexity 64

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 64
eloc 117
dl 0
loc 224
rs 3.28
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A myMoviesRss() 0 18 5
F categoryFeedRss() 0 26 14
F cartRss() 0 22 13
A showRssDesc() 0 37 3
B myShowsRss() 0 14 7
B userCheck() 0 35 9
F fullFeedRss() 0 20 13

How to fix   Complexity   

Complex Class

Complex classes like RssController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RssController, and based on these observations, apply Extract Interface, too.

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\Support\Arr;
10
use Illuminate\Http\Request;
11
12
class RssController extends BasePageController
13
{
14
    /**
15
     * @param \Illuminate\Http\Request $request
16
     *
17
     * @return array|\Illuminate\Http\JsonResponse
18
     * @throws \Throwable
19
     */
20
    public function myMoviesRss(Request $request)
21
    {
22
        $rss = new RSS(['Settings' => $this->settings]);
23
        $offset = 0;
24
25
        $user = $this->userCheck($request);
26
27
        if (is_json($user)) {
0 ignored issues
show
Bug introduced by
The function is_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

27
        if (/** @scrutinizer ignore-call */ is_json($user)) {
Loading history...
28
            return $user;
29
        }
30
31
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
32
33
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
34
35
        $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

35
        $relData = $rss->getMyMoviesRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']));
Loading history...
36
37
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
38
    }
39
40
    /**
41
     * @param \Illuminate\Http\Request $request
42
     *
43
     * @return array|\Illuminate\Http\JsonResponse
44
     * @throws \Throwable
45
     */
46
    public function myShowsRss(Request $request)
47
    {
48
        $rss = new RSS(['Settings' => $this->settings]);
49
        $offset = 0;
50
        $user = $this->userCheck($request);
51
        if (is_json($user)) {
0 ignored issues
show
Bug introduced by
The function is_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

51
        if (/** @scrutinizer ignore-call */ is_json($user)) {
Loading history...
52
            return $user;
53
        }
54
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
55
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
56
        $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

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

56
        $relData = $rss->getShowsRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
Loading history...
57
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
58
59
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
60
    }
61
62
    /**
63
     * @param \Illuminate\Http\Request $request
64
     *
65
     * @return array|\Illuminate\Http\JsonResponse
66
     * @throws \Throwable
67
     */
68
    public function fullFeedRss(Request $request)
69
    {
70
        $rss = new RSS(['Settings' => $this->settings]);
71
        $offset = 0;
72
        $user = $this->userCheck($request);
73
        if (is_json($user)) {
0 ignored issues
show
Bug introduced by
The function is_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
        if (/** @scrutinizer ignore-call */ is_json($user)) {
Loading history...
74
            return $user;
75
        }
76
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
77
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
78
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
79
        $userShow = $userAnidb = -1;
80
        if ($request->has('show')) {
81
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
82
        } elseif ($request->has('anidb')) {
83
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
84
        }
85
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
86
        $relData = $rss->getRss(Arr::wrap(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

86
        $relData = $rss->getRss(Arr::wrap(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

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

107
            $this->smarty->/** @scrutinizer ignore-call */ 
108
                           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...
108
        } else {
109
            $this->smarty->assign('show', 1);
110
        }
111
112
        if ($firstAni !== null) {
113
            $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...
114
        } else {
115
            $this->smarty->assign('anidb', 1);
116
        }
117
118
        $this->smarty->assign(
119
            [
120
                'categorylist'       => Category::getCategories(true, $this->userdata['categoryexclusions']),
121
                'parentcategorylist' => Category::getForMenu($this->userdata['categoryexclusions']),
122
            ]
123
        );
124
125
        $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

125
        /** @scrutinizer ignore-call */ 
126
        $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...
126
        $this->smarty->assign(
127
            compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description')
128
        );
129
        $this->pagerender();
130
    }
131
132
    /**
133
     * @param \Illuminate\Http\Request $request
134
     *
135
     * @return array|\Illuminate\Http\JsonResponse
136
     * @throws \Throwable
137
     */
138
    public function cartRss(Request $request)
139
    {
140
        $this->setPrefs();
141
        $rss = new RSS(['Settings' => $this->settings]);
142
        $offset = 0;
143
        $user = $this->userCheck($request);
144
        if (is_json($user)) {
0 ignored issues
show
Bug introduced by
The function is_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

144
        if (/** @scrutinizer ignore-call */ is_json($user)) {
Loading history...
145
            return $user;
146
        }
147
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
148
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
149
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
150
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
151
        $userShow = $userAnidb = -1;
152
        if ($request->has('show')) {
153
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
154
        } elseif ($request->has('anidb')) {
155
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
156
        }
157
158
        $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

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

158
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
159
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
160
    }
161
162
    /**
163
     * @param \Illuminate\Http\Request $request
164
     *
165
     * @return array|\Illuminate\Http\JsonResponse
166
     * @throws \Throwable
167
     */
168
    public function categoryFeedRss(Request $request)
169
    {
170
        $this->setPrefs();
171
        $rss = new RSS(['Settings' => $this->settings]);
172
        $offset = 0;
173
        if (! $request->has('id')) {
174
            return response()->json(['error' => 'Category ID is missing'], '403');
175
        }
176
177
        $user = $this->userCheck($request);
178
        if (is_json($user)) {
0 ignored issues
show
Bug introduced by
The function is_json was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

178
        if (/** @scrutinizer ignore-call */ is_json($user)) {
Loading history...
179
            return $user;
180
        }
181
        $categoryId = explode(',', $request->input('id'));
182
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
183
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
184
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
185
        $userShow = $userAnidb = -1;
186
        if ($request->has('show')) {
187
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
188
        } elseif ($request->has('anidb')) {
189
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
190
        }
191
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
192
        $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

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

192
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
193
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
194
    }
195
196
    /**
197
     * @param \Illuminate\Http\Request $request
198
     * @return array|\Illuminate\Http\JsonResponse
199
     * @throws \Throwable
200
     */
201
    private function userCheck(Request $request)
202
    {
203
        if (! $request->has('api_token')) {
204
            return response()->json(['error' => 'API key is required for viewing the RSS!'], 403);
205
        }
206
207
        $res = User::getByRssToken($request->input('api_token'));
208
209
        if ($res === null) {
210
            return response()->json(['error' => 'Invalid RSS token'], 403);
211
        }
212
213
        $uid = $res['id'];
214
        $rssToken = $res['api_token'];
215
        $maxRequests = $res->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...
216
217
        if ($res->hasRole('Disabled')) {
218
            return response()->json(['error' => 'Your account is disabled'], 403);
219
        }
220
221
        if (UserRequest::getApiRequests($uid) > $maxRequests) {
222
            return response()->json(['error' => 'You have reached your daily limit for API requests!'], 403);
223
        } else {
224
            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

224
            UserRequest::addApiRequest(/** @scrutinizer ignore-type */ $rssToken, $request->getRequestUri());
Loading history...
225
        }
226
        $params =
227
            [
228
                'dl'       => $request->has('dl') && $request->input('dl') === '1' ? '1' : '0',
229
                'del'      => $request->has('del') && $request->input('del') === '1' ? '1' : '0',
230
                'extended' => 1,
231
                'uid'      => $uid,
232
                'token'    => $rssToken,
233
            ];
234
235
        return ['user' => $res, 'user_id' => $uid, 'rss_token' => $rssToken, 'max_requests' => $maxRequests, 'params' => $params];
236
    }
237
}
238