Passed
Pull Request — master (#95)
by Thierry
02:14
created

RequestHandler::processRequest()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 26
rs 9.9666
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 ob_end_clean;
41
use function ob_get_level;
42
43
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
44
{
45
    /**
46
     * @var Container
47
     */
48
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
49
50
    /**
51
     * @var ConfigManager
52
     */
53
    protected $xConfigManager;
54
55
    /**
56
     * The plugin manager.
57
     *
58
     * @var PluginManager
59
     */
60
    private $xPluginManager;
61
62
    /**
63
     * The response manager.
64
     *
65
     * @var ResponseManager
66
     */
67
    private $xResponseManager;
68
69
    /**
70
     * The callbacks to run while processing the request
71
     *
72
     * @var CallbackManager
73
     */
74
    private $xCallbackManager;
75
76
    /**
77
     * @var UploadHandler
78
     */
79
    private $xUploadHandler;
80
81
    /**
82
     * The data bag response plugin
83
     *
84
     * @var DataBagPlugin
85
     */
86
    private $xDataBagPlugin;
87
88
    /**
89
     * @var Translator
90
     */
91
    private $xTranslator;
92
93
    /**
94
     * The request plugin that is able to process the current request
95
     *
96
     * @var RequestHandlerInterface
97
     */
98
    private $xRequestPlugin = null;
99
100
    /**
101
     * @var ServerRequestInterface
102
     */
103
    private $xRequest;
104
105
    /**
106
     * The constructor
107
     *
108
     * @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...
109
     * @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...
110
     * @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...
111
     * @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...
112
     * @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...
113
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
114
     * @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...
115
     * @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...
116
     * @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...
117
     */
118
    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...
119
        ResponseManager $xResponseManager, CallbackManager $xCallbackManager, ServerRequestInterface $xRequest,
120
        ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin, Translator $xTranslator)
121
    {
122
        $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...
123
        $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...
124
        $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...
125
        $this->xResponseManager = $xResponseManager;
126
        $this->xCallbackManager = $xCallbackManager;
127
        $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...
128
        $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...
129
        $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...
130
        $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...
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * These callbacks are called whenever an invalid request is processed.
135
     *
136
     * @return void
137
     */
138
    public function onBoot()
139
    {
140
        foreach($this->xCallbackManager->getBootCallbacks() as $xCallback)
141
        {
142
            call_user_func($xCallback);
143
        }
144
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
145
146
    /**
147
     * These are the pre-request processing callbacks passed to the Jaxon library.
148
     *
149
     * @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...
150
     *
151
     * @return void
152
     */
153
    public function onBefore(bool &$bEndRequest)
154
    {
155
        $xTarget = $this->xRequestPlugin->getTarget();
156
        // Call the user defined callback
157
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
158
        {
159
            $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]);
160
            if($bEndRequest)
161
            {
162
                return;
163
            }
164
            if($xReturn instanceof AbstractResponse)
165
            {
166
                $this->xResponseManager->append($xReturn);
167
            }
168
        }
169
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
170
171
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
172
     * These are the post-request processing callbacks passed to the Jaxon library.
173
     *
174
     * @return void
175
     */
176
    public function onAfter(bool $bEndRequest)
177
    {
178
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
179
        {
180
            $xReturn = call_user_func_array($xCallback,
181
                [$this->xRequestPlugin->getTarget(), $bEndRequest]);
182
            if($xReturn instanceof AbstractResponse)
183
            {
184
                $this->xResponseManager->append($xReturn);
185
            }
186
        }
187
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
188
189
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
190
     * These callbacks are called whenever an invalid request is processed.
191
     *
192
     * @return void
193
     */
194
    public function onInvalid(string $sMessage)
195
    {
196
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
197
        {
198
            $xReturn = call_user_func($xCallback, $sMessage);
199
            if($xReturn instanceof AbstractResponse)
200
            {
201
                $this->xResponseManager->append($xReturn);
202
            }
203
        }
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException should have a doc-comment as per coding-style.
Loading history...
207
     * These callbacks are called whenever an invalid request is processed.
208
     *
209
     * @var Exception $xException
210
     *
211
     * @return void
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 AbstractResponse)
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
     * @throws SetupException
268
     */
269
    private function _processRequest()
270
    {
271
        try
272
        {
273
            $bEndRequest = false;
274
            // Handle before processing event
275
            if(($this->xRequestPlugin))
276
            {
277
                $this->onBefore($bEndRequest);
278
            }
279
            if($bEndRequest)
280
            {
281
                return;
282
            }
283
284
            // Process uploaded files, if the upload plugin is enabled
285
            if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($this->xRequest))
286
            {
287
                $this->xUploadHandler->processRequest($this->xRequest);
288
            }
289
            // Process the request
290
            if(($this->xRequestPlugin))
291
            {
292
                $this->xRequestPlugin->processRequest();
293
            }
294
295
            // Handle after processing event
296
            if(($this->xRequestPlugin))
297
            {
298
                $this->onAfter($bEndRequest);
299
            }
300
        }
301
        catch(Exception $e)
302
        {
303
            // An exception was thrown while processing the request.
304
            // The request missed the corresponding handler function,
305
            // or an error occurred while attempting to execute the handler.
306
            $this->xResponseManager->error($e->getMessage());
307
            if($e instanceof RequestException || $e instanceof SetupException)
308
            {
309
                $this->onInvalid($e->getMessage());
310
            }
311
            else
312
            {
313
                $this->onError($e);
314
            }
315
            throw $e;
316
        }
317
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
318
319
    /**
320
     * Clean output buffers.
321
     *
322
     * @return void
323
     */
324
    private function _cleanOutputBuffers()
325
    {
326
        $er = error_reporting(0);
327
        while(ob_get_level() > 0)
328
        {
329
            ob_end_clean();
330
        }
331
        error_reporting($er);
332
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
333
334
    /**
335
     * Process the current request.
336
     *
337
     * Calls each of the request plugins to request that they process the current request.
338
     * If any plugin processes the request, it will return true.
339
     *
340
     * @return void
341
     * @throws RequestException
342
     * @throws SetupException
343
     */
344
    public function processRequest()
345
    {
346
        // Check if there is a plugin to process this request
347
        if(!$this->canProcessRequest())
348
        {
349
            return;
350
        }
351
352
        $this->_processRequest();
353
354
        // Process the databag
355
        $this->xDataBagPlugin->writeCommand();
356
357
        // Clean the processing buffer
358
        if(($this->xConfigManager->getOption('core.process.clean')))
359
        {
360
            $this->_cleanOutputBuffers();
361
        }
362
363
        // If the called function returned no response, take the global response
364
        if(!$this->xResponseManager->getResponse())
365
        {
366
            $this->xResponseManager->append($this->di->getResponse());
367
        }
368
369
        $this->xResponseManager->printDebug();
370
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
371
}
372