Add DatabaseCredentials class and implement database picker question
This commit is contained in:
29
src/Service/DatabaseCredentials.php
Normal file
29
src/Service/DatabaseCredentials.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
class DatabaseCredentials
|
||||
{
|
||||
public function __construct(
|
||||
private string $hostname,
|
||||
private string $user,
|
||||
private string $password,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
public function getHostname(): string
|
||||
{
|
||||
return $this->hostname;
|
||||
}
|
||||
|
||||
public function getUser(): string
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
}
|
@ -8,9 +8,9 @@ class MysqliConnection
|
||||
{
|
||||
private mysqli $connection;
|
||||
|
||||
public function __construct($hostname, $user, $password)
|
||||
public function __construct(DatabaseCredentials $credentials)
|
||||
{
|
||||
$this->connection = new mysqli($hostname, $user, $password);
|
||||
$this->connection = new mysqli($credentials->getHostname(), $credentials->getUser(), $credentials->getPassword());
|
||||
if ($this->connection->connect_error) {
|
||||
throw new \Exception("Connection failed: " . $this->connection->connect_error);
|
||||
}
|
||||
|
22
src/Service/Traits/SelectDatabaseQuestion.php
Normal file
22
src/Service/Traits/SelectDatabaseQuestion.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Traits;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
|
||||
trait SelectDatabaseQuestion
|
||||
{
|
||||
private function getDatabaseName(InputInterface $input, OutputInterface $output): string
|
||||
{
|
||||
$question = $this->getHelper('question');
|
||||
|
||||
if (!$name = $input->getArgument('name')) {
|
||||
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases());
|
||||
$name = $question->ask($input, $output, $selectQuestion);
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user