DatabaseManagingCommand::createDatabases()   C
last analyzed

Complexity

Conditions 12
Paths 10

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 46
rs 6.9666
cc 12
nc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Bug introduced by
The type Db3v4l\Core\DatabaseSchemaManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     * @todo make it easier to report back to the caller the errors that prevented creation of any DB
16
     */
17
    protected function createDatabases($instanceList, $dbSpecList)
18
    {
19
        // Sadly, psql does not allow to create a db and a user using a multiple-sql-commands string,
20
        // and we have to resort to using temp files
21
        /// @todo can we make this safer? Ideally the new user name and pwd should neither hit disk nor the process list...
22
        $results = $this->executeSqlAction(
23
            $instanceList,
24
            'Creating new database & user',
25
            function ($schemaManager, $instanceName) use ($dbSpecList) {
26
                $dbConnectionSpec = $dbSpecList[$instanceName];
27
                /** @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...
28
                return $schemaManager->getCreateDatabaseSqlAction(
29
                    $dbConnectionSpec['dbname'],
30
                    isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null,
31
                    isset($dbConnectionSpec['password']) ? $dbConnectionSpec['password'] : null,
32
                    (isset($dbConnectionSpec['charset']) && $dbConnectionSpec['charset'] != '') ? $dbConnectionSpec['charset'] : null
33
                );
34
            }
35
        );
36
37
        /// @todo the new connection spec should be generated by the schemaManager for each instance. This would allow
38
        ///       f.e. connection specs for Oracle to be different when using PDBs vs when using schemas
39
        ///       (this could be achieved by copying the 'dbname' member over the 'servicename' one, or unsetting 'servicename'...)
40
        $finalData = [];
41
        foreach($results['data'] as $instanceName => $data) {
0 ignored issues
show
Coding Style introduced by
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
42
            // check for failure in creation of temp db
43
            // @todo how can we tell apart correctly errors that actually prevented the db from being created from other errors?
44
            if (is_array($data) && isset($data['exitcode']) && $data['exitcode'] != 0) {
45
                continue;
46
            }
47
            $dbConnectionSpec = $dbSpecList[$instanceName];
48
            $finalData[$instanceName] = $instanceList[$instanceName];
49
            $finalData[$instanceName]['dbname'] = $dbConnectionSpec['dbname'];
50
            if (isset($dbConnectionSpec['user'])) {
51
                $finalData[$instanceName]['user'] = $dbConnectionSpec['user'];
52
            }
53
            if (isset($dbConnectionSpec['password'])) {
54
                $finalData[$instanceName]['password'] = $dbConnectionSpec['password'];
55
            }
56
            if (isset($dbConnectionSpec['charset'])) {
57
                $finalData[$instanceName]['charset'] = $dbConnectionSpec['charset'];
58
            }
59
        }
60
61
        $results['data'] = $finalData;
62
        return $results;
63
    }
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @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...
67
     * @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 8 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...
68
     * @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...
69
     * @param string $executionStrategy
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Superfluous parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
70
     * @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...
71
     * @throws \Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
72
     */
73
    protected function dropDatabases($instanceList, $dbSpecList, $ifExists = false)
74
    {
75
        return $this->executeSqlAction(
76
            $instanceList,
77
            'Dropping of database & user',
78
            function ($schemaManager, $instanceName) use ($dbSpecList, $ifExists) {
79
                $dbConnectionSpec = $dbSpecList[$instanceName];
80
                /** @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...
81
                return $schemaManager->getDropDatabaseSqlAction(
82
                    $dbConnectionSpec['dbname'],
83
                    isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null,
84
                    $ifExists
85
                );
86
            }
87
        );
88
    }
89
}
90