diff --git a/.env b/.env index 1ef7e5c..580cf9c 100644 --- a/.env +++ b/.env @@ -20,5 +20,5 @@ APP_SECRET= ###< symfony/framework-bundle ### DATABASE_HOSTNAME=localhost -DATABASE_USER=user -DATABASE_PASSWORD=password \ No newline at end of file +DATABASE_USER=admin +DATABASE_PASSWORD=admin \ No newline at end of file diff --git a/config/services.yaml b/config/services.yaml index 28f9639..2d6a76f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -22,9 +22,3 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones - - App\Service\DatabaseCredentials: - arguments: - $hostname: '%env(string:DATABASE_HOSTNAME)%' - $user: '%env(string:DATABASE_USER)%' - $password: '%env(DATABASE_PASSWORD)%' \ No newline at end of file diff --git a/src/Service/BackupService.php b/src/Service/BackupService.php index f6667bc..1e4de21 100644 --- a/src/Service/BackupService.php +++ b/src/Service/BackupService.php @@ -4,10 +4,13 @@ namespace App\Service; use Symfony\Component\Process\Process; -class BackupService +readonly class BackupService { + private const COMMAND_DUMP = 'mariadb-dump'; + private const COMMAND_DB = 'mariadb'; + public function __construct( - private readonly DatabaseCredentials $credentials + private DatabaseCredentials $credentials ) {} public function backup(string $db, ?string $fileName = null): string @@ -18,11 +21,11 @@ class BackupService } $process = new Process([ - 'mysqldump', - '-u', $this->credentials->getUser(), + self::COMMAND_DUMP, + '-u', $this->credentials->user, $db, ]); - $process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]); + $process->setEnv(['MYSQL_PWD' => $this->credentials->password]); $process->start(); $stream = fopen($fileName, 'w+'); @@ -40,14 +43,14 @@ class BackupService public function restore(string $db, string $fileName): void { $process = new Process([ - 'mysql', - '-u', $this->credentials->getUser(), + self::COMMAND_DB, + '-u', $this->credentials->user, $db, ]); $stream = fopen($fileName, 'r'); - $process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]); + $process->setEnv(['MYSQL_PWD' => $this->credentials->password]); $process->setInput($stream); $process->run(); } diff --git a/src/Service/DatabaseCredentials.php b/src/Service/DatabaseCredentials.php index cf069d9..7978756 100644 --- a/src/Service/DatabaseCredentials.php +++ b/src/Service/DatabaseCredentials.php @@ -2,26 +2,13 @@ namespace App\Service; -class DatabaseCredentials +use Symfony\Component\DependencyInjection\Attribute\Autowire; + +readonly class DatabaseCredentials { public function __construct( - private readonly string $hostname, - private readonly string $user, - private readonly string $password, + #[Autowire('%env(string:DATABASE_HOSTNAME)%')] public string $hostname, + #[Autowire('%env(string:DATABASE_USER)%')] public string $user, + #[Autowire('%env(string:DATABASE_PASSWORD)%')] public string $password, ) {} - - public function getHostname(): string - { - return $this->hostname; - } - - public function getUser(): string - { - return $this->user; - } - - public function getPassword(): string - { - return $this->password; - } } \ No newline at end of file diff --git a/src/Service/DatabaseService.php b/src/Service/DatabaseService.php index 7dfebbd..f9e4e72 100644 --- a/src/Service/DatabaseService.php +++ b/src/Service/DatabaseService.php @@ -5,15 +5,12 @@ namespace App\Service; use Exception; use mysqli; -class DatabaseService +readonly class DatabaseService { - private mysqli $conn; - public function __construct( - private readonly MysqliConnection $service + private MysqliConnection $conn ) { - $this->conn = $this->service->getConnection(); } public function listDatabases(): array diff --git a/src/Service/MysqliConnection.php b/src/Service/MysqliConnection.php index ba763ce..ab15654 100644 --- a/src/Service/MysqliConnection.php +++ b/src/Service/MysqliConnection.php @@ -5,20 +5,13 @@ namespace App\Service; use Exception; use mysqli; -class MysqliConnection +class MysqliConnection extends mysqli { - private mysqli $connection; - public function __construct(DatabaseCredentials $credentials) { - $this->connection = new mysqli($credentials->getHostname(), $credentials->getUser(), $credentials->getPassword()); - if ($this->connection->connect_error) { - throw new Exception("Connection failed: " . $this->connection->connect_error); + parent::__construct($credentials->hostname, $credentials->user, $credentials->password); + if ($this->connect_error) { + throw new Exception("Connection failed: " . $this->connect_error); } } - - public function getConnection(): mysqli - { - return $this->connection; - } } \ No newline at end of file