Completed
Push — master ( 807794...9b102f )
by Mihail
02:58
created

App::run()   F

Complexity

Conditions 18
Paths 804

Size

Total Lines 89
Code Lines 53

Duplication

Lines 10
Ratio 11.24 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 10
loc 89
rs 2.3577
cc 18
eloc 53
nc 804
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\Core;
4
5
use Ffcms\Core\Exception\ForbiddenException;
6
use Ffcms\Core\Exception\JsonException;
7
use Ffcms\Core\Exception\NativeException;
8
use Ffcms\Core\Exception\NotFoundException;
9
use Ffcms\Core\Exception\SyntaxException;
10
use Ffcms\Core\Managers\BootManager;
11
use Ffcms\Core\Managers\CronManager;
12
use Ffcms\Core\Managers\EventManager;
13
use Ffcms\Core\Helper\Security;
14
use Ffcms\Core\Helper\Type\Obj;
15
use Ffcms\Core\Helper\Type\Str;
16
use Ffcms\Core\I18n\Translate;
17
use Ffcms\Core\Network\Request;
18
use Ffcms\Core\Network\Response;
19
use Ffcms\Core\Arch\View;
20
use Ffcms\Core\Debug\Manager as Debug;
21
use Ffcms\Core\Cache\MemoryObject;
22
23
/**
24
 * Class App. Provide later static callbacks as entry point from any places of ffcms.
25
 * @package Ffcms\Core
26
 */
27
class App
28
{
29
    /** @var \Ffcms\Core\Network\Request */
30
    public static $Request;
31
32
    /** @var \Ffcms\Core\Properties */
33
    public static $Properties;
34
35
    /** @var \Ffcms\Core\Network\Response */
36
    public static $Response;
37
38
    /** @var \Ffcms\Core\Alias */
39
    public static $Alias;
40
41
    /** @var \Ffcms\Core\Arch\View */
42
    public static $View;
43
44
    /** @var \Ffcms\Core\Debug\Manager|null */
45
    public static $Debug;
46
47
    /** @var \Ffcms\Core\Helper\Security */
48
    public static $Security;
49
50
    /** @var \Ffcms\Core\I18n\Translate */
51
    public static $Translate;
52
53
    /** @var \Ffcms\Core\Interfaces\iUser */
54
    public static $User;
55
56
    /** @var \Symfony\Component\HttpFoundation\Session\Session */
57
    public static $Session;
58
59
    /** @var \Illuminate\Database\Capsule\Manager */
60
    public static $Database;
61
62
    /** @var \Ffcms\Core\Cache\MemoryObject */
63
    public static $Memory;
64
65
    /** @var \Swift_Mailer */
66
    public static $Mailer;
67
68
    /** @var \Ffcms\Core\Interfaces\iCaptcha */
69
    public static $Captcha;
70
71
    /** @var \phpFastCache\Drivers\files */
72
    public static $Cache;
73
74
    /** @var EventManager */
75
    public static $Event;
76
77
    /** @var CronManager */
78
    public static $Cron;
79
80
    /**
81
     * Prepare entry-point services
82
     * @param array|null $services
83
     * @param bool|object $loader
84
     * @throws NativeException
85
     * @throws \InvalidArgumentException
86
     */
87
    public static function init(array $services = null, $loader = false)
88
    {
89
        // initialize default services - used in all apps type
90
        self::$Memory = MemoryObject::instance();
91
        self::$Properties = new Properties();
92
        self::$Request = Request::createFromGlobals();
93
        self::$Security = new Security();
94
        self::$Response = new Response();
95
        self::$View = new View();
96
        self::$Translate = new Translate();
97
        self::$Alias = new Alias();
98
        self::$Event = new EventManager();
99
        self::$Cron = new CronManager();
100
101
        // check if debug is enabled and available for current session
102
        if (isset($services['Debug']) && $services['Debug'] === true && Debug::isEnabled() === true) {
103
            self::$Debug = new Debug();
104
        }
105
106
        $objects = App::$Properties->getAll('object');
107
        // pass dynamic initialization
108
        self::dynamicServicePrepare($services, $objects);
109
110
        // Initialize boot manager. This manager allow to auto-execute 'static boot()' methods in apps and widgets
111
        $bootManager = new BootManager($loader);
112
        $bootManager->run();
113
    }
114
115
    /**
116
     * Prepare dynamic services from object anonymous functions
117
     * @param array|null $services
118
     * @param null $objects
119
     * @throws NativeException
120
     */
121
    private static function dynamicServicePrepare(array $services = null, $objects = null)
122
    {
123
        // check if object configuration is passed
124
        if (!Obj::isArray($objects)) {
125
            throw new NativeException('Object configurations is not loaded: /Private/Config/Object.php');
126
        }
127
128
        // each all objects as service_name => service_instance()
129
        foreach ($objects as $name => $instance) {
0 ignored issues
show
Bug introduced by
The expression $objects of type null is not traversable.
Loading history...
130
            // check if definition of object is exist and services list contains it or is null to auto build
131
            if (property_exists(get_called_class(), $name) && $instance instanceof \Closure && (isset($services[$name]) || $services === null)) {
132
                if ($services[$name] === true || $services === null) { // initialize from configs
133
                    self::${$name} = $instance();
134
                } elseif (is_callable($services[$name])) { // raw initialization from App::run()
135
                    self::${$name} = $services[$name]();
136
                }
137
            }
138
        }
139
    }
140
141
    /**
142
     * Run applications and display output
143
     * @throws \DebugBar\DebugBarException
144
     */
145
    public static function run()
146
    {
147
        $html = null;
148
        // lets try to get html full content to page render
149
        try {
150
            /** @var \Ffcms\Core\Arch\Controller $callClass */
151
            $callClass = null;
152
            $callMethod = 'action' . self::$Request->getAction();
153
154
            // founded callback injection alias
155
            if (self::$Request->getCallbackAlias() !== false) {
156
                $cName = self::$Request->getCallbackAlias();
157 View Code Duplication
                if (class_exists($cName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
                    $callClass = new $cName;
159
                } else {
160
                    throw new NotFoundException('Callback alias of class "' . App::$Security->strip_tags($cName) . '" is not founded');
0 ignored issues
show
Documentation introduced by
$cName is of type boolean, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
161
                }
162
            } else { // typical parsing of native apps
163
                $cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController();
164
165
                // try to initialize class object
166 View Code Duplication
                if (class_exists($cName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
                    $callClass = new $cName;
168
                } else {
169
                    throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName));
170
                }
171
            }
172
173
            // try to call method of founded callback class
174
            if (method_exists($callClass, $callMethod)) {
175
                $actionQuery = [];
176
                // prepare action params for callback
177
                if (!Str::likeEmpty(self::$Request->getID())) {
178
                    $actionQuery[] = self::$Request->getID();
179
                    if (!Str::likeEmpty(self::$Request->getAdd())) {
180
                        $actionQuery[] = self::$Request->getAdd();
181
                    }
182
                }
183
184
                // get controller method arguments count
185
                $reflection = new \ReflectionMethod($callClass, $callMethod);
186
                $argumentCount = 0;
187
                foreach ($reflection->getParameters() as $arg) {
188
                    if (!$arg->isOptional()) {
189
                        $argumentCount++;
190
                    }
191
                }
192
193
                // check method arguments count and current request count to prevent warnings
194
                if (count($actionQuery) < $argumentCount) {
195
                    throw new NotFoundException(__('Arguments for method %method% is not enough. Expected: %required%, got: %current%.', [
196
                        'method' => $callMethod,
197
                        'required' => $argumentCount,
198
                        'current' => count($actionQuery)
199
                    ]));
200
                }
201
202
                // make callback call to action in controller and get response
203
                $actionResponse = call_user_func_array([$callClass, $callMethod], $actionQuery);
204
205
                if ($actionResponse !== null && !Str::likeEmpty($actionResponse)) {
206
                    // set response to controller property object
207
                    $callClass->setResponse($actionResponse);
208
                }
209
210
                // get full compiled response
211
                $html = $callClass->getOutput();
212
            } else {
213
                throw new NotFoundException('Method "' . App::$Security->strip_tags($callMethod) . '()" not founded in "' . get_class($callClass) . '"');
214
            }
215
        } catch (NotFoundException $e) { // catch exceptions and set output
216
            $html = $e->display();
217
        } catch (ForbiddenException $e) {
218
            $html = $e->display();
219
        } catch (SyntaxException $e) {
220
            $html = $e->display();
221
        } catch (JsonException $e) {
222
            $html = $e->display();
223
        } catch (NativeException $e) {
224
            $html = $e->display();
225
        } catch (\Exception $e) { // catch all other exceptions
226
            $html = (new NativeException($e->getMessage()))->display();
227
        }
228
229
        // set full rendered content to response builder
230
        self::$Response->setContent($html);
231
        // echo full response to user via http foundation
232
        self::$Response->send();
233
    }
234
235
}