mirror of
https://github.com/crivion/laranode.git
synced 2026-06-02 12:14:12 +08:00
mysql manager: refactoring to services & actions to keep my controller clean
This commit is contained in:
49
app/Services/MySQL/DeleteDatabaseService.php
Normal file
49
app/Services/MySQL/DeleteDatabaseService.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\MySQL;
|
||||
|
||||
use App\Models\Database;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class DeleteDatabaseException extends Exception {}
|
||||
|
||||
class DeleteDatabaseService
|
||||
{
|
||||
public function __construct(private Database $database) {}
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->dropMySQLDatabase();
|
||||
$this->dropMySQLUser();
|
||||
$this->deleteDatabaseRecord();
|
||||
}
|
||||
|
||||
private function dropMySQLDatabase(): void
|
||||
{
|
||||
$name = $this->database->name;
|
||||
|
||||
try {
|
||||
DB::statement("DROP DATABASE IF EXISTS `$name`");
|
||||
} catch (Exception $e) {
|
||||
throw new DeleteDatabaseException('Failed to drop MySQL database: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function dropMySQLUser(): void
|
||||
{
|
||||
$dbUser = $this->database->db_user;
|
||||
|
||||
try {
|
||||
DB::statement("DROP USER IF EXISTS `$dbUser`@'localhost'");
|
||||
DB::statement("FLUSH PRIVILEGES");
|
||||
} catch (Exception $e) {
|
||||
throw new DeleteDatabaseException('Failed to drop MySQL user: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteDatabaseRecord(): void
|
||||
{
|
||||
$this->database->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user