Completed
Push — master ( 4dd0e2...4c5e41 )
by Mihail
10:59
created

App::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 App
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
     */
28
	public static function init(array $services = null)
29
	{
30
		self::$Properties = new Properties();
31
        self::$Input = new Input();
32
        self::$Output = new Output();
33
34
        // establish database link
35
        if (Obj::isArray(self::$Properties->get('database')) && (isset($services['Database']) && $services['Database'] === true || $services === null)) {
36
            self::$Database = new Capsule;
37
            self::$Database->addConnection(self::$Properties->get('database'));
38
39
            // Make this Capsule instance available globally via static methods... (optional)
40
            self::$Database->setAsGlobal();
41
42
            // Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
43
            self::$Database->bootEloquent();
44
        }
45
	}
46
47
    /**
48
     * Build console controllers.
49
     * php console.php Controller/Action index
50
     */
51
    public static function run()
52
    {
53
        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...
54
        $output = null;
55
        if (!Obj::isArray($argv) || Str::likeEmpty($argv[1])) {
56
            $output = 'Console command is unknown! Type "console main/help" to get help guide';
57
        } else {
58
            $controller_action = $argv[1];
59
            $arrInput = explode('/', $controller_action);
60
            $controller = ucfirst(strtolower($arrInput[0]));
61
            $action = ucfirst(strtolower($arrInput[1]));
62
            if($action == null) {
63
                $action = 'Index';
64
            }
65
            // set action and id
66
            $action = 'action' . $action;
67
            $id = null;
68
            if (isset($argv[2])) {
69
                $id = $argv[2];
70
            }
71
72
            try {
73
                $controller_path = '/Apps/Controller/' . env_name . '/' . $controller . '.php';
74
                if(file_exists(root . $controller_path) && is_readable(root . $controller_path)) {
75
                    include_once(root . $controller_path);
76
                    $cname = 'Apps\\Controller\\' . env_name . '\\' . $controller;
77
                    if(class_exists($cname)) {
78
                        $load = new $cname;
79
                        if(method_exists($cname, $action)) {
80
                            if($id !== null) {
81
                                $output = @$load->$action($id);
82
                            } else {
83
                                $output = @$load->$action();
84
                            }
85
                        } else {
86
                            throw new NativeException('Method ' . $action . '() not founded in ' . $cname . ' in file {root}' . $controller_path);
87
                        }
88
                        unset($load);
89
                    } else {
90
                        throw new NativeException('Namespace\\Class - ' . $cname . ' not founded in {root}' . $controller_path);
91
                    }
92
                } else {
93
                    throw new NativeException('Controller not founded: {root}' . $controller_path);
94
                }
95
            } catch(NativeException $e) {
96
                $e->display($e->getMessage());
97
            }
98
        }
99
100
        return App::$Output->write($output);
101
    }
102
	
103
}