Passed
Pull Request — master (#94)
by Thierry
02:19
created

RequestHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * RequestHandler.php - Jaxon RequestPlugin Handler
5
 *
6
 * This class processes an incoming jaxon request.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
20
21
namespace Jaxon\Request\Handler;
22
23
use Jaxon\Config\ConfigManager;
24
use Jaxon\Di\Container;
25
use Jaxon\Exception\RequestException;
26
use Jaxon\Exception\SetupException;
27
use Jaxon\Plugin\Contract\RequestHandlerInterface;
28
use Jaxon\Plugin\Manager\PluginManager;
29
use Jaxon\Response\AbstractResponse;
30
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
31
use Jaxon\Response\ResponseManager;
32
use Jaxon\Utils\Translation\Translator;
33
use Psr\Http\Message\ServerRequestInterface;
34
35
use Exception;
36
37
use function call_user_func;
38
use function call_user_func_array;
39
use function error_reporting;
40
use function headers_sent;
41
use function ob_end_clean;
42
use function ob_get_level;
43
44
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
45
{
46
    /**
47
     * @var Container
48
     */
49
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
50
51
    /**
52
     * @var ConfigManager
53
     */
54
    protected $xConfigManager;
55
56
    /**
57
     * The plugin manager.
58
     *
59
     * @var PluginManager
60
     */
61
    private $xPluginManager;
62
63
    /**
64
     * The response manager.
65
     *
66
     * @var ResponseManager
67
     */
68
    private $xResponseManager;
69
70
    /**
71
     * The callbacks to run while processing the request
72
     *
73
     * @var CallbackManager
74
     */
75
    private $xCallbackManager;
76
77
    /**
78
     * @var UploadHandler
79
     */
80
    private $xUploadHandler;
81
82
    /**
83
     * The data bag response plugin
84
     *
85
     * @var DataBagPlugin
86
     */
87
    private $xDataBagPlugin;
88
89
    /**
90
     * @var Translator
91
     */
92
    private $xTranslator;
93
94
    /**
95
     * The request plugin that is able to process the current request
96
     *
97
     * @var RequestHandlerInterface
98
     */
99
    private $xRequestPlugin = null;
100
101
    /**
102
     * @var ServerRequestInterface
103
     */
104
    private $xRequest;
105
106
    /**
107
     * The constructor
108
     *
109
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
110
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
111
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
112
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
113
     * @param CallbackManager $xCallbackManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
114
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
115
     * @param UploadHandler|null $xUploadHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
116
     * @param DataBagPlugin $xDataBagPlugin
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
117
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
118
     */
119
    public function __construct(Container $di, ConfigManager $xConfigManager, PluginManager $xPluginManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
120
        ResponseManager $xResponseManager, CallbackManager $xCallbackManager, ServerRequestInterface $xRequest,
121
        ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator)
122
    {
123
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124
        $this->xConfigManager = $xConfigManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
125
        $this->xPluginManager = $xPluginManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
        $this->xResponseManager = $xResponseManager;
127
        $this->xCallbackManager = $xCallbackManager;
128
        $this->xRequest = $xRequest;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
129
        $this->xUploadHandler = $xUploadHandler;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
130
        $this->xDataBagPlugin = $xDataBagPlugin;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
131
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
132
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
133
134
    /**
135
     * These callbacks are called whenever an invalid request is processed.
136
     *
137
     * @return void
138
     */
139
    public function onBoot()
140
    {
141
        foreach($this->xCallbackManager->getBootCallbacks() as $xCallback)
142
        {
143
            call_user_func($xCallback);
144
        }
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
146
147
    /**
148
     * These are the pre-request processing callbacks passed to the Jaxon library.
149
     *
150
     * @param bool $bEndRequest    If set to true, the request processing is interrupted.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
151
     *
152
     * @return void
153
     */
154
    public function onBefore(bool &$bEndRequest)
155
    {
156
        $xTarget = $this->xRequestPlugin->getTarget();
157
        // Call the user defined callback
158
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
159
        {
160
            $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]);
161
            if($bEndRequest)
162
            {
163
                return;
164
            }
165
            if($xReturn instanceof AbstractResponse)
166
            {
167
                $this->xResponseManager->append($xReturn);
168
            }
169
        }
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
173
     * These are the post-request processing callbacks passed to the Jaxon library.
174
     *
175
     * @return void
176
     */
177
    public function onAfter(bool $bEndRequest)
178
    {
179
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
180
        {
181
            $xReturn = call_user_func_array($xCallback,
182
                [$this->xRequestPlugin->getTarget(), $bEndRequest]);
183
            if($xReturn instanceof AbstractResponse)
184
            {
185
                $this->xResponseManager->append($xReturn);
186
            }
187
        }
188
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
189
190
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
191
     * These callbacks are called whenever an invalid request is processed.
192
     *
193
     * @return void
194
     */
195
    public function onInvalid(string $sMessage)
196
    {
197
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
198
        {
199
            $xReturn = call_user_func($xCallback, $sMessage);
200
            if($xReturn instanceof AbstractResponse)
201
            {
202
                $this->xResponseManager->append($xReturn);
203
            }
204
        }
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException should have a doc-comment as per coding-style.
Loading history...
208
     * These callbacks are called whenever an invalid request is processed.
209
     *
210
     * @var Exception $xException
211
     *
212
     * @return void
213
     */
214
    public function onError(Exception $xException)
215
    {
216
        foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback)
217
        {
218
            $xReturn = call_user_func($xCallback, $xException);
219
            if($xReturn instanceof AbstractResponse)
220
            {
221
                $this->xResponseManager->append($xReturn);
222
            }
223
        }
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Check if the current request can be processed
228
     *
229
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
230
     *
231
     * @return bool
232
     */
233
    public function canProcessRequest(): bool
234
    {
235
        // Return true if the request plugin was already found
236
        if($this->xRequestPlugin !== null)
237
        {
238
            return true;
239
        }
240
241
        // Find a plugin to process the request
242
        foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
243
        {
244
            if($sClassName::canProcessRequest($this->xRequest))
245
            {
246
                $this->xRequestPlugin = $this->di->g($sClassName);
0 ignored issues
show
Bug introduced by
$sClassName of type Jaxon\Plugin\Contract\RequestHandlerInterface is incompatible with the type string expected by parameter $sClass of Jaxon\Di\Container::g(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

246
                $this->xRequestPlugin = $this->di->g(/** @scrutinizer ignore-type */ $sClassName);
Loading history...
247
                return true;
248
            }
249
        }
250
251
        // Check if the upload plugin is enabled
252
        if($this->xUploadHandler === null)
253
        {
254
            return false;
255
        }
256
257
        // If no other plugin than the upload plugin can process the request,
258
        // then it is an HTTP (not ajax) upload request
259
        $this->xUploadHandler->isHttpUpload();
260
        return $this->xUploadHandler->canProcessRequest($this->xRequest);
261
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
262
263
    /**
264
     * Process the current request and handle errors and exceptions.
265
     *
266
     * @return void
267
     * @throws RequestException
268
     * @throws SetupException
269
     */
270
    private function _processRequest()
271
    {
272
        try
273
        {
274
            $bEndRequest = false;
275
            // Handle before processing event
276
            if(($this->xRequestPlugin))
277
            {
278
                $this->onBefore($bEndRequest);
279
            }
280
            if($bEndRequest)
281
            {
282
                return;
283
            }
284
285
            // Process uploaded files, if the upload plugin is enabled
286
            if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($this->xRequest))
287
            {
288
                $this->xUploadHandler->processRequest($this->xRequest);
289
            }
290
            // Process the request
291
            if(($this->xRequestPlugin))
292
            {
293
                $this->xRequestPlugin->processRequest();
294
            }
295
296
            // Handle after processing event
297
            if(($this->xRequestPlugin))
298
            {
299
                $this->onAfter($bEndRequest);
300
            }
301
        }
302
        catch(Exception $e)
303
        {
304
            // An exception was thrown while processing the request.
305
            // The request missed the corresponding handler function,
306
            // or an error occurred while attempting to execute the handler.
307
            $this->xResponseManager->error($e->getMessage());
308
            if($e instanceof RequestException || $e instanceof SetupException)
309
            {
310
                $this->onInvalid($e->getMessage());
311
            }
312
            else
313
            {
314
                $this->onError($e);
315
            }
316
            throw $e;
317
        }
318
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
319
320
    /**
321
     * Clean output buffers.
322
     *
323
     * @return void
324
     */
325
    private function _cleanOutputBuffers()
326
    {
327
        $er = error_reporting(0);
328
        while(ob_get_level() > 0)
329
        {
330
            ob_end_clean();
331
        }
332
        error_reporting($er);
333
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
334
335
    /**
336
     * Process the current request.
337
     *
338
     * Calls each of the request plugins to request that they process the current request.
339
     * If any plugin processes the request, it will return true.
340
     *
341
     * @return void
342
     * @throws RequestException
343
     * @throws SetupException
344
     */
345
    public function processRequest()
346
    {
347
        // Check to see if headers have already been sent out, in which case we can't do our job
348
        if(headers_sent($sFilename, $nLineNumber))
349
        {
350
            echo $this->xTranslator->trans('errors.output.already-sent', [
351
                'location' => $sFilename . ':' . $nLineNumber
352
            ]), "\n", $this->xTranslator->trans('errors.output.advice');
353
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
354
        }
355
356
        // Check if there is a plugin to process this request
357
        if(!$this->canProcessRequest())
358
        {
359
            return;
360
        }
361
362
        $this->_processRequest();
363
364
        // Process the databag
365
        $this->xDataBagPlugin->writeCommand();
366
367
        // Clean the processing buffer
368
        if(($this->xConfigManager->getOption('core.process.clean')))
369
        {
370
            $this->_cleanOutputBuffers();
371
        }
372
373
        // If the called function returned no response, take the global response
374
        if(!$this->xResponseManager->getResponse())
375
        {
376
            $this->xResponseManager->append($this->di->getResponse());
377
        }
378
379
        $this->xResponseManager->printDebug();
380
381
        if(($this->xConfigManager->getOption('core.response.send')))
382
        {
383
            $this->xResponseManager->sendOutput();
384
            if(($this->xConfigManager->getOption('core.process.exit')))
385
            {
386
                exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
387
            }
388
        }
389
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
390
}
391