Completed
Push — dev ( e0bdc6...5bd128 )
by Darko
08:24
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 Illuminate\Support\Arr;
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
        $rss = new RSS(['Settings' => $this->settings]);
23
        $offset = 0;
24
25
        $user = $this->userCheck($request);
26
27
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
28
29
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
30
31
        $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

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

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

48
        $relData = $rss->getShowsRss(/** @scrutinizer ignore-type */ $userNum, $user['user_id'], User::getCategoryExclusionById($user['user_id']), $userAirDate);
Loading history...
49
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
50
51
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
52
    }
53
54
    /**
55
     * @param \Illuminate\Http\Request $request
56
     * @throws \Exception
57
     * @throws \Throwable
58
     */
59
    public function fullFeedRss(Request $request)
60
    {
61
        $rss = new RSS(['Settings' => $this->settings]);
62
        $offset = 0;
63
        $user = $this->userCheck($request);
64
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
65
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
66
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
67
        $userShow = $userAnidb = -1;
68
        if ($request->has('show')) {
69
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
70
        } elseif ($request->has('anidb')) {
71
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
72
        }
73
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
74
        $relData = $rss->getRss(Arr::wrap(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

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

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

95
            $this->smarty->/** @scrutinizer ignore-call */ 
96
                           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...
96
        } else {
97
            $this->smarty->assign('show', 1);
98
        }
99
100
        if ($firstAni !== null) {
101
            $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...
102
        } else {
103
            $this->smarty->assign('anidb', 1);
104
        }
105
106
        $this->smarty->assign(
107
            [
108
                'categorylist'       => Category::getCategories(true, $this->userdata['categoryexclusions']),
109
                'parentcategorylist' => Category::getForMenu($this->userdata['categoryexclusions']),
110
            ]
111
        );
112
113
        $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

113
        /** @scrutinizer ignore-call */ 
114
        $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...
114
        $this->smarty->assign(
115
            compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description')
116
        );
117
        $this->pagerender();
118
    }
119
120
    public function cartRss(Request $request)
121
    {
122
        $this->setPrefs();
123
        $rss = new RSS(['Settings' => $this->settings]);
124
        $offset = 0;
125
        $user = $this->userCheck($request);
126
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
127
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
128
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
129
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
130
        $userShow = $userAnidb = -1;
131
        if ($request->has('show')) {
132
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
133
        } elseif ($request->has('anidb')) {
134
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
135
        }
136
137
        $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

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

137
        $relData = $rss->getRss([-2], $userShow, $userAnidb, $user['user_id'], $userAirDate, $userLimit, /** @scrutinizer ignore-type */ $userNum);
Loading history...
138
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
139
    }
140
141
    /**
142
     * @param \Illuminate\Http\Request $request
143
     * @return \Illuminate\Http\JsonResponse
144
     * @throws \Exception
145
     * @throws \Throwable
146
     */
147
    public function categoryFeedRss(Request $request)
148
    {
149
        $this->setPrefs();
150
        $rss = new RSS(['Settings' => $this->settings]);
151
        $offset = 0;
152
        if (! $request->has('id')) {
153
            return response()->json(['error' => 'Category ID is missing'], '403');
154
        }
155
156
        $user = $this->userCheck($request);
157
        $categoryId = explode(',', $request->input('id'));
158
        $userAirDate = $request->has('airdate') && is_numeric($request->input('airdate')) ? abs($request->input('airdate')) : -1;
159
        $userNum = ($request->has('num') && is_numeric($request->input('num')) ? abs($request->input('num')) : 0);
160
        $userLimit = $request->has('limit') && is_numeric($request->input('limit')) ? $request->input('limit') : 100;
161
        $userShow = $userAnidb = -1;
162
        if ($request->has('show')) {
163
            $userShow = ((int) $request->input('show') === 0 ? -1 : $request->input('show') + 0);
164
        } elseif ($request->has('anidb')) {
165
            $userAnidb = ((int) $request->input('anidb') === 0 ? -1 : $request->input('anidb') + 0);
166
        }
167
        $outputXML = (! ($request->has('o') && $request->input('o') === 'json'));
168
        $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

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

168
        $relData = $rss->getRss($categoryId, $userShow, $userAnidb, $user['user_id'], /** @scrutinizer ignore-type */ $userAirDate, $userLimit, $userNum);
Loading history...
169
        $rss->output($relData, $user['params'], $outputXML, $offset, 'rss');
170
    }
171
172
    /**
173
     * @param \Illuminate\Http\Request $request
174
     * @return array|\Illuminate\Http\JsonResponse
175
     * @throws \Throwable
176
     */
177
    private function userCheck(Request $request)
178
    {
179
        if (! $request->has('api_token')) {
180
            return response()->json(['error' => 'API key is required for viewing the RSS!'], 403);
181
        }
182
183
        $res = User::getByRssToken($request->input('api_token'));
184
185
        if ($res === null) {
186
            return response()->json(['error' => 'Invalid RSS token'], 403);
187
        }
188
189
        $uid = $res['id'];
190
        $rssToken = $res['api_token'];
191
        $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...
192
193
        if ($res->hasRole('Disabled')) {
194
            return response()->json(['error' => 'Your account is disabled'], 403);
195
        }
196
197
        if (UserRequest::getApiRequests($uid) > $maxRequests) {
198
            return response()->json(['error' => 'You have reached your daily limit for API requests!'], 403);
199
        } else {
200
            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

200
            UserRequest::addApiRequest(/** @scrutinizer ignore-type */ $rssToken, $request->getRequestUri());
Loading history...
201
        }
202
        $params =
203
            [
204
                'dl'       => $request->has('dl') && $request->input('dl') === '1' ? '1' : '0',
205
                'del'      => $request->has('del') && $request->input('del') === '1' ? '1' : '0',
206
                'extended' => 1,
207
                'uid'      => $uid,
208
                'token'    => $rssToken,
209
            ];
210
211
        return ['user' => $res, 'user_id' => $uid, 'rss_token' => $rssToken, 'max_requests' => $maxRequests, 'params' => $params];
212
    }
213
}
214