Passed
Push — master ( 9f15d2...3a106b )
by Darko
10:30
created

FileListApiController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 18
c 1
b 0
f 0
dl 0
loc 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getFileList() 0 28 3
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\Release;
7
use Blacklight\NZB;
8
use Illuminate\Http\JsonResponse;
9
10
class FileListApiController extends Controller
11
{
12
    /**
13
     * Get file list for a release
14
     */
15
    public function getFileList(string $guid): JsonResponse
16
    {
17
        $nzb = new NZB;
18
19
        $rel = Release::getByGuid($guid);
20
        if (! $rel) {
21
            return response()->json(['error' => 'Release not found'], 404);
22
        }
23
24
        $nzbpath = $nzb->NZBPath($guid);
25
26
        if (! file_exists($nzbpath)) {
0 ignored issues
show
Bug introduced by
It seems like $nzbpath can also be of type false; however, parameter $filename of file_exists() 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

26
        if (! file_exists(/** @scrutinizer ignore-type */ $nzbpath)) {
Loading history...
27
            return response()->json(['error' => 'NZB file not found'], 404);
28
        }
29
30
        ob_start();
31
        @readgzfile($nzbpath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for readgzfile(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

31
        /** @scrutinizer ignore-unhandled */ @readgzfile($nzbpath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
It seems like $nzbpath can also be of type false; however, parameter $filename of readgzfile() 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

31
        @readgzfile(/** @scrutinizer ignore-type */ $nzbpath);
Loading history...
32
        $nzbfile = ob_get_clean();
33
34
        $files = $nzb->nzbFileList($nzbfile);
35
36
        return response()->json([
37
            'release' => [
38
                'guid' => $rel['guid'],
39
                'searchname' => $rel['searchname'],
40
            ],
41
            'files' => $files,
42
            'total' => count($files),
43
        ]);
44
    }
45
}
46