CreateDatabaseCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 104
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 21 2
A getParameters() 0 10 2
A getName() 0 12 3
A createDatabase() 0 22 3
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
    private $connection;
26
27
    /**
28
     * @param Connection $connection
29
     */
30 5
    public function __construct(Connection $connection)
31
    {
32 5
        $this->connection = $connection;
33 5
    }
34
35
    /**
36
     * @param InputInterface  $input
37
     * @param OutputInterface $output
38
     *
39
     * @return int|null
40
     */
41 5
    public function __invoke(InputInterface $input, OutputInterface $output)
42
    {
43 5
        $parameters = $this->getParameters();
44
45 5
        $name = $this->getName($parameters);
46
47 4
        $isPath = isset($parameters['path']);
48
49
        // Need to get rid of _every_ occurrence of dbname from connection configuration
50 4
        unset($parameters['dbname'], $parameters['path'], $parameters['url']);
51
52 4
        $tmpConnection = DriverManager::getConnection($parameters);
53 4
        $shouldNotCreateDatabase = in_array($name, $tmpConnection->getSchemaManager()->listDatabases());
54
55
        // Only quote if we don't have a path
56 4
        if (!$isPath) {
57 3
            $name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name);
58
        }
59
60 4
        return $this->createDatabase($output, $tmpConnection, $name, $shouldNotCreateDatabase);
61
    }
62
63
    /**
64
     * @return array
65
     */
66 5
    private function getParameters(): array
67
    {
68 5
        $params = $this->connection->getParams();
69
70 5
        if (isset($params['master'])) {
71 5
            $params = $params['master'];
72
        }
73
74 5
        return $params;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 5
    private function getName(array $parameters): string
81
    {
82 5
        if (isset($parameters['path'])) {
83 1
            return $parameters['path'];
84
        }
85
86 4
        if (isset($parameters['dbname'])) {
87 3
            return $parameters['dbname'];
88
        }
89
90 1
        throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter.");
91
    }
92
93
    /**
94
     * @param OutputInterface $output
95
     * @param Connection      $tmpConnection
96
     * @param string          $name
97
     * @param bool            $shouldNotCreateDatabase
98
     *
99
     * @return int
100
     */
101 4
    private function createDatabase(
102
        OutputInterface $output,
103
        Connection $tmpConnection,
104
        string $name,
105
        bool $shouldNotCreateDatabase
106
    ): int {
107
        try {
108 4
            if ($shouldNotCreateDatabase) {
109 1
                $output->writeln(sprintf('<info>Database <comment>%s</comment> already exists.</info>', $name));
110
            } else {
111 3
                $tmpConnection->getSchemaManager()->createDatabase($name);
112 2
                $output->writeln(sprintf('<info>Created database <comment>%s</comment>', $name));
113
            }
114
115 3
            return 0;
116 1
        } catch (\Exception $e) {
117 1
            $output->writeln(sprintf('<error>Could not create database <comment>%s</comment></error>', $name));
118 1
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
119
120 1
            return 1;
121
        }
122
    }
123
}
124