Passed
Push — dev ( c55b95...34fb50 )
by Darko
06:54
created

AdminNzbController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 69
dl 0
loc 129
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B export() 0 64 5
B import() 0 51 10
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use Blacklight\NZBExport;
7
use Blacklight\NZBImport;
8
use Blacklight\Releases;
9
use Blacklight\utility\Utility;
10
use Illuminate\Http\Request;
11
12
class AdminNzbController extends BasePageController
13
{
14
    /**
15
     * @param \Illuminate\Http\Request $request
16
     *
17
     * @throws \Exception
18
     */
19
    public function import(Request $request)
20
    {
21
        $this->setAdminPrefs();
22
23
        $filesToProcess = [];
24
        if ($this->isPostBack()) {
25
            $useNzbName = false;
26
            $deleteNZB = true;
27
            // Get the list of NZB files from php /tmp folder if nzb files were uploaded.
28
            if (isset($_FILES['uploadedfiles'])) {
29
                foreach ($_FILES['uploadedfiles']['error'] as $key => $error) {
30
                    if ($error === UPLOAD_ERR_OK) {
31
                        $tmp_name = $_FILES['uploadedfiles']['tmp_name'][$key];
32
                        $name = $_FILES['uploadedfiles']['name'][$key];
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
33
                        $filesToProcess[] = $tmp_name;
34
                    }
35
                }
36
            } else {
37
38
                // Check if the user wants to use the file name as the release name.
39
                $useNzbName = ($request->has('usefilename') && $request->input('usefilename') === 'on');
40
41
                // Check if the user wants to delete the NZB file when done importing.
42
                $deleteNZB = ($request->has('deleteNZB') && $request->input('deleteNZB') === 'on');
43
44
                // Get the path the user set in the browser if he put one.
45
                $path = ($request->has('folder') ? $request->input('folder') : '');
46
                if (substr($path, \strlen($path) - 1) !== DS) {
47
                    $path .= DS;
48
                }
49
50
                // Get the files from the user specified path.
51
                $filesToProcess = glob($path.'*.nzb');
52
            }
53
54
            if (\count($filesToProcess) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $filesToProcess can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, 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

54
            if (\count(/** @scrutinizer ignore-type */ $filesToProcess) > 0) {
Loading history...
55
56
                // Create a new instance of NZBImport and send it the file locations.
57
                $NZBImport = new NZBImport(['Browser' => true, 'Settings' => null]);
58
59
                $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

59
                $this->smarty->/** @scrutinizer ignore-call */ 
60
                               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...
60
                    'output',
61
                    $NZBImport->beginImport($filesToProcess, $useNzbName, $deleteNZB)
0 ignored issues
show
Bug introduced by
It seems like $filesToProcess can also be of type false; however, parameter $filesToProcess of Blacklight\NZBImport::beginImport() does only seem to accept array, 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

61
                    $NZBImport->beginImport(/** @scrutinizer ignore-type */ $filesToProcess, $useNzbName, $deleteNZB)
Loading history...
62
                );
63
            }
64
        }
65
66
        $meta_title = $title = 'Import Nzbs';
67
        $content = $this->smarty->fetch('nzb-import.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

67
        /** @scrutinizer ignore-call */ 
68
        $content = $this->smarty->fetch('nzb-import.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...
68
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
69
        $this->adminrender();
70
    }
71
72
    /**
73
     * @param \Illuminate\Http\Request $request
74
     *
75
     * @throws \Exception
76
     */
77
    public function export(Request $request)
78
    {
79
        if (Utility::isCLI()) {
80
            exit('This script is only for exporting from the web, use the script in misc/testing'.
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...
81
                PHP_EOL);
82
        }
83
84
        $this->setAdminPrefs();
85
        $rel = new Releases();
86
87
        if ($this->isPostBack()) {
88
            $path = $request->input('folder');
89
            $postFrom = ($request->input('postfrom') ?? '');
90
            $postTo = ($request->input('postto') ?? '');
91
            $group = ($request->input('group') === '-1' ? 0 : (int) $request->input('group'));
92
            $gzip = ($request->input('gzip') === '1');
93
94
            if ($path !== '') {
95
                $NE = new NZBExport([
96
                    'Browser'  => true, 'Settings' => null,
97
                    'Releases' => $rel,
98
                ]);
99
                $retVal = $NE->beginExport(
100
                    [
101
                        $path,
102
                        $postFrom,
103
                        $postTo,
104
                        $group,
105
                        $gzip,
106
                    ]
107
                );
108
            } else {
109
                $retVal = 'Error, a path is required!';
110
            }
111
112
            $this->smarty->assign(
113
                [
114
                    'folder'   => $path,
115
                    'output'   => $retVal,
116
                    'fromdate' => $postFrom,
117
                    'todate'   => $postTo,
118
                    'group'    => $request->input('group'),
119
                    'gzip'     => $request->input('gzip'),
120
                ]
121
            );
122
        } else {
123
            $this->smarty->assign(
124
                [
125
                    'fromdate' => $rel->getEarliestUsenetPostDate(),
126
                    'todate'   => $rel->getLatestUsenetPostDate(),
127
                ]
128
            );
129
        }
130
131
        $meta_title = $title = 'Export Nzbs';
132
        $this->smarty->assign(
133
            [
134
                'gziplist'  => [1 => 'True', 0 => 'False'],
135
                'grouplist' => $rel->getReleasedGroupsForSelect(true),
136
            ]
137
        );
138
        $content = $this->smarty->fetch('nzb-export.tpl');
139
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
140
        $this->adminrender();
141
    }
142
}
143