Passed
Push — master ( 41d5a5...e82656 )
by Thierry
03:02
created

RequestHandler::onInvalid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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\Plugin\Contract\RequestHandlerInterface;
27
use Jaxon\Plugin\Manager\PluginManager;
28
use Jaxon\Response\ResponseInterface;
29
use Jaxon\Response\Manager\ResponseManager;
30
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
31
use Jaxon\Utils\Translation\Translator;
32
use Psr\Http\Message\ServerRequestInterface;
33
34
use Exception;
35
36
use function call_user_func;
37
use function call_user_func_array;
38
use function error_reporting;
39
use function ob_end_clean;
40
use function ob_get_level;
41
42
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
43
{
44
    /**
45
     * @var Container
46
     */
47
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
48
49
    /**
50
     * @var ConfigManager
51
     */
52
    protected $xConfigManager;
53
54
    /**
55
     * The plugin manager.
56
     *
57
     * @var PluginManager
58
     */
59
    private $xPluginManager;
60
61
    /**
62
     * The response manager.
63
     *
64
     * @var ResponseManager
65
     */
66
    private $xResponseManager;
67
68
    /**
69
     * The callbacks to run while processing the request
70
     *
71
     * @var CallbackManager
72
     */
73
    private $xCallbackManager;
74
75
    /**
76
     * @var UploadHandler
77
     */
78
    private $xUploadHandler;
79
80
    /**
81
     * The data bag response plugin
82
     *
83
     * @var DataBagPlugin
84
     */
85
    private $xDataBagPlugin;
86
87
    /**
88
     * @var Translator
89
     */
90
    private $xTranslator;
91
92
    /**
93
     * The request plugin that is able to process the current request
94
     *
95
     * @var RequestHandlerInterface
96
     */
97
    private $xRequestPlugin = null;
98
99
    /**
100
     * @var ServerRequestInterface
101
     */
102
    private $xRequest;
103
104
    /**
105
     * The constructor
106
     *
107
     * @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...
108
     * @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...
109
     * @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...
110
     * @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...
111
     * @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...
112
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
113
     * @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...
114
     * @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...
115
     * @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...
116
     */
117
    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...
118
        ResponseManager $xResponseManager, CallbackManager $xCallbackManager, ServerRequestInterface $xRequest,
119
        ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator)
120
    {
121
        $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...
122
        $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...
123
        $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...
124
        $this->xResponseManager = $xResponseManager;
125
        $this->xCallbackManager = $xCallbackManager;
126
        $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...
127
        $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...
128
        $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...
129
        $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...
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * These callbacks are called whenever an invalid request is processed.
134
     *
135
     * @return void
136
     */
137
    public function onBoot()
138
    {
139
        foreach($this->xCallbackManager->getBootCallbacks() as $xCallback)
140
        {
141
            call_user_func($xCallback);
142
        }
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * These are the pre-request processing callbacks passed to the Jaxon library.
147
     *
148
     * @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...
149
     *
150
     * @return void
151
     */
152
    public function onBefore(bool &$bEndRequest)
153
    {
154
        $xTarget = $this->xRequestPlugin->getTarget();
155
        // Call the user defined callback
156
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
157
        {
158
            $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]);
159
            if($xReturn instanceof ResponseInterface)
160
            {
161
                $this->xResponseManager->append($xReturn);
162
            }
163
            if($bEndRequest)
164
            {
165
                return;
166
            }
167
        }
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
171
     * These are the post-request processing callbacks passed to the Jaxon library.
172
     *
173
     * @return void
174
     */
175
    public function onAfter(bool $bEndRequest)
176
    {
177
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
178
        {
179
            $xReturn = call_user_func_array($xCallback,
180
                [$this->xRequestPlugin->getTarget(), $bEndRequest]);
181
            if($xReturn instanceof ResponseInterface)
182
            {
183
                $this->xResponseManager->append($xReturn);
184
            }
185
        }
186
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
187
188
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
189
     * These callbacks are called whenever an invalid request is processed.
190
     *
191
     * @return void
192
     */
193
    public function onInvalid(string $sMessage)
194
    {
195
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
196
        {
197
            $xReturn = call_user_func($xCallback, $sMessage);
198
            if($xReturn instanceof ResponseInterface)
199
            {
200
                $this->xResponseManager->append($xReturn);
201
            }
202
        }
203
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
204
205
    /**
206
     * These callbacks are called whenever an invalid request is processed.
207
     *
208
     * @param Exception $xException
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
209
     *
210
     * @return void
211
     * @throws RequestException
212
     */
213
    public function onError(Exception $xException)
214
    {
215
        foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback)
216
        {
217
            $xReturn = call_user_func($xCallback, $xException);
218
            if($xReturn instanceof ResponseInterface)
219
            {
220
                $this->xResponseManager->append($xReturn);
221
            }
222
        }
223
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
224
225
    /**
226
     * Check if the current request can be processed
227
     *
228
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
229
     *
230
     * @return bool
231
     */
232
    public function canProcessRequest(): bool
233
    {
234
        // Return true if the request plugin was already found
235
        if($this->xRequestPlugin !== null)
236
        {
237
            return true;
238
        }
239
240
        // Find a plugin to process the request
241
        foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
242
        {
243
            if($sClassName::canProcessRequest($this->xRequest))
244
            {
245
                $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

245
                $this->xRequestPlugin = $this->di->g(/** @scrutinizer ignore-type */ $sClassName);
Loading history...
246
                return true;
247
            }
248
        }
249
250
        // Check if the upload plugin is enabled
251
        if($this->xUploadHandler === null)
252
        {
253
            return false;
254
        }
255
256
        // If no other plugin than the upload plugin can process the request,
257
        // then it is an HTTP (not ajax) upload request
258
        $this->xUploadHandler->isHttpUpload();
259
        return $this->xUploadHandler->canProcessRequest($this->xRequest);
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * Process the current request and handle errors and exceptions.
264
     *
265
     * @return void
266
     * @throws RequestException
267
     */
268
    private function _processRequest()
269
    {
270
        // Process uploaded files, if the upload plugin is enabled
271
        if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($this->xRequest))
272
        {
273
            $this->xUploadHandler->processRequest($this->xRequest);
274
        }
275
        // Process the request
276
        if(($this->xRequestPlugin))
277
        {
278
            $xResponse = $this->xRequestPlugin->processRequest($this->xRequest);
279
            if(($xResponse))
280
            {
281
                $this->xResponseManager->append($xResponse);
282
            }
283
            // Process the databag
284
            $this->xDataBagPlugin->writeCommand();
285
        }
286
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
287
288
    /**
289
     * Clean output buffers.
290
     *
291
     * @return void
292
     */
293
    private function _cleanOutputBuffers()
294
    {
295
        $er = error_reporting(0);
296
        while(ob_get_level() > 0)
297
        {
298
            ob_end_clean();
299
        }
300
        error_reporting($er);
301
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
302
303
    /**
304
     * Process the current request.
305
     *
306
     * Calls each of the request plugins to request that they process the current request.
307
     * If any plugin processes the request, it will return true.
308
     *
309
     * @return void
310
     * @throws RequestException
311
     */
312
    public function processRequest()
313
    {
314
        // Check if there is a plugin to process this request
315
        if(!$this->canProcessRequest())
316
        {
317
            return;
318
        }
319
320
        try
321
        {
322
            $bEndRequest = false;
323
            // Handle before processing event
324
            if(($this->xRequestPlugin))
325
            {
326
                $this->onBefore($bEndRequest);
327
            }
328
            if($bEndRequest)
329
            {
330
                return;
331
            }
332
333
            $this->_processRequest();
334
335
            // Handle after processing event
336
            if(($this->xRequestPlugin))
337
            {
338
                $this->onAfter($bEndRequest);
339
            }
340
        }
341
        // An exception was thrown while processing the request.
342
        // The request missed the corresponding handler function,
343
        // or an error occurred while attempting to execute the handler.
344
        catch(RequestException $e)
345
        {
346
            $this->xResponseManager->error($e->getMessage());
347
            $this->onInvalid($e->getMessage());
348
            throw $e;
349
        }
350
        catch(Exception $e)
351
        {
352
            $this->xResponseManager->error($e->getMessage());
353
            $this->onError($e);
354
            throw $e;
355
        }
356
357
        // Print the debug messages
358
        $this->xResponseManager->printDebug();
359
        // Clean the processing buffer
360
        if(($this->xConfigManager->getOption('core.process.clean')))
361
        {
362
            $this->_cleanOutputBuffers();
363
        }
364
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
365
}
366