lasthourcloud/app/Livewire/Project/Database/BackupEdit.php

101 lines
3.2 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Database;
use Livewire\Component;
2023-11-07 11:11:47 +00:00
use Spatie\Url\Url;
class BackupEdit extends Component
{
public $backup;
2023-08-11 14:13:53 +00:00
public $s3s;
2023-10-10 11:10:43 +00:00
public ?string $status = null;
2023-08-10 14:28:29 +00:00
public array $parameters;
protected $rules = [
'backup.enabled' => 'required|boolean',
'backup.frequency' => 'required|string',
'backup.number_of_backups_locally' => 'required|integer|min:1',
2023-08-10 15:55:03 +00:00
'backup.save_s3' => 'required|boolean',
2023-08-11 14:13:53 +00:00
'backup.s3_storage_id' => 'nullable|integer',
2023-10-13 13:45:24 +00:00
'backup.databases_to_backup' => 'nullable',
];
protected $validationAttributes = [
'backup.enabled' => 'Enabled',
'backup.frequency' => 'Frequency',
'backup.number_of_backups_locally' => 'Number of Backups Locally',
2023-08-10 15:55:03 +00:00
'backup.save_s3' => 'Save to S3',
2023-08-11 14:13:53 +00:00
'backup.s3_storage_id' => 'S3 Storage',
2023-10-13 13:45:24 +00:00
'backup.databases_to_backup' => 'Databases to Backup',
2023-08-11 14:13:53 +00:00
];
protected $messages = [
'backup.s3_storage_id' => 'Select a S3 Storage',
];
2023-08-10 14:28:29 +00:00
public function mount()
{
$this->parameters = get_route_parameters();
if (is_null(data_get($this->backup, 's3_storage_id'))) {
2023-08-11 14:13:53 +00:00
$this->backup->s3_storage_id = 'default';
}
2023-08-10 14:28:29 +00:00
}
2023-08-10 14:25:59 +00:00
public function delete()
{
2024-03-21 11:44:32 +00:00
try {
$this->backup->delete();
if ($this->backup->database->getMorphClass() === 'App\Models\ServiceDatabase') {
$previousUrl = url()->previous();
$url = Url::fromString($previousUrl);
$url = $url->withoutQueryParameter('selectedBackupId');
$url = $url->withFragment('backups');
$url = $url->getPath() . "#{$url->getFragment()}";
return redirect($url);
} else {
return redirect()->route('project.database.backup.index', $this->parameters);
}
} catch (\Throwable $e) {
return handleError($e, $this);
2023-11-07 11:11:47 +00:00
}
2023-08-10 14:25:59 +00:00
}
public function instantSave()
{
2023-08-11 14:13:53 +00:00
try {
$this->custom_validate();
$this->backup->save();
$this->backup->refresh();
2024-03-21 11:44:32 +00:00
$this->dispatch('success', 'Backup updated successfully.');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', $e->getMessage());
2023-08-11 14:13:53 +00:00
}
}
2023-08-11 14:13:53 +00:00
private function custom_validate()
{
2023-08-11 18:48:52 +00:00
if (!is_numeric($this->backup->s3_storage_id)) {
$this->backup->s3_storage_id = null;
}
$isValid = validate_cron_expression($this->backup->frequency);
if (!$isValid) {
2023-08-11 14:13:53 +00:00
throw new \Exception('Invalid Cron / Human expression');
}
$this->validate();
2023-08-11 14:13:53 +00:00
}
public function submit()
{
try {
$this->custom_validate();
2023-10-13 13:45:24 +00:00
if ($this->backup->databases_to_backup == '' || $this->backup->databases_to_backup === null) {
$this->backup->databases_to_backup = null;
}
2023-08-11 14:13:53 +00:00
$this->backup->save();
$this->backup->refresh();
2023-12-07 18:06:32 +00:00
$this->dispatch('success', 'Backup updated successfully');
2023-09-11 15:36:30 +00:00
} catch (\Throwable $e) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', $e->getMessage());
2023-08-11 14:13:53 +00:00
}
}
}