Passed
Push — dev6 ( 126d7e...94112f )
by Ron
08:55
created

BackupController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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