CreateDatabaseCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 25
cp 0
c 0
b 0
f 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A __construct() 0 20 1
A execute() 0 11 3
1
<?php
2
3
namespace Simplex\Quickstart\Shared\Console;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DriverManager;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
final class CreateDatabaseCommand extends Command
13
{
14
    public const COMMAND_NAME = 'orm:database:create';
15
16
    private const OPTION_IF_NOT_EXISTS = 'if-not-exists';
17
18
    /** @var Connection */
19
    private $connection;
20
21
    /** @var string */
22
    private $name;
23
24
    /**
25
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
26
     */
27
    public function __construct(
28
        string $host,
29
        string $port,
30
        string $name,
31
        string $user,
32
        string $pass
33
    ) {
34
        $this->connection = DriverManager::getConnection(
35
            [
36
                'driver' => 'pdo_mysql',
37
                'host' => $host,
38
                'port' => $port,
39
                'user' => $user,
40
                'password' => $pass,
41
            ]
42
        );
43
44
        $this->name = $name;
45
46
        parent::__construct();
47
    }
48
49
    protected function configure()
50
    {
51
        $this
52
            ->setName(self::COMMAND_NAME)
53
            ->setDescription('Creates the configured database')
54
            ->addOption(
55
                self::OPTION_IF_NOT_EXISTS,
56
                null,
57
                InputOption::VALUE_NONE,
58
                'Don\'t trigger an error, when the database already exists'
59
            )
60
        ;
61
    }
62
63
    /**
64
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
65
     */
66
    protected function execute(InputInterface $input, OutputInterface $output)
67
    {
68
        if ($input->getOption(self::OPTION_IF_NOT_EXISTS)
69
            && in_array($this->name, $this->connection->getSchemaManager()->listDatabases())
70
        ) {
71
            return;
72
        }
73
74
        $this->connection->getSchemaManager()->createDatabase($this->name);
75
76
        $output->writeln(sprintf('<info>Created database <comment>%s</comment></info>', $this->name));
77
    }
78
}
79