Completed
Push — master ( 4c5e41...32a7a7 )
by Mihail
04:16
created

Console::run()   C

Complexity

Conditions 11
Paths 49

Size

Total Lines 51
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
rs 5.7333
cc 11
eloc 38
nc 49
nop 0

How to fix   Long Method    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
3
namespace Ffcms\Console;
4
5
use Ffcms\Core\Exception\NativeException;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Helper\Type\Str;
8
use Ffcms\Core\Properties;
9
use Ffcms\Console\Transfer\Input;
10
use Ffcms\Console\Transfer\Output;
11
use \Illuminate\Database\Capsule\Manager as Capsule;
12
13
class Console
14
{
15
    /** @var \Ffcms\Core\Properties */
16
	public static $Properties;
17
    /** @var \Ffcms\Console\Transfer\Input */
18
    public static $Input;
19
    /** @var \Ffcms\Console\Transfer\Output */
20
    public static $Output;
21
    /** @var \Illuminate\Database\Capsule\Manager */
22
    public static $Database;
23
24
    /**
25
     * Build console entry point
26
     * @param array|null $services
27
     * @throws \Ffcms\Core\Exception\NativeException
28
     */
29
	public static function init(array $services = null)
30
	{
31
		self::$Properties = new Properties();
32
        self::$Input = new Input();
33
        self::$Output = new Output();
34
35
        // establish database link
36
        if (Obj::isArray(self::$Properties->get('database')) && (isset($services['Database']) && $services['Database'] === true || $services === null)) {
37
            self::$Database = new Capsule;
38
            self::$Database->addConnection(self::$Properties->get('database'));
39
40
            // Make this Capsule instance available globally via static methods... (optional)
41
            self::$Database->setAsGlobal();
42
43
            // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
44
            self::$Database->bootEloquent();
45
        }
46
	}
47
48
    /**
49
     * Build console controllers.
50
     * php console.php Controller/Action index
51
     */
52
    public static function run()
53
    {
54
        global $argv;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
55
        $output = null;
56
        if (!Obj::isArray($argv) || Str::likeEmpty($argv[1])) {
57
            $output = 'Console command is unknown! Type "console main/help" to get help guide';
58
        } else {
59
            $controller_action = $argv[1];
60
            $arrInput = explode('/', $controller_action);
61
            $controller = ucfirst(strtolower($arrInput[0]));
62
            $action = ucfirst(strtolower($arrInput[1]));
63
            if($action == null) {
64
                $action = 'Index';
65
            }
66
            // set action and id
67
            $action = 'action' . $action;
68
            $id = null;
69
            if (isset($argv[2])) {
70
                $id = $argv[2];
71
            }
72
73
            try {
74
                $controller_path = '/Apps/Controller/' . env_name . '/' . $controller . '.php';
75
                if(file_exists(root . $controller_path) && is_readable(root . $controller_path)) {
76
                    include_once(root . $controller_path);
77
                    $cname = 'Apps\Controller\\' . env_name . '\\' . $controller;
78
                    if(class_exists($cname)) {
79
                        $load = new $cname;
80
                        if(method_exists($cname, $action)) {
81
                            if($id !== null) {
82
                                $output = @$load->$action($id);
83
                            } else {
84
                                $output = @$load->$action();
85
                            }
86
                        } else {
87
                            throw new NativeException('Method ' . $action . '() not founded in ' . $cname . ' in file {root}' . $controller_path);
88
                        }
89
                        unset($load);
90
                    } else {
91
                        throw new NativeException('Namespace\\Class - ' . $cname . ' not founded in {root}' . $controller_path);
92
                    }
93
                } else {
94
                    throw new NativeException('Controller not founded: {root}' . $controller_path);
95
                }
96
            } catch(NativeException $e) {
97
                $e->display($e->getMessage());
98
            }
99
        }
100
101
        return self::$Output->write($output);
102
    }
103
	
104
}