Passed
Push — master ( 95f016...f67a13 )
by Thierry
07:34
created

Handler::_processRequest()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 31
rs 9.6111
cc 5
nc 12
nop 0
1
<?php
2
3
/**
4
 * Handler.php - Jaxon Request 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\Exception\Error;
24
use Jaxon\Jaxon;
25
use Jaxon\Plugin\Manager as PluginManager;
26
use Jaxon\Plugin\Request;
27
use Jaxon\Response\Manager as ResponseManager;
28
use Jaxon\Request\Plugin\FileUpload;
29
use Jaxon\Response\AbstractResponse;
30
31
use Exception;
32
33
class Handler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Handler
Loading history...
34
{
35
    use \Jaxon\Features\Config;
36
37
    /**
38
     * The plugin manager.
39
     *
40
     * @var PluginManager
41
     */
42
    private $xPluginManager;
43
44
    /**
45
     * The response manager.
46
     *
47
     * @var ResponseManager
48
     */
49
    private $xResponseManager;
50
51
    /**
52
     * The arguments handler.
53
     *
54
     * @var Argument
55
     */
56
    private $xArgumentManager;
57
58
    /**
59
     * The callbacks to run while processing the request
60
     *
61
     * @var Callback
62
     */
63
    private $xCallbackManager;
64
65
    /**
66
     * The request plugin that is able to process the current request
67
     *
68
     * @var Request
69
     */
70
    private $xTargetRequestPlugin = null;
71
72
    /**
73
     * The file upload request plugin
74
     *
75
     * @var FileUpload
76
     */
77
    private $xUploadRequestPlugin = null;
78
79
    /**
80
     * The constructor
81
     *
82
     * @param PluginManager         $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 9 found
Loading history...
83
     * @param ResponseManager       $xResponseManager
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
84
     * @param FileUpload            $xUploadRequestPlugin
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
85
     */
86
    public function __construct(PluginManager $xPluginManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
87
        ResponseManager $xResponseManager, FileUpload $xUploadRequestPlugin)
88
    {
89
        $this->xPluginManager = $xPluginManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
90
        $this->xResponseManager = $xResponseManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 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...
91
        $this->xUploadRequestPlugin = $xUploadRequestPlugin;
92
93
        $this->xArgumentManager = new Argument();
94
        $this->xCallbackManager = new Callback();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Jaxon\Request\Handler\Callback() of type Jaxon\Request\Handler\Callback is incompatible with the declared type callable of property $xCallbackManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
96
97
    /**
98
     * Return the method that was used to send the arguments from the client
99
     *
100
     * The method is one of: Argument::METHOD_UNKNOWN, Argument::METHOD_GET, Argument::METHOD_POST.
101
     *
102
     * @return integer
103
     */
104
    public function getRequestMethod()
105
    {
106
        return $this->xArgumentManager->getRequestMethod();
107
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
108
109
    /**
110
     * Return true if the current request method is GET
111
     *
112
     * @return bool
113
     */
114
    public function requestMethodIsGet()
115
    {
116
        return ($this->xArgumentManager->getRequestMethod() == Argument::METHOD_GET);
117
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
118
119
    /**
120
     * Return the array of arguments that were extracted and parsed from the GET or POST data
121
     *
122
     * @return array
123
     * @throws Error
124
     */
125
    public function processArguments()
126
    {
127
        $this->onBoot();
128
        return $this->xArgumentManager->process();
129
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
130
131
    /**
132
     * Get the callback handler
133
     *
134
     * @return Callback
135
     */
136
    public function getCallbackManager()
137
    {
138
        return $this->xCallbackManager;
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * These callbacks are called whenever an invalid request is processed.
143
     *
144
     * @return void
145
     */
146
    public function onBoot()
147
    {
148
        foreach($this->xCallbackManager->getBootCallbacks() as $xCallback)
149
        {
150
            call_user_func($xCallback);
151
        }
152
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
153
154
    /**
155
     * These are the pre-request processing callbacks passed to the Jaxon library.
156
     *
157
     * @param  boolean  $bEndRequest   If set to true, the request processing is interrupted.
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
158
     *
159
     * @return void
160
     */
161
    public function onBefore(&$bEndRequest)
162
    {
163
        // Call the user defined callback
164
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
165
        {
166
            $xReturn = call_user_func($xCallback,
167
                $this->xTargetRequestPlugin->getTarget(), $bEndRequest);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->xTargetRequestPlugin->getTarget() targeting Jaxon\Plugin\Request::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...
168
            if($xReturn instanceof AbstractResponse)
169
            {
170
                $this->xResponseManager->append($xReturn);
171
            }
172
        }
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
174
175
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
176
     * These are the post-request processing callbacks passed to the Jaxon library.
177
     *
178
     * @return void
179
     */
180
    public function onAfter($bEndRequest)
181
    {
182
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
183
        {
184
            $xReturn = call_user_func($xCallback,
185
                $this->xTargetRequestPlugin->getTarget(), $bEndRequest);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->xTargetRequestPlugin->getTarget() targeting Jaxon\Plugin\Request::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...
186
            if($xReturn instanceof AbstractResponse)
187
            {
188
                $this->xResponseManager->append($xReturn);
189
            }
190
        }
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
194
     * These callbacks are called whenever an invalid request is processed.
195
     *
196
     * @return void
197
     */
198
    public function onInvalid($sMessage)
199
    {
200
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
201
        {
202
            $xReturn = call_user_func($xCallback, $sMessage);
203
            if($xReturn instanceof AbstractResponse)
204
            {
205
                $this->xResponseManager->append($xReturn);
206
            }
207
        }
208
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
209
210
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException should have a doc-comment as per coding-style.
Loading history...
211
     * These callbacks are called whenever an invalid request is processed.
212
     *
213
     * @return void
214
     * @throws Exception
215
     */
216
    public function onError(Exception $xException)
217
    {
218
        foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback)
219
        {
220
            $xReturn = call_user_func($xCallback, $xException);
221
            if($xReturn instanceof AbstractResponse)
222
            {
223
                $this->xResponseManager->append($xReturn);
224
            }
225
        }
226
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
227
228
    /**
229
     * Check if the current request can be processed
230
     *
231
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
232
     *
233
     * @return boolean
234
     */
235
    public function canProcessRequest()
236
    {
237
        // Return true if the request plugin was already found
238
        if(($this->xTargetRequestPlugin))
239
        {
240
            return true;
241
        }
242
243
        // Find a plugin to process the request
244
        foreach($this->xPluginManager->getRequestPlugins() as $xPlugin)
245
        {
246
            if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest())
247
            {
248
                $this->xTargetRequestPlugin = $xPlugin;
249
                return true;
250
            }
251
        }
252
253
        // Check if the upload plugin is enabled
254
        if(!$this->getOption('core.upload.enabled'))
255
        {
256
            return false;
257
        }
258
259
        // If no other plugin than the upload plugin can process the request,
260
        // then it is a HTTP (not ajax) upload request
261
        $this->xUploadRequestPlugin->noRequestPluginFound();
262
        return $this->xUploadRequestPlugin->canProcessRequest();
263
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
264
265
    /**
266
     * Process the current request and handle errors and exceptions.
267
     *
268
     * @return void
269
     */
270
    private function _processRequest()
271
    {
272
        try
273
        {
274
            // Process uploaded files, if the upload plugin is enabled
275
            if($this->getOption('core.upload.enabled'))
276
            {
277
                $this->xUploadRequestPlugin->processRequest();
278
            }
279
280
            // Process the request
281
            if(($this->xTargetRequestPlugin))
282
            {
283
                $this->xTargetRequestPlugin->processRequest();
284
            }
285
        }
286
        catch(Exception $e)
287
        {
288
            // An exception was thrown while processing the request.
289
            // The request missed the corresponding handler function,
290
            // or an error occurred while attempting to execute the handler.
291
292
            $this->xResponseManager->error($e->getMessage());
293
294
            if($e instanceof Error)
295
            {
296
                $this->onInvalid($e->getMessage());
297
            }
298
            else
299
            {
300
                $this->onError($e);
301
            }
302
        }
303
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
304
305
    /**
306
     * Clean output buffers.
307
     *
308
     * @return void
309
     */
310
    private function _cleanOutputBuffers()
311
    {
312
        $er = error_reporting(0);
313
        while(ob_get_level() > 0)
314
        {
315
            ob_end_clean();
316
        }
317
        error_reporting($er);
318
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
319
320
    /**
321
     * Process the current request.
322
     *
323
     * Calls each of the request plugins to request that they process the current request.
324
     * If any plugin processes the request, it will return true.
325
     *
326
     * @return void
327
     */
328
    public function processRequest()
329
    {
330
        // Check if there is a plugin to process this request
331
        if(!$this->canProcessRequest())
332
        {
333
            return;
334
        }
335
336
        $bEndRequest = false;
337
338
        // Handle before processing event
339
        if(($this->xTargetRequestPlugin))
340
        {
341
            $this->onBefore($bEndRequest);
342
        }
343
344
        if(!$bEndRequest)
345
        {
346
            $this->_processRequest();
347
        }
348
349
        // Clean the processing buffer
350
        if(($this->getOption('core.process.clean')))
351
        {
352
            $this->_cleanOutputBuffers();
353
        }
354
355
        if(($this->xTargetRequestPlugin))
356
        {
357
            // Handle after processing event
358
            $this->onAfter($bEndRequest);
359
        }
360
361
        // If the called function returned no response, take the global response
362
        if(!$this->xResponseManager->getResponse())
363
        {
364
            $this->xResponseManager->append(jaxon()->getResponse());
365
        }
366
367
        $this->xResponseManager->printDebug();
368
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
369
}
370