Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Bootstrap::run()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 32
nop 0
dl 0
loc 63
rs 7.2517
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
/**
4
 * Boot.php - Jaxon application bootstrapper
5
 *
6
 * @package jaxon-core
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2019 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
12
13
namespace Jaxon\App;
14
15
use Jaxon\Utils\Config\Config;
16
17
class Bootstrap
18
{
19
    use \Jaxon\Features\Event;
20
21
    /**
22
     * The library options
23
     *
24
     * @var array
25
     */
26
    private $aLibOptions = [];
27
28
    /**
29
     * The application options
30
     *
31
     * @var array
32
     */
33
    private $aAppOptions = [];
34
35
    /**
36
     * The Ajax endpoint URI
37
     *
38
     * @var string
39
     */
40
    private $sUri = '';
41
42
    /**
43
     * The js code URI
44
     *
45
     * @var string
46
     */
47
    private $sJsUri = '';
48
49
    /**
50
     * The js code dir
51
     *
52
     * @var string
53
     */
54
    private $sJsDir = '';
55
56
    /**
57
     * Export the js code
58
     *
59
     * @var boolean
60
     */
61
    private $bExportJs = false;
62
63
    /**
64
     * Minify the js code
65
     *
66
     * @var boolean
67
     */
68
    private $bMinifyJs = false;
69
70
    /**
71
     * Set the library options
72
     *
73
     * @var array       $aLibOptions    The library options
74
     *
75
     * @return Boot
76
     */
77
    public function lib(array $aLibOptions)
78
    {
79
        $this->aLibOptions = $aLibOptions;
80
        return $this;
81
    }
82
83
    /**
84
     * Set the applications options
85
     *
86
     * @var array       $aAppOptions    The application options
87
     *
88
     * @return Boot
89
     */
90
    public function app(array $aAppOptions)
91
    {
92
        $this->aAppOptions = $aAppOptions;
93
        return $this;
94
    }
95
96
    /**
97
     * Set the ajax endpoint URI
98
     *
99
     * @var string  $sUri   The ajax endpoint URI
100
     *
101
     * @return Boot
102
     */
103
    public function uri($sUri)
104
    {
105
        $this->sUri = $sUri;
106
        return $this;
107
    }
108
109
    /**
110
     * Set the javascript code
111
     *
112
     * @var
113
     *
114
     * @return Boot
115
     */
116
    public function js($bExportJs, $sJsUri = '', $sJsDir = '', $bMinifyJs = false)
117
    {
118
        $this->sJsUri = $sJsUri;
119
        $this->sJsDir = $sJsDir;
120
        $this->bExportJs = $bExportJs;
121
        $this->bMinifyJs = $bMinifyJs;
122
        return $this;
123
    }
124
125
    /**
126
     * Set the Jaxon application options.
127
     *
128
     * @param Config        $xAppConfig        The config options
129
     *
130
     * @return void
131
     */
132
    private function setupApp($xAppConfig)
133
    {
134
        $di = jaxon()->di();
135
        // Register user functions and classes
136
        $di->getPluginManager()->registerFromConfig($xAppConfig);
137
        // Save the view namespaces
138
        $di->getViewManager()->addNamespaces($xAppConfig);
139
    }
140
141
    /**
142
     * Wraps the module/package/bundle setup method.
143
     *
144
     * @return void
145
     */
146
    public function run()
147
    {
148
        $jaxon = jaxon();
149
        $di = $jaxon->di();
150
        $app = $jaxon->app();
0 ignored issues
show
Unused Code introduced by
$app is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
        $view = $di->getViewManager();
152
153
        // Event before setting up the module
154
        $this->triggerEvent('pre.setup');
155
156
        // Add the view renderer
157
        $view->addRenderer('jaxon', function () {
158
            return new View\View();
159
        });
160
161
        // Set the pagination view namespace
162
        $view->addNamespace('pagination', '', '', 'jaxon');
163
164
        // Set the the view facade as template renderer
165
        $di->alias(TemplateRenderer::class, ViewRenderer::class);
166
        // Setup the lib config options.
167
        $di->getConfig()->setOptions($this->aLibOptions);
168
169
        // Event before the module has set the config
170
        $this->triggerEvent('pre.config');
171
172
        // Get the app config options.
173
        $xAppConfig = $di->newConfig($this->aAppOptions);
174
        $xAppConfig->setOption('options.views.default', 'default');
175
        // Setup the app.
176
        $this->setupApp($xAppConfig);
177
178
        // Event after the module has read the config
179
        $this->triggerEvent('post.config');
180
181
        // Use the Composer autoloader. It's important to call this before triggers and callbacks.
182
        // $jaxon->useComposerAutoloader();
183
        // Jaxon library settings
184
        if(!$jaxon->hasOption('js.app.export'))
185
        {
186
            $jaxon->setOption('js.app.export', $this->bExportJs);
187
        }
188
        if(!$jaxon->hasOption('js.app.minify'))
189
        {
190
            $jaxon->setOption('js.app.minify', $this->bMinifyJs);
191
        }
192
        if(!$jaxon->hasOption('js.app.uri') && $this->sJsUri != '')
193
        {
194
            $jaxon->setOption('js.app.uri', $this->sJsUri);
195
        }
196
        if(!$jaxon->hasOption('js.app.dir') && $this->sJsDir != '')
197
        {
198
            $jaxon->setOption('js.app.dir', $this->sJsDir);
199
        }
200
        // Set the request URI
201
        if(!$jaxon->hasOption('core.request.uri') && $this->sUri != '')
202
        {
203
            $jaxon->setOption('core.request.uri', $this->sUri);
204
        }
205
206
        // Event after setting up the module
207
        $this->triggerEvent('post.setup');
208
    }
209
}
210