getContainerSettings()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 147
Code Lines 107

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 3
eloc 107
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 147
rs 8

How to fix   Long Method   

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
3
// Choosing the right autoload between running within a PHAR or as a plain script.
4
5
$externalAutoload = __DIR__ . "/../../../../vendor/autoload.php";
6
if (file_exists($externalAutoload)) {
7
    require $externalAutoload;
8
} else {
9
    require __DIR__ . "/../vendor/autoload.php";
10
}
11
12
use clearice\argparser\ArgumentParser;
13
use clearice\io\Io;
14
use yentu\manipulators\AbstractDatabaseManipulator;
15
use yentu\Migrations;
16
use ntentan\config\Config;
17
use yentu\commands\Migrate;
18
use ntentan\panie\Container;
19
use yentu\Cli;
0 ignored issues
show
Bug introduced by
The type yentu\Cli 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...
20
use yentu\commands\Command;
21
22
$container = new Container();
23
$container->setup(getContainerSettings());
24
$ui = $container->get(Cli::class);
25
exit($ui->run());
26
27
/**
28
 * Return the container setup.
29
 */
30
function getContainerSettings()
31
{
32
    return [
33
        '$dbConfig:array' => [
34
            function (Container $container) {
35
                $arguments = $container->get('$arguments:array');
36
                $configFile = $arguments['config-path'] ?? "yentu/yentu.ini";
37
38
                if (!file_exists($configFile)) {
39
                    $container->get(Io::class)->error("Failed to load the configuration file: $configFile\n");
40
                    exit(100);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
41
                }
42
43
                $config = parse_ini_file($configFile, true);
44
                return $config['db'] ?? [];
45
            },
46
            'singleton' => true
47
        ],
48
        '$arguments:array' => [
49
            function (Container $container) {
50
                $argumentParser = $container->get(ArgumentParser::class);
51
                return $argumentParser->parse();
52
            },
53
            'singleton' => true
54
        ],
55
        Migrations::class => [Migrations::class, 'singleton' => true],
56
        Io::class => [Io::class, 'singleton' => true],
57
        AbstractDatabaseManipulator::class => [
58
            function ($container) {
59
                $config = $container->get(Config::class)->get('db');
60
                $class = "\\yentu\\manipulators\\" . ucfirst($config['driver']);
61
                return $container->get($class, ['config' => $config]);
62
            },
63
            'singleton' => true
64
        ],
65
        Migrate::class => [
66
            Migrate::class,
67
            'calls' => ['setRollbackCommand']
68
        ],
69
        ArgumentParser::class => [
70
            function (Container $container) {
71
                $io = $container->get(Io::class);
72
                $argumentParser = new ArgumentParser();
73
74
                // Setup commands
75
                $argumentParser->addCommand(['name' => 'import', 'help' => 'import the schema of an existing database']);
76
                $argumentParser->addCommand(['name' => 'migrate', 'help' => 'run new migrations on the target database']);
77
                $argumentParser->addCommand(['name' => 'init', 'help' => 'initialize the yentu directory']);
78
                $argumentParser->addCommand(['name' => 'create', 'help' => 'create a new migration']);
79
                $argumentParser->addCommand(['name' => 'rollback', 'help' => 'rollback the previus migration which was run']);
80
                $argumentParser->addCommand(['name' => 'status', 'help' => 'display the current status of the migrations']);
81
82
                // Setup options
83
                $argumentParser->addOption([
84
                    'command' => 'import', 'short_name' => 'd', 'name' => 'skip-defaults',
85
                    'help' => 'do not import the default values of the columns'
86
                ]);
87
                $argumentParser->addOption([
88
                    'command' => 'init', 'short_name' => 'i', 'name' => 'interactive',
89
                    'help' => 'initialize yentu interactively'
90
                ]);
91
                $argumentParser->addOption([
92
                    'command' => 'init', 'short_name' => 'd', 'name' => 'driver',
93
                    'help' => 'database platform. Supports postgresql', 'type' => 'string'
94
                ]);
95
                $argumentParser->addOption([
96
                    'command' => 'init', 'short_name' => 'h', 'name' => 'host',
97
                    'help' => 'the hostname of the target database', 'type' => 'string'
98
                ]);
99
                $argumentParser->addOption([
100
                    'command' => 'init', 'short_name' => 'P', 'name' => 'port',
101
                    'help' => 'the port of the target database', 'type' => 'string'
102
                ]);
103
                $argumentParser->addOption([
104
                    'command' => 'init', 'short_name' => 'n', 'name' => 'dbname',
105
                    'help' => 'the name of the target database', 'type' => 'string'
106
                ]);
107
                $argumentParser->addOption([
108
                    'command' => 'init', 'short_name' => 'u', 'name' => 'user',
109
                    'help' => 'the user name on the target database', 'type' => 'string'
110
                ]);
111
                $argumentParser->addOption([
112
                    'command' => 'init', 'short_name' => 'p', 'name' => 'password',
113
                    'help' => 'the password of the user on the target database', 'type' => 'string'
114
                ]);
115
116
                $argumentParser->addOption([
117
                    'command' => 'migrate', 'name' => 'no-foreign-keys',
118
                    'help' => 'do not apply any database foriegn-key constraints'
119
                ]);
120
                $argumentParser->addOption([
121
                    'command' => 'migrate', 'name' => 'only-foreign-keys',
122
                    'help' => 'apply only database foreign-key constraints (fails on errors)'
123
                ]);
124
                $argumentParser->addOption([
125
                    'command' => 'migrate', 'name' => 'force-foreign-keys',
126
                    'help' => 'apply only database foreign-key constraints'
127
                ]);
128
                $argumentParser->addOption([
129
                    'command' => 'migrate', 'name' => 'dump-queries',
130
                    'help' => 'just dump the query and perform no action'
131
                ]);
132
                $argumentParser->addOption([
133
                    'command' => 'migrate', 'name' => 'dry',
134
                    'help' => 'perform a dry run. Do not alter the database in anyway'
135
                ]);
136
                $argumentParser->addOption([
137
                    'command' => 'migrate', 'name' => 'default-ondelete',
138
                    'help' => 'the default cascade action for foreign key deletes', 'type' => 'string'
139
                ]);
140
                $argumentParser->addOption([
141
                    'command' => 'migrate', 'name' => 'default-onupdate',
142
                    'help' => 'the default cascade action for foreign key updates', 'type' => 'string'
143
144
                ]);
145
                $argumentParser->addOption([
146
                    'short_name' => 'y', 'name' => 'home',
147
                    'help' => 'specifies where the yentu configurations and migrations are found',
148
                    'type' => 'string'
149
                ]);
150
                $argumentParser->addOption([
151
                    'short_name' => 'v', 'name' => 'verbose',
152
                    'help' => 'set level of verbosity. high, mid, low and none',
153
                    'type' => 'string'
154
                ]);
155
                $argumentParser->addOption([
156
                    'short_name' => 'c', 'name' => 'config-path', 'help' => 'set path to the configuration file',
157
                    'command' => ['migrate', 'init'], 'type' => 'string'
158
                ]);
159
160
                $argumentParser->addOption(['name' => 'details', 'help' => 'show details of all migrations.', 'command' => 'status']);
161
                $argumentParser->enableHelp("Yentu database migration tool", "Report bugs on https://github.com/ntentan/yentu");
162
163
                return $argumentParser;
164
            },
165
            'singleton' => true
166
        ],
167
        Command::class => [
168
            function ($container) {
169
                $arguments = $container->get('$arguments:array');
170
                if (isset($arguments['__command'])) {
171
                    $commandClass = "yentu\\commands\\" . ucfirst($arguments['__command']);
172
                    $command = $container->get($commandClass);
173
                    $command->setOptions($arguments);
174
                    return $command;
175
                } else {
176
                    return null;
177
                }
178
            }
179
        ]
180
    ];
181
}
182
183