Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

Handler::getRequestMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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\Jaxon;
24
use Jaxon\Plugin\Manager as PluginManager;
25
use Jaxon\Response\Manager as ResponseManager;
26
use Jaxon\Request\Plugin\FileUpload;
27
use Jaxon\Response\AbstractResponse;
28
29
use Exception;
30
31
class Handler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Handler
Loading history...
32
{
33
    use \Jaxon\Features\Config;
34
35
    /**
36
     * The plugin manager.
37
     *
38
     * @var PluginManager
39
     */
40
    private $xPluginManager;
41
42
    /**
43
     * The response manager.
44
     *
45
     * @var ResponseManager
46
     */
47
    private $xResponseManager;
48
49
    /**
50
     * The arguments handler.
51
     *
52
     * @var \Jaxon\Request\Handler\Argument
53
     */
54
    private $xArgumentManager;
55
56
    /**
57
     * The callbacks to run while processing the request
58
     *
59
     * @var \Jaxon\Request\Handler\Callback
60
     */
61
    private $xCallbackManager;
62
63
    /**
64
     * The request plugin that is able to process the current request
65
     *
66
     * @var \Jaxon\Plugin\Request
67
     */
68
    private $xTargetRequestPlugin = null;
69
70
    /**
71
     * The file upload request plugin
72
     *
73
     * @var FileUpload
74
     */
75
    private $xUploadRequestPlugin = null;
76
77
    /**
78
     * The constructor
79
     *
80
     * @param PluginManager         $xPluginManager
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @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...
82
     * @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...
83
     */
84
    public function __construct(PluginManager $xPluginManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
85
        ResponseManager $xResponseManager, FileUpload $xUploadRequestPlugin)
86
    {
87
        $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...
88
        $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...
89
        $this->xUploadRequestPlugin = $xUploadRequestPlugin;
90
91
        $this->xArgumentManager = new Argument();
92
        $this->xCallbackManager = new Callback();
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * Return the method that was used to send the arguments from the client
97
     *
98
     * The method is one of: Argument::METHOD_UNKNOWN, Argument::METHOD_GET, Argument::METHOD_POST.
99
     *
100
     * @return integer
101
     */
102
    public function getRequestMethod()
103
    {
104
        return $this->xArgumentManager->getRequestMethod();
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Return true if the current request method is GET
109
     *
110
     * @return bool
111
     */
112
    public function requestMethodIsGet()
113
    {
114
        return ($this->xArgumentManager->getRequestMethod() == Argument::METHOD_GET);
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Return the array of arguments that were extracted and parsed from the GET or POST data
119
     *
120
     * @return array
121
     */
122
    public function processArguments()
123
    {
124
        return $this->xArgumentManager->process();
125
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
126
127
    /**
128
     * Get the callback handler
129
     *
130
     * @return Callback
131
     */
132
    public function getCallbackManager()
133
    {
134
        return $this->xCallbackManager;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->xCallbackManager returns the type Jaxon\Request\Handler\Callback which is incompatible with the documented return type callable.
Loading history...
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * This is the pre-request processing callback passed to the Jaxon library.
139
     *
140
     * @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 name; 3 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
141
     *
142
     * @return void
143
     */
144
    public function onBefore(&$bEndRequest)
145
    {
146
        // Call the user defined callback
147
        if(!($xCallback = $this->xCallbackManager->before()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
148
        {
149
            return;
150
        }
151
        $xReturn = call_user_func_array($xCallback,
152
            [$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...
153
        if($xReturn instanceof AbstractResponse)
154
        {
155
            $this->xResponseManager->append($xReturn);
156
        }
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
160
     * This is the post-request processing callback passed to the Jaxon library.
161
     *
162
     * @return void
163
     */
164
    public function onAfter($bEndRequest)
165
    {
166
        if(!($xCallback = $this->xCallbackManager->after()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
167
        {
168
            return;
169
        }
170
        $xReturn = call_user_func_array($xCallback,
171
            [$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...
172
        if($xReturn instanceof AbstractResponse)
173
        {
174
            $this->xResponseManager->append($xReturn);
175
        }
176
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
177
178
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
179
     * This callback is called whenever an invalid request is processed.
180
     *
181
     * @return void
182
     */
183
    public function onInvalid($sMessage)
184
    {
185
        if(!($xCallback = $this->xCallbackManager->invalid()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
186
        {
187
            return;
188
        }
189
        $xReturn = call_user_func_array($xCallback, [$sMessage]);
190
        if($xReturn instanceof AbstractResponse)
191
        {
192
            $this->xResponseManager->append($xReturn);
193
        }
194
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
195
196
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException should have a doc-comment as per coding-style.
Loading history...
197
     * This callback is called whenever an invalid request is processed.
198
     *
199
     * @return void
200
     * @throws Exception
201
     */
202
    public function onError(Exception $xException)
203
    {
204
        if(!($xCallback = $this->xCallbackManager->error()))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
205
        {
206
            throw $xException;
207
        }
208
        $xReturn = call_user_func_array($xCallback, [$xException]);
209
        if($xReturn instanceof AbstractResponse)
210
        {
211
            $this->xResponseManager->append($xReturn);
212
        }
213
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
214
215
    /**
216
     * Check if the current request can be processed
217
     *
218
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
219
     *
220
     * @return boolean
221
     */
222
    public function canProcessRequest()
223
    {
224
        // Return true if the request plugin was already found
225
        if(($this->xTargetRequestPlugin))
226
        {
227
            return true;
228
        }
229
230
        // Find a plugin to process the request
231
        foreach($this->xPluginManager->getRequestPlugins() as $xPlugin)
232
        {
233
            if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest())
234
            {
235
                $this->xTargetRequestPlugin = $xPlugin;
236
                return true;
237
            }
238
        }
239
240
        // Check if the upload plugin is enabled
241
        if(!$this->getOption('core.upload.enabled'))
242
        {
243
            return false;
244
        }
245
246
        // If no other plugin than the upload plugin can process the request,
247
        // then it is a HTTP (not ajax) upload request
248
        $this->xUploadRequestPlugin->noRequestPluginFound();
249
        return $this->xUploadRequestPlugin->canProcessRequest();
250
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
251
252
    /**
253
     * Process the current request and handle errors and exceptions.
254
     *
255
     * @return void
256
     */
257
    private function _processRequest()
258
    {
259
        try
260
        {
261
            // Process uploaded files, if the upload plugin is enabled
262
            if($this->getOption('core.upload.enabled'))
263
            {
264
                $this->xUploadRequestPlugin->processRequest();
265
            }
266
267
            // Process the request
268
            if(($this->xTargetRequestPlugin))
269
            {
270
                $this->xTargetRequestPlugin->processRequest();
271
            }
272
        }
273
        catch(Exception $e)
274
        {
275
            // An exception was thrown while processing the request.
276
            // The request missed the corresponding handler function,
277
            // or an error occurred while attempting to execute the handler.
278
279
            $this->xResponseManager->error($e->getMessage());
280
281
            if($e instanceof \Jaxon\Exception\Error)
282
            {
283
                $this->onInvalid($e->getMessage());
284
            }
285
            else
286
            {
287
                $this->onError($e);
288
            }
289
        }
290
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
291
292
    /**
293
     * Clean output buffers.
294
     *
295
     * @return void
296
     */
297
    private function _cleanOutputBuffers()
298
    {
299
        $er = error_reporting(0);
300
        while(ob_get_level() > 0)
301
        {
302
            ob_end_clean();
303
        }
304
        error_reporting($er);
305
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
306
307
    /**
308
     * Process the current request.
309
     *
310
     * Calls each of the request plugins to request that they process the current request.
311
     * If any plugin processes the request, it will return true.
312
     *
313
     * @return void
314
     */
315
    public function processRequest()
316
    {
317
        // Check if there is a plugin to process this request
318
        if(!$this->canProcessRequest())
319
        {
320
            return;
321
        }
322
323
        $bEndRequest = false;
324
325
        // Handle before processing event
326
        if(($this->xTargetRequestPlugin))
327
        {
328
            $this->onBefore($bEndRequest);
329
        }
330
331
        if(!$bEndRequest)
332
        {
333
            $this->_processRequest();
334
        }
335
336
        // Clean the processing buffer
337
        if(($this->getOption('core.process.clean')))
338
        {
339
            $this->_cleanOutputBuffers();
340
        }
341
342
        if(($this->xTargetRequestPlugin))
343
        {
344
            // Handle after processing event
345
            $this->onAfter($bEndRequest);
346
        }
347
348
        // If the called function returned no response, take the the global response
349
        if(!$this->xResponseManager->getResponse())
350
        {
351
            $this->xResponseManager->append(jaxon()->getResponse());
352
        }
353
354
        $this->xResponseManager->printDebug();
355
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
356
}
357