Test Failed
Branch master (c7fd68)
by Gaetano
06:34
created

DatabaseManagingCommand::createDatabases()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 38
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Db3v4l\Command;
4
5
use Db3v4l\Core\DatabaseSchemaManager;
6
7
abstract class DatabaseManagingCommand extends SQLExecutingCommand
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DatabaseManagingCommand
Loading history...
8
{
9
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
10
     * @param string[][] $instanceList
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
11
     * @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, 'password': mandatory)
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
12
     * @return array 'succeeded': int, 'failed': int, 'results': same format as dbConfigurationManager::getInstanceConfiguration
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
13
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
14
     */
15
    protected function createDatabases($instanceList, $dbSpecList)
16
    {
17
        // Sadly, psql does not allow to create a db and a user using a multiple-sql-commands string,
18
        // and we have to resort to using temp files
19
        /// @todo can we make this safer? Ideally the new user name and pwd should neither hit disk nor the process list...
20
        $results = $this->executeSqlAction(
21
            $instanceList,
22
            'Creating new database & user',
23
            function ($schemaManager, $instanceName) use ($dbSpecList) {
24
                $dbConnectionSpec = $dbSpecList[$instanceName];
25
                /** @var DatabaseSchemaManager $schemaManager */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
26
                return $schemaManager->getCreateDatabaseSqlAction(
27
                    $dbConnectionSpec['dbname'],
28
                    isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null,
29
                    isset($dbConnectionSpec['password']) ? $dbConnectionSpec['password'] : null,
30
                    (isset($dbConnectionSpec['charset']) && $dbConnectionSpec['charset'] != '') ? $dbConnectionSpec['charset'] : null
31
                );
32
            }
33
        );
34
35
        $finalData = [];
36
        foreach($results['data'] as $instanceName => $data) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
37
            $dbConnectionSpec = $dbSpecList[$instanceName];
38
            $finalData[$instanceName] = $instanceList[$instanceName];
39
            $finalData[$instanceName]['dbname'] = $dbConnectionSpec['dbname'];
40
            if (isset($dbConnectionSpec['user'])) {
41
                $finalData[$instanceName]['user'] = $dbConnectionSpec['user'];
42
            }
43
            if (isset($dbConnectionSpec['password'])) {
44
                $finalData[$instanceName]['password'] = $dbConnectionSpec['password'];
45
            }
46
            if (isset($dbConnectionSpec['charset'])) {
47
                $finalData[$instanceName]['charset'] = $dbConnectionSpec['charset'];
48
            }
49
        }
50
51
        $results['data'] = $finalData;
52
        return $results;
53
    }
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @param string[][] $instanceList
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, if unspecified assumed same as user)
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @param bool $ifExists
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
59
     * @return array 'succeeded': int, 'failed': int, 'results': string[]
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
60
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
61
     */
62
    protected function dropDatabases($instanceList, $dbSpecList, $ifExists = false)
63
    {
64
        return $this->executeSqlAction(
65
            $instanceList,
66
            'Dropping of database & user',
67
            function ($schemaManager, $instanceName) use ($dbSpecList, $ifExists) {
68
                $dbConnectionSpec = $dbSpecList[$instanceName];
69
                /** @var DatabaseSchemaManager $schemaManager */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
70
                return $schemaManager->getDropDatabaseSqlAction(
71
                    $dbConnectionSpec['dbname'],
72
                    isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null,
73
                    $ifExists
74
                );
75
            }
76
        );
77
    }
78
}
79