Completed
Branch dev (b4f2c3)
by James Ekow Abaka
03:46
created

main.php ➔ get_container_settings()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 147
rs 8
c 0
b 0
f 0

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
/*
4
 * The MIT License
5
 *
6
 * Copyright 2015 ekow.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
require __DIR__ . "/../../../../vendor/autoload.php";
28
require_once __DIR__ . "/../src/globals.php";
29
30
use clearice\argparser\ArgumentParser;
31
use clearice\io\Io;
32
use yentu\manipulators\AbstractDatabaseManipulator;
33
use ntentan\atiaa\DriverFactory;
34
use yentu\Yentu;
35
use ntentan\config\Config;
36
use yentu\commands\Command;
37
use yentu\commands\Migrate;
38
use ntentan\panie\Container;
39
use yentu\Cli;
40
41
$container = new Container();
42
$container->setup(get_container_settings());
43
$ui = $container->resolve(Cli::class);
44
$ui->run();
45
46
/**
47
 * Return the container setup.
48
 */
49
function get_container_settings() {
50
    return [
51
        Yentu::class => [Yentu::class, 'singleton' => true],
52
        Io::class => [Io::class, 'singleton' => true],
53
        Config::class => [
54
            'singleton' => true
55
        ],
56
        DriverFactory::class => [ 
57
            function($container) {
58
                return new DriverFactory($container->resolve(Config::class)->get('db'));        
59
            }, 
60
            'singleton' => true
61
        ],
62
        AbstractDatabaseManipulator::class => [
63
            function ($container) {
64
                $config = $container->resolve(Config::class)->get('db');
65
                $class = "\\yentu\\manipulators\\" . ucfirst($config['driver']);
66
                return $container->resolve($class, ['config' => $config]);
67
            },
68
            'singleton' => true
69
        ],
70
        Migrate::class => [
71
            Migrate::class,
72
            'calls' => ['setRollbackCommand']
73
        ],
74
        ArgumentParser::class => [
75
            function($container) {
76
                $io = $container->resolve(Io::class);
0 ignored issues
show
Unused Code introduced by
$io is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
77
                $argumentParser = new ArgumentParser();
78
                
79
                // Setup commands
80
                $argumentParser->addCommand(['name' => 'import', 'help' => 'import the schema of an existing database']);
81
                $argumentParser->addCommand(['name' => 'migrate', 'help' => 'run new migrations on the target database']);
82
                $argumentParser->addCommand(['name' => 'init', 'help' => 'initialize the yentu directory']);
83
                $argumentParser->addCommand(['name' => 'create', 'help' => 'create a new migration']);
84
                $argumentParser->addCommand(['name' => 'rollback', 'help' => 'rollback the previus migration which was run']);
85
                $argumentParser->addCommand(['name' => 'status', 'help' => 'display the current status of the migrations']);
86
                
87
                // Setup options
88
                $argumentParser->addOption([
89
                    'command' => 'import', 'short_name' => 'd', 'name' => 'skip-defaults',
90
                    'help' => 'do not import the default values of the columns'
91
                ]);
92
                $argumentParser->addOption([
93
                    'command' => 'init', 'short_name' => 'i', 'name' => 'interractive',
94
                    'help' => 'initialize yentu interractively'
95
                ]);
96
                $argumentParser->addOption([
97
                    'command' => 'init', 'short_name' => 'd', 'name' => 'driver',
98
                    'help' => 'database platform. Supports postgresql', 'type' => 'string'
99
                ]);
100
                $argumentParser->addOption([
101
                    'command' => 'init', 'short_name' => 'h', 'name' => 'host',
102
                    'help' => 'the hostname of the target database', 'type' => 'string'
103
                ]);
104
                $argumentParser->addOption([
105
                    'command' => 'init', 'short_name' => 'P', 'name' => 'port',
106
                    'help' => 'the port of the target database', 'type' => 'string'
107
                ]);
108
                $argumentParser->addOption([
109
                    'command' => 'init', 'short_name' => 'n', 'name' => 'dbname',
110
                    'help' => 'the name of the target database', 'type' => 'string'
111
                ]);
112
                $argumentParser->addOption([
113
                    'command' => 'init', 'short_name' => 'u', 'name' => 'user',
114
                    'help' => 'the user name on the target database', 'type' => 'string'
115
                ]);
116
                $argumentParser->addOption([
117
                    'command' => 'init', 'short_name' => 'p', 'name' => 'password',
118
                    'help' => 'the password of the user on the target database', 'type' => 'string'
119
                ]);
120
                
121
                $argumentParser->addOption([
122
                    'command' => 'migrate', 'name' => 'no-foreign-keys',
123
                    'help' => 'do not apply any database foriegn-key constraints'
124
                ]);
125
                $argumentParser->addOption([
126
                    'command' => 'migrate', 'name' => 'only-foreign-keys',
127
                    'help' => 'apply only database foreign-key constraints (fails on errors)'
128
                ]);
129
                $argumentParser->addOption([
130
                    'command' => 'migrate', 'name' => 'force-foreign-keys',
131
                    'help' => 'apply only database foreign-key constraints'
132
                ]);
133
                $argumentParser->addOption([
134
                    'command' => 'migrate', 'name' => 'dump-queries',
135
                    'help' => 'just dump the query and perform no action'
136
                ]);
137
                $argumentParser->addOption([
138
                    'command' => 'migrate', 'name' => 'dry',
139
                    'help' => 'perform a dry run. Do not alter the database in anyway'
140
                ]);
141
                $argumentParser->addOption([
142
                    'command' => 'migrate', 'name' => 'default-schema',
143
                    'type' => 'string', 'help' => 'use this as the default schema for all migrations'
144
                ]);
145
                $argumentParser->addOption([
146
                    'command' => 'migrate', 'name' => 'default-ondelete',
147
                    'help' => 'the default cascade action for foreign key deletes', 'type' => 'string'
148
                ]);
149
                $argumentParser->addOption([
150
                    'command' => 'migrate', 'name' => 'default-onupdate',
151
                    'help' => 'the default cascade action for foreign key updates', 'type' => 'string'
152
                
153
                ]);
154
                
155
                $argumentParser->addOption([
156
                    'short_name' => 'y', 'name' => 'home',
157
                    'help' => 'specifies where the yentu configurations and migrations are found',
158
                    'type' => 'string'
159
                ]);
160
                $argumentParser->addOption([
161
                    'short_name' => 'v', 'name' => 'verbose',
162
                    'help' => 'set level of verbosity. high, mid, low and none',
163
                    'type' => 'string'
164
                ]);
165
                $argumentParser->addOption(['name' => 'details', 'help' => 'show details of all migrations.', 'command' => 'status']);
166
                $argumentParser->enableHelp("Yentu database migration tool", "Report bugs on https://github.com/ntentan/yentu");            
167
168
                return $argumentParser;
169
            },
170
            'singleton' => true
171
        ], 
172
        Command::class => [
173
             function($container) {
174
                 $argumentParser = $container->resolve(ArgumentParser::class);
175
                 $arguments = $argumentParser->parse();
176
                 if(isset($arguments['__command'])) {
177
                     $commandClass = "yentu\\commands\\" . ucfirst($arguments['__command']);
178
                     $defaultHome = $arguments['home'] ?? './yentu';
179
                     $config = $container->resolve(Config::class);
180
                     $config->readPath($defaultHome . "/config/default.conf.php");
181
                     $yentu = $container->resolve(
182
                        Yentu::class, [
183
                            'migrationVariables' => $config->get('variables', []),
184
                            'otherMigrations' => $config->get('other_migrations', [])
185
                        ]
186
                    );    
187
                     $yentu->setDefaultHome($defaultHome);
188
                     return $container->resolve($commandClass);      
189
                 } else {
190
                     return null;
191
                 }
192
            }
193
        ]
194
    ];
195
}
196
197