Completed
Push — master ( f65284...d533b0 )
by Mihail
03:36
created

App::run()   C

Complexity

Conditions 16
Paths 228

Size

Total Lines 62
Code Lines 41

Duplication

Lines 10
Ratio 16.13 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 10
loc 62
rs 5.0058
cc 16
eloc 41
nc 228
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\Helper\FileSystem\File;
11
use Ffcms\Core\Helper\Security;
12
use Ffcms\Core\Helper\Type\Obj;
13
use Ffcms\Core\Helper\Type\Str;
14
use Ffcms\Core\I18n\Translate;
15
use Ffcms\Core\Network\Request;
16
use Ffcms\Core\Network\Response;
17
use Ffcms\Core\Arch\View;
18
use Ffcms\Core\Debug\Manager as Debug;
19
use Ffcms\Core\Cache\MemoryObject;
20
21
/**
22
 * Class App - entry point for applications
23
 * @package Ffcms\Core
24
 */
25
class App
26
{
27
28
    /** @var \Ffcms\Core\Network\Request */
29
    public static $Request;
30
31
    /** @var \Ffcms\Core\Properties */
32
    public static $Properties;
33
34
    /** @var \Ffcms\Core\Network\Response */
35
    public static $Response;
36
37
    /** @var \Ffcms\Core\Alias */
38
    public static $Alias;
39
40
    /** @var \Ffcms\Core\Arch\View */
41
    public static $View;
42
43
    /** @var \Ffcms\Core\Debug\Manager|null */
44
    public static $Debug;
45
46
    /** @var \Ffcms\Core\Helper\Security */
47
    public static $Security;
48
49
    /** @var \Ffcms\Core\I18n\Translate */
50
    public static $Translate;
51
52
    /** @var \Ffcms\Core\Interfaces\iUser */
53
    public static $User;
54
55
    /** @var \Symfony\Component\HttpFoundation\Session\Session */
56
    public static $Session;
57
58
    /** @var \Illuminate\Database\Capsule\Manager */
59
    public static $Database;
60
61
    /** @var \Ffcms\Core\Cache\MemoryObject */
62
    public static $Memory;
63
64
    /** @var \Swift_Mailer */
65
    public static $Mailer;
66
67
    /** @var \Ffcms\Core\Interfaces\iCaptcha */
68
    public static $Captcha;
69
70
    /** @var \BasePhpFastCache */
71
    public static $Cache;
72
73
    private $services;
0 ignored issues
show
Unused Code introduced by
The property $services is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
74
75
76
    /**
77
     * Prepare entry-point services
78
     * @param array|null $services
79
     * @throws NativeException
80
     */
81
    public static function init(array $services = null)
82
    {
83
        // initialize default services - used in all apps type
84
        self::$Memory = MemoryObject::instance();
85
        self::$Properties = new Properties();
86
        self::$Request = Request::createFromGlobals();
87
        self::$Security = new Security();
88
        self::$Response = new Response();
89
        self::$View = new View();
90
        self::$Translate = new Translate();
91
        self::$Alias = new Alias();
92
93
        // check if debug is enabled and available for current session
94
        if (isset($services['Debug']) && $services['Debug'] === true && Debug::isEnabled() === true) {
95
            self::$Debug = new Debug();
96
        }
97
98
        $objects = App::$Properties->getAll('object');
99
        // pass dynamic initialization
100
        self::dynamicServicePrepare($services, $objects);
101
    }
102
103
    /**
104
     * Prepare dynamic services from object anonymous functions
105
     * @param array|null $services
106
     * @param null $objects
107
     * @throws NativeException
108
     */
109
    private static function dynamicServicePrepare(array $services = null, $objects = null)
110
    {
111
        // check if object configuration is passed
112
        if (!Obj::isArray($objects)) {
113
            throw new NativeException('Object configurations is not loaded: /Private/Config/Object.php');
114
        }
115
116
        // each all objects as service_name => service_instance()
117
        foreach ($objects as $name => $instance) {
0 ignored issues
show
Bug introduced by
The expression $objects of type null is not traversable.
Loading history...
118
            // check if definition of object is exist and services list contains it or is null to auto build
119
            if (property_exists(get_called_class(), $name) && $instance instanceof \Closure && (isset($services[$name]) || $services === null)) {
120
                if ($services[$name] === true || $services === null) { // initialize from configs
121
                    self::${$name} = $instance();
122
                } elseif (is_callable($services[$name])) { // raw initialization from App::run()
123
                    self::${$name} = $services[$name]();
124
                }
125
            }
126
        }
127
    }
128
129
    /**
130
     * Run applications and display output
131
     * @throws \DebugBar\DebugBarException
132
     */
133
    public static function run()
134
    {
135
        try {
136
            $callClass = null;
137
            $callMethod = 'action' . self::$Request->getAction();
138
139
            // founded callback injection alias
140
            if (self::$Request->getCallbackAlias() !== false) {
141
                $cName = self::$Request->getCallbackAlias();
142 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...
143
                    $callClass = new $cName;
144
                } else {
145
                    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...
146
                }
147
            } else { // typical parsing of native apps
148
                $cName = '\Apps\Controller\\' . env_name . '\\' . self::$Request->getController();
149
150
                // try to initialize class object
151 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...
152
                    $callClass = new $cName;
153
                } else {
154
                    throw new NotFoundException('Application can not be runned. Initialized class not founded: ' . App::$Security->strip_tags($cName));
155
                }
156
            }
157
158
            // try to call method of founded callback class
159
            if (method_exists($callClass, $callMethod)) {
160
                $response = null;
161
                // param "id" is passed
162
                if (!Str::likeEmpty(self::$Request->getID())) {
163
                    // param "add" is passed
164
                    if (!Str::likeEmpty(self::$Request->getAdd())) {
165
                        $response = $callClass->$callMethod(self::$Request->getID(), self::$Request->getAdd());
166
                    } else {
167
                        $response = $callClass->$callMethod(self::$Request->getID());
168
                    }
169
                } else {
170
                    // no passed params is founded
171
                    $response = $callClass->$callMethod();
172
                }
173
174
                // work with returned response data
175
                if ($response !== null && Obj::isString($response) && method_exists($callClass, 'setResponse')) {
176
                    $callClass->setResponse($response);
177
                }
178
            } else {
179
                throw new NotFoundException('Method "' . $callMethod . '()" not founded in "' . get_class($callClass) . '"');
180
            }
181
        } catch (NotFoundException $e) {
182
            $e->display();
183
        } catch (ForbiddenException $e) {
184
            $e->display();
185
        } catch (SyntaxException $e) {
186
            $e->display();
187
        } catch (JsonException $e) {
188
            $e->display();
189
        } catch (NativeException $e) {
190
            $e->display();
191
        } catch (\Exception $e) { // catch all other exceptions
192
            (new NativeException($e->getMessage()))->display();
193
        }
194
    }
195
196
}