Passed
Push — dev ( 9d591d...c59ba0 )
by Darko
22:01
created

QueueController   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 317
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 211
c 0
b 0
f 0
dl 0
loc 317
rs 8.8798

4 Methods

Rating   Name   Duplication   Size   Complexity  
B nzbget() 0 68 8
F index() 0 81 17
B sabnzbd() 0 70 6
C nzbVortex() 0 76 13

How to fix   Complexity   

Complex Class

Complex classes like QueueController 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 QueueController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Settings;
6
use Blacklight\NZBGet;
7
use Blacklight\NZBVortex;
8
use Blacklight\SABnzbd;
9
use Illuminate\Http\Request;
10
11
class QueueController extends BasePageController
12
{
13
    /**
14
     * @param \Illuminate\Http\Request $request
15
     *
16
     * @throws \Exception
17
     */
18
    public function index(Request $request)
19
    {
20
        $this->setPrefs();
21
22
        $queueType = $error = '';
23
        $queue = null;
24
        switch (Settings::settingValue('apps.sabnzbplus.integrationtype')) {
25
            case SABnzbd::INTEGRATION_TYPE_NONE:
26
                if ($this->userdata->queuetype === 2) {
27
                    $queueType = 'NZBGet';
28
                    $queue = new NZBGet($this);
29
                }
30
                break;
31
            case SABnzbd::INTEGRATION_TYPE_USER:
32
                switch ((int) $this->userdata->queuetype) {
33
                    case 1:
34
                        $queueType = 'Sabnzbd';
35
                        $queue = new SABnzbd($this);
36
                        break;
37
                    case 2:
38
                        $queueType = 'NZBGet';
39
                        $queue = new NZBGet($this);
40
                        break;
41
                }
42
                break;
43
        }
44
45
        if ($queue !== null) {
0 ignored issues
show
introduced by
The condition $queue !== null is always false.
Loading history...
46
            if ($queueType === 'Sabnzbd') {
47
                if (empty($queue->url)) {
48
                    $error = 'ERROR: The Sabnzbd URL is missing!';
49
                }
50
51
                if (empty($queue->apikey)) {
52
                    if ($error === '') {
53
                        $error = 'ERROR: The Sabnzbd API key is missing!';
54
                    } else {
55
                        $error .= ' The Sabnzbd API key is missing!';
56
                    }
57
                }
58
            }
59
60
            if ($error === '') {
61
                if ($request->has('del')) {
62
                    $queue->delFromQueue($request->input('del'));
63
                }
64
65
                if ($request->has('pause')) {
66
                    $queue->pauseFromQueue($request->input('pause'));
67
                }
68
69
                if ($request->has('resume')) {
70
                    $queue->resumeFromQueue($request->input('resume'));
71
                }
72
73
                if ($request->has('pall')) {
74
                    $queue->pauseAll();
75
                }
76
77
                if ($request->has('rall')) {
78
                    $queue->resumeAll();
79
                }
80
81
                $this->smarty->assign('serverURL', $queue->url);
82
            }
83
        }
84
85
        $this->smarty->assign(
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

85
        $this->smarty->/** @scrutinizer ignore-call */ 
86
                       assign(

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...
86
            [
87
                'queueType' => $queueType,
88
                'error' => $error,
89
                'user' => $this->userdata,
90
            ]
91
        );
92
        $title = 'Your '.$queueType.' Download Queue';
93
        $meta_title = 'View'.$queueType.' Queue';
94
        $meta_keywords = 'view,'.strtolower($queueType).',queue';
95
        $meta_description = 'View'.$queueType.' Queue';
96
        $content = $this->smarty->fetch('viewqueue.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

96
        /** @scrutinizer ignore-call */ 
97
        $content = $this->smarty->fetch('viewqueue.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...
97
        $this->smarty->assign(compact('title', 'content', 'meta_title', 'meta_keywords', 'meta_description'));
98
        $this->pagerender();
99
    }
100
101
    /**
102
     * @throws \Exception
103
     */
104
    public function nzbget()
105
    {
106
        $this->setPrefs();
107
        $nzbGet = new NZBGet($this);
108
109
        $output = '';
110
        $data = $nzbGet->getQueue();
111
112
        if ($data !== false) {
113
            if (\count($data) > 0) {
114
                $status = $nzbGet->status();
115
116
                if ($status !== false) {
0 ignored issues
show
introduced by
The condition $status !== false is always false.
Loading history...
117
                    $output .=
118
                        "<div class='container text-center' style='display:block;'>
119
				<div style='width:16.666666667%;float:left;'><b>Avg Speed:</b><br /> ".human_filesize($status['AverageDownloadRate'], 2)."/s </div>
120
				<div style='width:16.666666667%;float:left;'><b>Speed:</b><br /> ".human_filesize($status['DownloadRate'], 2)."/s </div>
121
				<div style='width:16.666666667%;float:left;'><b>Limit:</b><br /> ".human_filesize($status['DownloadLimit'], 2)."/s </div>
122
				<div style='width:16.666666667%;float:left;'><b>Queue Left(no pars):</b><br /> ".human_filesize($status['RemainingSizeLo'], 2)." </div>
123
				<div style='width:16.666666667%;float:left;'><b>Free Space:</b><br /> ".human_filesize($status['FreeDiskSpaceMB'] * 1024000, 2)." </div>
124
				<div style='width:16.666666667%;float:left;'><b>Status:</b><br /> ".($status['Download2Paused'] === 1 ? 'Paused' : 'Downloading').' </div>
125
			</div>';
126
                }
127
128
                $count = 1;
129
                $output .=
130
                    "<table class='table table-striped table-condensed table-highlight data'>
131
				<thead>
132
					<tr >
133
						<th style='width=10px;text-align:center;'>#</th>
134
						<th style='text-align:left;'>Name</th>
135
						<th style='width:80px;text-align:center;'>Size</th>
136
						<th style='width:80px;text-align:center;'>Left(+pars)</th>
137
						<th style='width:50px;text-align:center;'>Done</th>
138
						<th style='width:80px;text-align:center;'>Status</th>
139
						<th style='width:50px;text-align:center;'>Delete</th>
140
						<th style='width:80px;text-align:center;'><a href='?pall'>Pause all</a></th>
141
						<th style='width:80px;text-align:center;'><a href='?rall'>Resume all</a></th>
142
					</tr>
143
				</thead>
144
				<tbody>";
145
146
                foreach ($data as $item) {
147
                    $output .=
148
                        '<tr>'.
149
                        "<td style='text-align:center;width:10px'>".$count.'</td>'.
150
                        "<td style='text-align:left;'>".$item['NZBName'].'</td>'.
151
                        "<td style='text-align:center;'>".$item['FileSizeMB'].' MB</td>'.
152
                        "<td style='text-align:center;'>".$item['RemainingSizeMB'].' MB</td>'.
153
                        "<td style='text-align:center;'>".($item['FileSizeMB'] === 0 ? 0 : round(100 - ($item['RemainingSizeMB'] / $item['FileSizeMB']) * 100)).'%</td>'.
154
                        "<td style='text-align:center;'>".($item['ActiveDownloads'] > 0 ? 'Downloading' : 'Paused').'</td>'.
155
                        "<td style='text-align:center;'><a  onclick=\"return confirm('Are you sure?');\" href='?del=".$item['LastID']."'>Delete</a></td>".
156
                        "<td style='text-align:center;'><a href='?pause=".$item['LastID']."'>Pause</a></td>".
157
                        "<td style='text-align:center;'><a href='?resume=".$item['LastID']."'>Resume</a></td>".
158
                        '</tr>';
159
                    $count++;
160
                }
161
                $output .=
162
                    '</tbody>
163
		</table>';
164
            } else {
165
                $output .= "<br /><br /><p style='text-align:center;'>The queue is currently empty.</p>";
166
            }
167
        } else {
168
            $output .= "<p style='text-align:center;'>Error retreiving queue.</p>";
169
        }
170
171
        echo $output;
172
    }
173
174
    /**
175
     * @throws \Exception
176
     */
177
    public function sabnzbd()
178
    {
179
        $this->setPrefs();
180
        $sab = new SABnzbd($this);
181
182
        $output = '';
183
184
        $json = $sab->getAdvQueue();
185
186
        if ($json !== false) {
0 ignored issues
show
introduced by
The condition $json !== false is always true.
Loading history...
187
            $obj = json_decode($json);
188
            $queue = $obj->{'queue'};
189
            $count = 1;
190
191
            $output .=
192
                "<div class='text-center' style='display:block;'>
193
			<div style='width:16.666666667%;float:left;'><b>Speed:</b><br /> ".$obj->{'speed'}."B/s </div>
194
			<div style='width:16.666666667%;float:left;'><b>Queued:</b><br /> ".round($obj->{'mbleft'}, 2).'MB / '.round($obj->{'mb'}, 2).'MB'." </div>
195
			<div style='width:16.666666667%;float:left;'><b>Status:</b><br /> ".ucwords(strtolower($obj->{'state'}))." </div>
196
			<div style='width:16.666666667%;float:left;'><b>Free (temp):</b><br /> ".round($obj->{'diskspace1'})."GB </div>
197
			<div style='width:16.666666667%;float:left;'><b>Free Space:</b><br /> ".round($obj->{'diskspace2'})."GB</div>
198
			<div style='width:16.666666667%;float:left;'><b>Stats:</b><br /> ".preg_replace('/\s+\|\s+| /', ',', $obj->{'loadavg'}).' </div>
199
		</div>';
200
201
            if (\count($queue) > 0) {
202
                $output .=
203
                    "<table class='table table-striped table-condensed table-highlight data'>
204
				<thead>
205
					<tr >
206
						<th style='width=10px;text-align:center;'>#</th>
207
						<th style='text-align:left;'>Name</th>
208
						<th style='width:80px;text-align:center;'>Size</th>
209
						<th style='width:80px;text-align:center;'>Left</th>
210
						<th style='width:50px;text-align:center;'>Done</th>
211
						<th style='width:80px;text-align:center;'>Time Left</th>
212
						<th style='width:50px;text-align:center;'>Delete</th>
213
						<th style='width:80px;text-align:center;'><a href='?pall'>Pause all</a></th>
214
						<th style='width:80px;text-align:center;'><a href='?rall'>Resume all</a></th>
215
					</tr>
216
				</thead>
217
				<tbody>";
218
219
                foreach ($queue->{'slots'} as $item) {
220
                    if (strpos($item->{'filename'}, 'fetch NZB') === false) {
221
                        $output .=
222
                            '<tr>'.
223
                            "<td style='text-align:center;width:10px'>".$count.'</td>'.
224
                            "<td style='text-align:left;'>".$item->{'filename'}.'</td>'.
225
                            "<td style='text-align:center;'>".round($item->{'mb'}, 2).' MB</td>'.
226
                            "<td style='text-align:center;'>".round($item->{'mbleft'}, 2).' MB</td>'.
227
                            "<td style='text-align:center;'>".($item->{'mb'} === 0 ? 0 : round(100 - ($item->{'mbleft'} / $item->{'mb'}) * 100)).'%</td>'.
228
                            "<td style='text-align:center;'>".$item->{'timeleft'}.'</td>'.
229
                            "<td style='text-align:center;'><a  onclick=\"return confirm('Are you sure?');\" href='?del=".$item->{'id'}."'>Delete</a></td>".
230
                            "<td style='text-align:center;'><a href='?pause=".$item->{'id'}."'>Pause</a></td>".
231
                            "<td style='text-align:center;'><a href='?resume=".$item->{'id'}."'>Resume</a></td>".
232
                            '</tr>';
233
                        $count++;
234
                    }
235
                }
236
                $output .=
237
                    '</tbody>
238
			</table>';
239
            } else {
240
                $output .= "<br /><br /><p style='text-align:center;'>The queue is currently empty.</p>";
241
            }
242
        } else {
243
            $output .= "<p style='text-align:center;'>Error retrieving queue.</p>";
244
        }
245
246
        echo $output;
247
    }
248
249
    /**
250
     * @throws \Exception
251
     */
252
    public function nzbVortex()
253
    {
254
        $this->setPrefs();
255
        try {
256
            if (isset($_GET['isAjax'])) {
257
                $vortex = new NZBVortex;
258
259
                // I guess we Ajax this way.
260
                if (isset($_GET['getOverview'])) {
261
                    $overview = $vortex->getOverview();
262
                    $this->smarty->assign('overview', $overview);
263
                    $content = $this->smarty->fetch('nzbvortex-ajax.tpl');
264
                    echo $content;
265
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
266
                }
267
268
                if (isset($_GET['addQueue'])) {
269
                    $nzb = $_GET['addQueue'];
270
                    $vortex->addQueue($nzb);
271
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
272
                }
273
274
                if (isset($_GET['resume'])) {
275
                    $vortex->resume((int) $_GET['resume']);
276
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
277
                }
278
279
                if (isset($_GET['pause'])) {
280
                    $vortex->pause((int) $_GET['pause']);
281
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
282
                }
283
284
                if (isset($_GET['moveup'])) {
285
                    $vortex->moveUp((int) $_GET['moveup']);
286
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
287
                }
288
289
                if (isset($_GET['movedown'])) {
290
                    $vortex->moveDown((int) $_GET['movedown']);
291
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
292
                }
293
294
                if (isset($_GET['movetop'])) {
295
                    $vortex->moveTop((int) $_GET['movetop']);
296
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
297
                }
298
299
                if (isset($_GET['movebottom'])) {
300
                    $vortex->moveBottom((int) $_GET['movebottom']);
301
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
302
                }
303
304
                if (isset($_GET['delete'])) {
305
                    $vortex->delete((int) $_GET['delete']);
306
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
307
                }
308
309
                if (isset($_GET['filelist'])) {
310
                    $response = $vortex->getFilelist((int) $_GET['filelist']);
311
                    echo json_encode($response);
312
                    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
313
                }
314
            }
315
        } catch (\Exception $e) {
316
            header('HTTP/1.1 500 Internal Server Error');
317
            printf($e->getMessage());
318
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
319
        }
320
321
        $title = 'NZBVortex';
322
323
        $content = $this->smarty->fetch('nzbvortex.tpl');
324
325
        $this->smarty->assign(compact('title', 'content'));
326
327
        $this->pagerender();
328
    }
329
}
330