Completed
Push — master ( c9ec76...c066a0 )
by Dominik
01:42
created

CreateDatabaseCommand::__invoke()   C

Complexity

Conditions 9
Paths 168

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5
c 0
b 0
f 0
cc 9
eloc 27
nc 168
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Doctrine\DBAL\Command;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DriverManager;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * This file is part of the Doctrine Bundle.
14
 *
15
 * The code was originally distributed inside the Symfony framework.
16
 *
17
 * (c) Fabien Potencier <[email protected]>
18
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
19
 */
20
final class CreateDatabaseCommand
21
{
22
    /**
23
     * @var Connection
24
     */
25
    protected $connection;
26
27
    /**
28
     * @param Connection $connection
29
     */
30
    public function __construct(Connection $connection)
31
    {
32
        $this->connection = $connection;
33
    }
34
35
    /**
36
     * @param InputInterface  $input
37
     * @param OutputInterface $output
38
     *
39
     * @return int|null
40
     */
41
    public function __invoke(InputInterface $input, OutputInterface $output)
42
    {
43
        $connection = $this->connection;
44
45
        $params = $connection->getParams();
46
47
        if (isset($params['master'])) {
48
            $params = $params['master'];
49
        }
50
51
        $hasPath = isset($params['path']);
52
        $name = $hasPath ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
53
54
        if (!$name) {
55
            throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter.");
56
        }
57
58
        // Need to get rid of _every_ occurrence of dbname from connection configuration
59
        unset($params['dbname'], $params['path'], $params['url']);
60
61
        $tmpConnection = DriverManager::getConnection($params);
62
        $shouldNotCreateDatabase = in_array($name, $tmpConnection->getSchemaManager()->listDatabases());
63
64
        // Only quote if we don't have a path
65
        if (!$hasPath) {
66
            $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
67
        }
68
69
        $error = false;
70
71
        try {
72
            if ($shouldNotCreateDatabase) {
73
                $output->writeln(sprintf('<info>Database <comment>%s</comment> already exists.</info>', $name));
74
            } else {
75
                $tmpConnection->getSchemaManager()->createDatabase($name);
76
                $output->writeln(sprintf('<info>Created database <comment>%s</comment>', $name));
77
            }
78
        } catch (\Exception $e) {
79
            $output->writeln(sprintf('<error>Could not create database <comment>%s</comment></error>', $name));
80
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
81
            $error = true;
82
        }
83
84
        $tmpConnection->close();
85
86
        return $error ? 1 : 0;
87
    }
88
}
89