RssController::cartRss()   F
last analyzed

Complexity

Conditions 13
Paths 641

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 21
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\Http\Controllers\Api\RSS;
6
use App\Models\Category;
7
use App\Models\User;
8
use App\Models\UserDownload;
9
use App\Models\UserRequest;
10
use Illuminate\Http\JsonResponse;
11
use Illuminate\Http\Request;
12
use Illuminate\Support\Arr;
13
use Illuminate\Support\Carbon;
14
15
class RssController extends BasePageController
16
{
17
    /**
18
     * @return JsonResponse|void
19
     *
20
     * @throws \Throwable
21
     */
22
    public function myMoviesRss(Request $request)
23
    {
24
        $rss = new RSS(['Settings' => $this->settings]);
0 ignored issues
show
Unused Code introduced by
The call to App\Http\Controllers\Api\RSS::__construct() has too many arguments starting with array('Settings' => $this->settings). ( Ignorable by Annotation )

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

24
        $rss = /** @scrutinizer ignore-call */ new RSS(['Settings' => $this->settings]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
25
        $offset = 0;
26
27
        $user = $this->userCheck($request);
28
29
        if (is_object($user)) {
30
            return $user;
31
        }
32
33
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
34
35
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
36
37
        $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 App\Http\Controllers\Api\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

37
        $relData = $rss->getMyMoviesRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']));
Loading history...
38
39
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
40
    }
41
42
    /**
43
     * @return JsonResponse|void
44
     *
45
     * @throws \Throwable
46
     */
47
    public function myShowsRss(Request $request)
48
    {
49
        $rss = new RSS;
50
        $offset = 0;
51
        $user = $this->userCheck($request);
52
        if (is_object($user)) {
53
            return $user;
54
        }
55
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
56
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
57
        $relData = $rss->getShowsRss($userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
0 ignored issues
show
Bug introduced by
It seems like $userNum can also be of type double; however, parameter $limit of App\Http\Controllers\Api\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

57
        $relData = $rss->getShowsRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
Loading history...
Bug introduced by
It seems like $userAirDate can also be of type double; however, parameter $airDate of App\Http\Controllers\Api\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

57
        $relData = $rss->getShowsRss($userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), /** @scrutinizer ignore-type */ $userAirDate);
Loading history...
58
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
59
60
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
61
    }
62
63
    /**
64
     * @return JsonResponse|void
65
     *
66
     * @throws \Throwable
67
     */
68
    public function fullFeedRss(Request $request)
69
    {
70
        $this->setPreferences();
71
        $rss = new RSS;
72
        $offset = 0;
73
        $user = $this->userCheck($request);
74
        if (is_object($user)) {
75
            return $user;
76
        }
77
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
78
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
79
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
80
        $userShow = $userAnidb = -1;
81
        if ($request->has('show')) {
82
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
83
        } elseif ($request->has('anidb')) {
84
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
85
        }
86
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
87
        $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 App\Http\Controllers\Api\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

87
        $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 App\Http\Controllers\Api\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

87
        $relData = $rss->getRss(Arr::wrap(0), $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
88
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
89
    }
90
91
    /**
92
     * @throws \Exception
93
     */
94
    public function showRssDesc(): void
95
    {
96
        $this->setPreferences();
97
        $rss = new RSS;
98
99
        $title = 'Rss Info';
100
        $meta_title = 'Rss Nzb Info';
101
        $meta_keywords = 'view,nzb,description,details,rss,atom';
102
        $meta_description = 'View information about NNTmux RSS Feeds.';
103
104
        $firstShow = $rss->getFirstInstance('videos_id', 'releases', 'id');
105
        $firstAni = $rss->getFirstInstance('anidbid', 'releases', 'id');
106
107
        if ($firstShow !== null) {
108
            $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...
109
        } else {
110
            $this->smarty->assign('show', 1);
111
        }
112
113
        if ($firstAni !== null) {
114
            $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...
115
        } else {
116
            $this->smarty->assign('anidb', 1);
117
        }
118
119
        $catExclusions = $this->userdata->categoryexclusions ?? [];
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User.
Loading history...
120
121
        $this->smarty->assign(
122
            [
123
                'categorylist' => Category::getCategories(true, $catExclusions),
124
                'parentcategorylist' => Category::getForMenu($catExclusions),
125
            ]
126
        );
127
128
        $content = $this->smarty->fetch('rssdesc.tpl');
129
        $this->smarty->assign(
130
            compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description')
131
        );
132
        $this->pagerender();
133
    }
134
135
    /**
136
     * @throws \Throwable
137
     */
138
    public function cartRss(Request $request): JsonResponse|array
139
    {
140
        $rss = new RSS;
141
        $offset = 0;
142
        $user = $this->userCheck($request);
143
        if (is_object($user)) {
144
            return $user;
145
        }
146
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
147
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
148
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
149
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
150
        $userShow = $userAnidb = -1;
151
        if ($request->has('show')) {
152
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
153
        } elseif ($request->has('anidb')) {
154
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
155
        }
156
157
        $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 App\Http\Controllers\Api\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

157
        $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 App\Http\Controllers\Api\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

157
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
158
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Illuminate\Http\JsonResponse|array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
159
    }
160
161
    /**
162
     * @throws \Throwable
163
     */
164
    public function categoryFeedRss(Request $request): JsonResponse|array
165
    {
166
        $rss = new RSS;
167
        $offset = 0;
168
        if ($request->missing('id')) {
169
            return response()->json(['error' => 'Category ID is missing'], '403');
170
        }
171
172
        $user = $this->userCheck($request);
173
        if (is_object($user)) {
174
            return $user;
175
        }
176
        $categoryId = explode(',', $request->input('id'));
177
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
178
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
179
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
180
        $userShow = $userAnidb = -1;
181
        if ($request->has('show')) {
182
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
183
        } elseif ($request->has('anidb')) {
184
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
185
        }
186
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
187
        $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 App\Http\Controllers\Api\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

187
        $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 App\Http\Controllers\Api\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

187
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
188
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Illuminate\Http\JsonResponse|array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
189
    }
190
191
    /**
192
     * @throws \Throwable
193
     */
194
    private function userCheck(Request $request): JsonResponse|array
195
    {
196
        if ($request->missing('api_token')) {
197
            return response()->json(['error' => 'API key is required for viewing the RSS!'], 403);
198
        }
199
200
        $res = User::getByRssToken($request->input('api_token'));
201
202
        if ($res === null) {
203
            return response()->json(['error' => 'Invalid RSS token'], 403);
204
        }
205
206
        $uid = $res['id'];
207
        $rssToken = $res['api_token'];
208
        $maxRequests = $res->role->apirequests;
0 ignored issues
show
Bug introduced by
The property apirequests does not seem to exist on Spatie\Permission\Models\Role.
Loading history...
209
        $maxDownloads = $res->role->downloadrequests;
0 ignored issues
show
Bug introduced by
The property downloadrequests does not seem to exist on Spatie\Permission\Models\Role.
Loading history...
210
        $usedRequests = UserRequest::getApiRequests($uid);
211
        $time = UserRequest::whereUsersId($uid)->min('timestamp');
212
        $apiOldestTime = $time !== null ? Carbon::createFromTimeString($time)->toRfc2822String() : '';
213
        $grabTime = UserDownload::whereUsersId($uid)->min('timestamp');
214
        $oldestGrabTime = $grabTime !== null ? Carbon::createFromTimeString($grabTime)->toRfc2822String() : '';
215
216
        if ($res->hasRole('Disabled')) {
217
            return response()->json(['error' => 'Your account is disabled'], 403);
218
        }
219
220
        if ($usedRequests > $maxRequests) {
221
            return response()->json(['error' => 'You have reached your daily limit for API requests!'], 403);
222
        } else {
223
            UserRequest::addApiRequest($rssToken, $request->getRequestUri());
224
        }
225
        $params =
226
            [
227
                'dl' => $request->has('dl') && $request->input('dl') === '1' ? '1' : '0',
228
                'del' => $request->has('del') && $request->input('del') === '1' ? '1' : '0',
229
                'extended' => 1,
230
                'uid' => $uid,
231
                'token' => $rssToken,
232
                'apilimit' => $maxRequests,
233
                'requests' => $usedRequests,
234
                'downloadlimit' => $maxDownloads,
235
                'grabs' => UserDownload::getDownloadRequests($uid),
236
                'oldestapi' => $apiOldestTime,
237
                'oldestgrab' => $oldestGrabTime,
238
            ];
239
240
        return ['user' => $res, 'user_id' => $uid, 'rss_token' => $rssToken, 'max_requests' => $maxRequests, 'params' => $params];
241
    }
242
}
243