Passed
Push — master ( 9017c9...c1ab88 )
by Darío
03:58
created

Application::__construct()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 56
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 12
nop 1
dl 0
loc 56
rs 8.6186
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
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
use Drone\FileSystem\Shell;
14
15
/**
16
 * Application class
17
 *
18
 * This is the main class for mvc pattern
19
 */
20
class Application
21
{
22
    /**
23
     * List of modules available
24
     *
25
     * @var array
26
     */
27
    private $modules;
28
29
    /**
30
     * The router instance
31
     *
32
     * @var Router
33
     */
34
    private $router;
35
36
    /**
37
     * Development or production mode
38
     *
39
     * @var boolean
40
     */
41
    private $devMode;
42
43
    /**
44
     * Returns the router instance
45
     *
46
     * @return Router
47
     */
48
    public function getRouter()
49
    {
50
        return $this->router;
51
    }
52
53
    /**
54
     * Prepares the app environment
55
     *
56
     * @return null
57
     */
58
    public function prepare()
59
    {
60
        # start sessions
61
        if (!isset($_SESSION))
62
            session_start();
63
    }
64
65
    /**
66
     * Constructor
67
     *
68
     * @param array $init_parameters
69
     */
70
    public function __construct($init_parameters)
71
    {
72
        $this->prepare();
73
74
        $this->devMode = $init_parameters["environment"]["dev_mode"];
75
        $this->modules = $init_parameters["modules"];
76
77
        /*
78
         *  DEV MODE:
79
         *  Set Development or production environment
80
         */
81
82
        if ($this->devMode)
83
        {
84
            ini_set('display_errors', 1);
85
86
            // See errors
87
            // error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
88
89
            // PHP 5.4
90
            // error_reporting(E_ALL);
91
92
            // Best way to view all possible errors
93
            error_reporting(-1);
94
        }
95
        else {
96
            ini_set('display_errors', 0);
97
            error_reporting(-1);
98
        }
99
100
        $this->loadModules($this->modules);
101
102
        $this->router = new Router($init_parameters["router"]["routes"]);
103
        $this->router->setBasePath($init_parameters["environment"]["base_path"]);
104
105
        # load routes from application.config.php
106
        if (file_exists("config/application.config.php"))
107
        {
108
            $app_config_file = require "config/application.config.php";
109
110
            foreach ($app_config_file["router"]["routes"] as $name => $route)
111
            {
112
                if ($route instanceof \Zend\Router\Http\RouteInterface)
113
                    $this->getRouter()->addZendRoute($name, $route);
114
                else
115
                    $this->getRouter()->addRoute($route);
116
            }
117
        }
118
119
        # load routes from modules
120
        foreach ($this->modules as $module)
121
        {
122
            if (file_exists("module/$module/config/module.config.php"))
123
            {
124
                $module_config_file = require "module/$module/config/module.config.php";
125
                $this->getRouter()->addRoute($module_config_file["router"]["routes"]);
126
            }
127
        }
128
    }
129
130
    /**
131
     * Loads module classes and autoloading functions
132
     *
133
     * @param array $modules
134
     *
135
     * @throws RuntimeException
136
     *
137
     * @return null
138
     */
139
    private function loadModules($modules)
140
    {
141
        if (count($modules))
142
        {
143
            foreach ($modules as $module)
144
            {
145
                /*
146
                 *  This instruction include each module declared in application.config.php
147
                 *  Each module has an autoloader to load its classes (controllers and models)
148
                 */
149
                if (file_exists("module/".$module."/Module.php"))
150
                    include("module/".$module."/Module.php");
151
152
                spl_autoload_register($module . "\Module::loader");
153
            }
154
        }
155
        else
156
            throw new \RuntimeException("The application must have at least one module");
157
    }
158
159
    /**
160
     * Runs the application
161
     *
162
     * @return null
163
     */
164
    public function run()
165
    {
166
        $module = isset($_GET["module"]) ? $_GET["module"] : null;
167
        $controller = isset($_GET["controller"]) ? $_GET["controller"] : null;
168
        $view = isset($_GET["view"]) ? $_GET["view"] : null;
169
170
        $request = new  \Zend\Http\Request();
171
172
        # build URI
173
        $uri = '';
174
        $uri .= !empty($module) ? '/' . $module : "";
175
        $uri .= !empty($controller) ? '/' . $controller : "";
176
        $uri .= !empty($view) ? '/' . $view : "";
177
178
        if (empty($uri))
179
            $uri = "/";
180
181
        $request->setUri($uri);
182
183
        $match = $this->router->getZendRouter()->match($request);
184
185
        if (!is_null($match))
186
        {
187
            $params = $match->getParams();
188
            $parts = explode("\\", $params["controller"]);
189
            $module = $parts[0];
190
            $controller = $parts[2];
191
            $view = $params["action"];
192
193
            $this->router->setIdentifiers($module, $controller, $view);
194
        }
195
        else
196
            $this->router->setIdentifiers($module, $controller, $view);
197
198
        $this->router->run();
199
    }
200
}