Add DatabaseCredentials class and implement database picker question

This commit is contained in:
Tim
2021-12-16 00:06:38 +01:00
parent d78f52b965
commit 0ff9021d6e
5 changed files with 96 additions and 3 deletions

View 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;
}
}

View File

@ -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);
}

View 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;
}
}