DropDatabaseCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 5
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 9
cp 0
c 0
b 0
f 0
cc 3
rs 9.4285
crap 12
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 DropDatabaseCommand extends Command
13
{
14
    public const COMMAND_NAME = 'orm:database:drop';
15
16
    private const OPTION_IF_EXISTS = 'if-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('Drop the configured database')
54
            ->addOption(
55
                self::OPTION_IF_EXISTS,
56
                null,
57
                InputOption::VALUE_NONE,
58
                'Don\'t trigger an error, when the database does not exist'
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_EXISTS)
69
            && !in_array($this->name, $this->connection->getSchemaManager()->listDatabases())
70
        ) {
71
            return;
72
        }
73
74
        $this->connection->getSchemaManager()->dropDatabase($this->name);
75
76
        $output->writeln(sprintf('<info>Dropped database <comment>%s</comment></info>', $this->name));
77
    }
78
}
79