Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( 534532...ff2b19 )
by Cristian
09:41
created

BackupController::create()   B

Complexity

Conditions 3
Paths 10

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 14
nc 10
nop 0
1
<?php
2
3
namespace Backpack\BackupManager\app\Http\Controllers;
4
5
use Artisan;
6
use Exception;
7
use Illuminate\Routing\Controller;
8
use League\Flysystem\Adapter\Local;
9
use Log;
10
use Request;
11
use Response;
12
use Storage;
13
14
class BackupController extends Controller
15
{
16
    public function index()
17
    {
18
        if (!count(config('backup.backup.destination.disks'))) {
19
            dd(trans('backpack::backup.no_disks_configured'));
20
        }
21
22
        $this->data['backups'] = [];
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
24
        foreach (config('backup.backup.destination.disks') as $disk_name) {
25
            $disk = Storage::disk($disk_name);
26
            $adapter = $disk->getDriver()->getAdapter();
27
            $files = $disk->allFiles();
28
29
            // make an array of backup files, with their filesize and creation date
30
            foreach ($files as $k => $f) {
31
                // only take the zip files into account
32
                if (substr($f, -4) == '.zip' && $disk->exists($f)) {
33
                    $this->data['backups'][] = [
34
                        'file_path'     => $f,
35
                        'file_name'     => str_replace('backups/', '', $f),
36
                        'file_size'     => $disk->size($f),
37
                        'last_modified' => $disk->lastModified($f),
38
                        'disk'          => $disk_name,
39
                        'download'      => ($adapter instanceof Local) ? true : false,
40
                        ];
41
                }
42
            }
43
        }
44
45
        // reverse the backups, so the newest one would be on top
46
        $this->data['backups'] = array_reverse($this->data['backups']);
47
        $this->data['title'] = 'Backups';
48
49
        return view('backupmanager::backup', $this->data);
50
    }
51
52
    public function create()
53
    {
54
        try {
55
            ini_set('max_execution_time', 300);
56
            // start the backup process
57
            $flags = config('backup.backup.backpack_flags', false);
58
59
            if ($flags) {
60
                Artisan::call('backup:run', $flags);
61
            } else {
62
                Artisan::call('backup:run');
63
            }
64
65
            $output = Artisan::output();
66
67
            // log the results
68
            Log::info("Backpack\BackupManager -- new backup started from admin interface \r\n".$output);
69
            // return the results as a response to the ajax call
70
            echo $output;
71
        } catch (Exception $e) {
72
            Response::make($e->getMessage(), 500);
73
        }
74
75
        return 'success';
76
    }
77
78
    /**
79
     * Downloads a backup zip file.
80
     */
81
    public function download()
82
    {
83
        $disk = Storage::disk(Request::input('disk'));
0 ignored issues
show
Bug introduced by
It seems like \Request::input('disk') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Contracts\Filesystem\Factory::disk() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84
        $file_name = Request::input('file_name');
85
        $adapter = $disk->getDriver()->getAdapter();
86
87
        if ($adapter instanceof Local) {
88
            $storage_path = $disk->getDriver()->getAdapter()->getPathPrefix();
89
90
            if ($disk->exists($file_name)) {
0 ignored issues
show
Bug introduced by
It seems like $file_name defined by \Request::input('file_name') on line 84 can also be of type array or null; however, Illuminate\Contracts\Fil...em\Filesystem::exists() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
91
                return response()->download($storage_path.$file_name);
92
            } else {
93
                abort(404, trans('backpack::backup.backup_doesnt_exist'));
94
            }
95
        } else {
96
            abort(404, trans('backpack::backup.only_local_downloads_supported'));
97
        }
98
    }
99
100
    /**
101
     * Deletes a backup file.
102
     */
103
    public function delete($file_name)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $file_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
104
    {
105
        $disk = Storage::disk(Request::input('disk'));
0 ignored issues
show
Bug introduced by
It seems like \Request::input('disk') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Contracts\Filesystem\Factory::disk() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
106
107
        if ($disk->exists($file_name)) {
108
            $disk->delete($file_name);
109
110
            return 'success';
111
        } else {
112
            abort(404, trans('backpack::backup.backup_doesnt_exist'));
113
        }
114
    }
115
}
116