BackupController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 36
c 1
b 0
f 0
dl 0
loc 117
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 24 3
A create() 0 2 1
A store() 0 8 1
A edit() 0 9 2
A show() 0 12 2
A destroy() 0 12 2
A update() 0 2 1
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\Admin\BackupRequest;
7
use App\Jobs\ApplicationBackupJob;
8
use App\Models\AppSettings;
9
use Illuminate\Http\Request;
10
use Inertia\Inertia;
11
use App\Traits\AppSettingsTrait;
12
use Carbon\Carbon;
13
use Illuminate\Support\Facades\Storage;
14
15
class BackupController extends Controller
16
{
17
    use AppSettingsTrait;
18
19
    /**
20
     * Application Backup Landing Page
21
     */
22
    public function index()
23
    {
24
        $this->authorize('viewAny', AppSettings::class);
25
26
        $backupList = Storage::disk('backups')->files();
27
        $backups    = [];
28
        foreach($backupList as $b)
29
        {
30
            $parts = pathinfo($b);
31
            if($parts['extension'] === 'zip')
32
            {
33
                $backups[] = [
34
                    'name' => $b,
35
                    'date' => Carbon::createFromTimestamp(Storage::disk('backups')->lastModified($b))->format('M d, Y h:m a'),
36
                ];
37
            }
38
        }
39
40
        return Inertia::render('Admin/Backups', [
41
            'settings' => [
42
                'enabled' => (bool) config('app.backups.enabled'),
43
                'number'  => config('app.backups.number'),
44
            ],
45
            'backups' => $backups,
46
        ]);
47
    }
48
49
    /**
50
     * Show the form for creating a new resource.
51
     *
52
     * @return \Illuminate\Http\Response
53
     */
54
    public function create()
55
    {
56
        //
57
    }
58
59
    /**
60
     * Update the automatic backup settings
61
     */
62
    public function store(BackupRequest $request)
63
    {
64
        $this->saveSettings('app.backups.enabled', $request->enabled);
65
        $this->saveSettings('app.backups.number',  $request->number);
66
67
        return back()->with([
68
            'message' => 'Automatic Backup Settings Updated',
69
            'type'    => 'success',
70
        ]);
71
    }
72
73
    /**
74
     * Run a system backup
75
     */
76
    public function show($id)
77
    {
78
        if($id === 'run')
79
        {
80
            ApplicationBackupJob::dispatch();
81
            return back()->with([
82
                'message' => 'Backup Started Successfully - currently running in background',
83
                'type'    => 'success',
84
            ]);
85
        }
86
87
        return abort(404);
88
    }
89
90
    /**
91
     * Download a backup file
92
     */
93
    public function edit($id)
94
    {
95
        //  Verify that the backup file exists
96
        if(!Storage::disk('backups')->exists($id))
97
        {
98
            abort(404, 'The backup file you are looking for does not exist');
99
        }
100
101
        return Storage::disk('backups')->download($id);
102
    }
103
104
    /**
105
     * Update the specified resource in storage.
106
     *
107
     * @param  \Illuminate\Http\Request  $request
108
     * @param  int  $id
109
     * @return \Illuminate\Http\Response
110
     */
111
    public function update(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

111
    public function update(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

111
    public function update(Request $request, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        //
114
115
    }
116
117
    /**
118
     * Delete a backup file
119
     */
120
    public function destroy($id)
121
    {
122
        //  Verify that the backup file exists
123
        if(!Storage::disk('backups')->exists($id))
124
        {
125
            abort(404, 'The backup file you are looking for does not exist');
126
        }
127
128
        Storage::disk('backups')->delete($id);
129
        return back()->with([
130
            'message' => $id.' deleted',
131
            'type'    => 'warning',
132
        ]);
133
    }
134
}
135