Passed
Push — master ( 7c83a0...cd350b )
by Thierry
02:08
created

RequestHandler::getRequestMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Jaxon;
24
use Jaxon\Plugin\PluginManager;
25
use Jaxon\Plugin\RequestPlugin;
26
use Jaxon\Response\AbstractResponse;
27
use Jaxon\Response\ResponseManager;
28
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
29
use Jaxon\Utils\Config\Config;
30
use Jaxon\Exception\RequestException;
31
32
use Exception;
33
34
use function call_user_func;
35
use function call_user_func_array;
36
use function error_reporting;
37
use function ob_end_clean;
38
use function ob_get_level;
39
40
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
41
{
42
    /**
43
     * @var Jaxon
44
     */
45
    private $jaxon;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
46
47
    /**
48
     * @var Config
49
     */
50
    protected $xConfig;
51
52
    /**
53
     * The plugin manager.
54
     *
55
     * @var PluginManager
56
     */
57
    private $xPluginManager;
58
59
    /**
60
     * The response manager.
61
     *
62
     * @var ResponseManager
63
     */
64
    private $xResponseManager;
65
66
    /**
67
     * The arguments handler.
68
     *
69
     * @var ArgumentManager
70
     */
71
    private $xArgumentManager;
72
73
    /**
74
     * The callbacks to run while processing the request
75
     *
76
     * @var CallbackManager
77
     */
78
    private $xCallbackManager;
79
80
    /**
81
     * The request plugin that is able to process the current request
82
     *
83
     * @var RequestPlugin
84
     */
85
    private $xTargetRequestPlugin = null;
86
87
    /**
88
     * The data bag response plugin
89
     *
90
     * @var DataBagPlugin
91
     */
92
    private $xDataBagPlugin = null;
93
94
    /**
95
     * The constructor
96
     *
97
     * @param Jaxon $jaxon
0 ignored issues
show
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
98
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
99
     * @param ArgumentManager $xArgument
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     * @param PluginManager  $xPluginManager
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
101
     * @param ResponseManager  $xResponseManager
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param DataBagPlugin  $xDataBagPlugin
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 2 found
Loading history...
103
     */
104
    public function __construct(Jaxon $jaxon, Config $xConfig, ArgumentManager $xArgument,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
105
        PluginManager $xPluginManager, ResponseManager $xResponseManager, DataBagPlugin $xDataBagPlugin)
106
    {
107
        $this->jaxon = $jaxon;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
108
        $this->xConfig = $xConfig;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
109
        $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...
110
        $this->xResponseManager = $xResponseManager;
111
        $this->xArgumentManager = $xArgument;
112
        $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...
113
114
        $this->xCallbackManager = new CallbackManager();
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Return the method that was used to send the arguments from the client
119
     *
120
     * The method is one of: ArgumentManager::METHOD_UNKNOWN, ArgumentManager::METHOD_GET, ArgumentManager::METHOD_POST.
121
     *
122
     * @return int
123
     */
124
    public function getRequestMethod(): int
125
    {
126
        return $this->xArgumentManager->getRequestMethod();
127
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
128
129
    /**
130
     * Return the array of arguments that were extracted and parsed from the GET or POST data
131
     *
132
     * @return array
133
     * @throws RequestException
134
     */
135
    public function processArguments(): array
136
    {
137
        return $this->xArgumentManager->process();
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * Get the callback handler
142
     *
143
     * @return CallbackManager
144
     */
145
    public function getCallbackManager(): CallbackManager
146
    {
147
        return $this->xCallbackManager;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * These callbacks are called whenever an invalid request is processed.
152
     *
153
     * @return void
154
     */
155
    public function onBoot()
156
    {
157
        foreach($this->xCallbackManager->getBootCallbacks() as $xCallback)
158
        {
159
            call_user_func($xCallback);
160
        }
161
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
162
163
    /**
164
     * These are the pre-request processing callbacks passed to the Jaxon library.
165
     *
166
     * @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...
167
     *
168
     * @return void
169
     */
170
    public function onBefore(bool &$bEndRequest)
171
    {
172
        $xTarget = $this->xTargetRequestPlugin->getTarget();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $xTarget is correct as $this->xTargetRequestPlugin->getTarget() targeting Jaxon\Plugin\RequestPlugin::getTarget() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
173
        // Call the user defined callback
174
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
175
        {
176
            $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]);
177
            if($bEndRequest)
178
            {
179
                return;
180
            }
181
            if($xReturn instanceof AbstractResponse)
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 $bEndRequest should have a doc-comment as per coding-style.
Loading history...
189
     * These are the post-request processing callbacks passed to the Jaxon library.
190
     *
191
     * @return void
192
     */
193
    public function onAfter(bool $bEndRequest)
194
    {
195
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
196
        {
197
            $xReturn = call_user_func_array($xCallback,
198
                [$this->xTargetRequestPlugin->getTarget(), $bEndRequest]);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->xTargetRequestPlugin->getTarget() targeting Jaxon\Plugin\RequestPlugin::getTarget() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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 $sMessage should have a doc-comment as per coding-style.
Loading history...
207
     * These callbacks are called whenever an invalid request is processed.
208
     *
209
     * @return void
210
     */
211
    public function onInvalid(string $sMessage)
212
    {
213
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
214
        {
215
            $xReturn = call_user_func($xCallback, $sMessage);
216
            if($xReturn instanceof AbstractResponse)
217
            {
218
                $this->xResponseManager->append($xReturn);
219
            }
220
        }
221
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
222
223
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException should have a doc-comment as per coding-style.
Loading history...
224
     * These callbacks are called whenever an invalid request is processed.
225
     *
226
     * @var Exception $xException
227
     *
228
     * @return void
229
     */
230
    public function onError(Exception $xException)
231
    {
232
        foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback)
233
        {
234
            $xReturn = call_user_func($xCallback, $xException);
235
            if($xReturn instanceof AbstractResponse)
236
            {
237
                $this->xResponseManager->append($xReturn);
238
            }
239
        }
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Check if the current request can be processed
244
     *
245
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
246
     *
247
     * @return bool
248
     */
249
    public function canProcessRequest(): bool
250
    {
251
        // Return true if the request plugin was already found
252
        if(($this->xTargetRequestPlugin))
253
        {
254
            return true;
255
        }
256
257
        // Find a plugin to process the request
258
        foreach($this->xPluginManager->getRequestPlugins() as $sClassName)
259
        {
260
            $xPlugin = $this->jaxon->di()->get($sClassName);
261
            if($xPlugin->canProcessRequest())
262
            {
263
                $this->xTargetRequestPlugin = $xPlugin;
264
                return true;
265
            }
266
        }
267
268
        // Check if the upload plugin is enabled
269
        if(!($xUploadPlugin = $this->jaxon->di()->getUploadPlugin()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
270
        {
271
            return false;
272
        }
273
274
        // If no other plugin than the upload plugin can process the request,
275
        // then it is an HTTP (not ajax) upload request
276
        $xUploadPlugin->isHttpUpload();
277
        return $xUploadPlugin->canProcessRequest();
278
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
279
280
    /**
281
     * Process the current request and handle errors and exceptions.
282
     *
283
     * @return void
284
     * @throws RequestException
285
     */
286
    private function _processRequest()
287
    {
288
        try
289
        {
290
            // Process uploaded files, if the upload plugin is enabled
291
            if(($xUploadPlugin = $this->jaxon->di()->getUploadPlugin()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
292
            {
293
                $xUploadPlugin->processRequest();
294
            }
295
296
            // Process the request
297
            if(($this->xTargetRequestPlugin))
298
            {
299
                $this->xTargetRequestPlugin->processRequest();
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)
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
     */
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
        $bEndRequest = false;
353
354
        // Handle before processing event
355
        if(($this->xTargetRequestPlugin))
356
        {
357
            $this->onBefore($bEndRequest);
358
        }
359
360
        if(!$bEndRequest)
361
        {
362
            $this->_processRequest();
363
            // Process the databag
364
            $this->xDataBagPlugin->writeCommand();
365
        }
366
367
        // Clean the processing buffer
368
        if(($this->xConfig->getOption('core.process.clean')))
369
        {
370
            $this->_cleanOutputBuffers();
371
        }
372
373
        if(($this->xTargetRequestPlugin))
374
        {
375
            // Handle after processing event
376
            $this->onAfter($bEndRequest);
377
        }
378
379
        // If the called function returned no response, take the global response
380
        if(!$this->xResponseManager->getResponse())
381
        {
382
            $this->xResponseManager->append($this->jaxon->getResponse());
383
        }
384
385
        $this->xResponseManager->printDebug();
386
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
387
}
388