Tester::dbconfig()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 5
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Divergence package.
4
 *
5
 * (c) Henry Paradiz <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Divergence\CLI\Controllers\Commands;
11
12
use Divergence\CLI\Env;
13
use Divergence\CLI\Command;
14
use Divergence\CLI\Controllers\CommandLineHandler;
15
16
class Tester extends CommandLineHandler
17
{
18
    public static function handle()
19
    {
20
        switch ($action = static::shiftArgs()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
21
            case 'dbconfig':
22
                static::dbconfig();
23
            break;
24
25
            default:
26
                Basics::usage();
27
        }
28
    }
29
30
    public static function error($error)
31
    {
32
        Command::$climate->error($error);
33
    }
34
35
    public static function dbconfig()
36
    {
37
        $climate = Command::$climate;
38
39
        try {
40
            $configs = Env::getConfig(getcwd(), 'db');
41
        } catch (\Exception $e) {
42
            $climate->shout('No database config found! Are you sure this is a project folder?');
43
            return;
44
        }
45
        $labels = array_keys($configs);
46
47
        if (!$label = static::shiftArgs()) {
48
            $input = $climate->radio('Choose a config to test:', $labels);
49
            $response = $input->prompt();
50
            
51
            if (in_array($response, $labels)) {
52
                static::testDatabaseConfig($configs[$response]);
53
            }
54
        } else {
55
            if (in_array($label, $labels)) {
56
                static::testDatabaseConfig($configs[$label]);
57
            } else {
58
                $climate->yellow('No database config found with that label.');
59
            }
60
        }
61
    }
62
63
    public static function testDatabaseConfig($config)
64
    {
65
        $climate = Command::$climate;
66
        $climate->inline('Testing config.......... ');
67
        if (Database::connectionTester($config)) {
68
            $climate->green('Success.');
69
        } else {
70
            $climate->red('Failed.');
71
        }
72
    }
73
}
74