Passed
Pull Request — master (#53)
by Thierry
05:08 queued 01:01
created

Handler::processArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 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
28
use Exception;
29
30
class Handler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Handler
Loading history...
31
{
32
    use \Jaxon\Features\Config;
33
34
    /**
35
     * The plugin manager.
36
     *
37
     * @var PluginManager
38
     */
39
    private $xPluginManager;
40
41
    /**
42
     * The response manager.
43
     *
44
     * @var ResponseManager
45
     */
46
    private $xResponseManager;
47
48
    /**
49
     * The arguments handler.
50
     *
51
     * @var \Jaxon\Request\Handler\Argument
52
     */
53
    private $xArgumentManager;
54
55
    /**
56
     * The callbacks to run while processing the request
57
     *
58
     * @var \Jaxon\Request\Handler\Callback
59
     */
60
    private $xCallbackManager;
61
62
    /**
63
     * The request plugin that is able to process the current request
64
     *
65
     * @var \Jaxon\Plugin\Request
66
     */
67
    private $xTargetRequestPlugin = null;
68
69
    /**
70
     * The file upload request plugin
71
     *
72
     * @var FileUpload
73
     */
74
    private $xUploadRequestPlugin = null;
75
76
    /**
77
     * The constructor
78
     *
79
     * @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...
80
     * @param ResponseManager       $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 7 found
Loading history...
81
     * @param FileUpload            $xUploadRequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 12 found
Loading history...
82
     */
83
    public function __construct(PluginManager $xPluginManager,
84
        ResponseManager $xResponseManager, FileUpload $xUploadRequestPlugin)
85
    {
86
        $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...
87
        $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...
88
        $this->xUploadRequestPlugin = $xUploadRequestPlugin;
89
90
        $this->xArgumentManager = new Argument();
91
        $this->xCallbackManager = new Callback();
92
    }
93
94
    /**
95
     * Return the method that was used to send the arguments from the client
96
     *
97
     * The method is one of: Argument::METHOD_UNKNOWN, Argument::METHOD_GET, Argument::METHOD_POST.
98
     *
99
     * @return integer
100
     */
101
    public function getRequestMethod()
102
    {
103
        return $this->xArgumentManager->getRequestMethod();
104
    }
105
106
    /**
107
     * Return true if the current request method is GET
108
     *
109
     * @return bool
110
     */
111
    public function requestMethodIsGet()
112
    {
113
        return ($this->xArgumentManager->getRequestMethod() == Argument::METHOD_GET);
114
    }
115
116
    /**
117
     * Return the array of arguments that were extracted and parsed from the GET or POST data
118
     *
119
     * @return array
120
     */
121
    public function processArguments()
122
    {
123
        return $this->xArgumentManager->process();
124
    }
125
126
    /**
127
     * Get the callback handler
128
     *
129
     * @return Callback
130
     */
131
    public function getCallbackManager()
132
    {
133
        return $this->xCallbackManager;
134
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
137
     * This is the pre-request processing callback passed to the Jaxon library.
138
     *
139
     * @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
Doc comment for parameter &$bEndRequest does not match actual variable name $bEndRequest
Loading history...
140
     *
141
     * @return Jaxon\Response\Response  the Jaxon response
0 ignored issues
show
Documentation introduced by
Should the return type not be Jaxon\Response\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
142
     */
143 View Code Duplication
    public function onBefore(&$bEndRequest)
144
    {
145
        // Call the user defined callback
146
        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...
147
        {
148
            call_user_func_array($xCallback, [$this->xTargetRequestPlugin->getTarget(), &$bEndRequest]);
149
        }
150
    }
151
152
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
153
     * This is the post-request processing callback passed to the Jaxon library.
154
     *
155
     * @return Jaxon\Response\Response  the Jaxon response
0 ignored issues
show
Documentation introduced by
Should the return type not be Jaxon\Response\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
156
     */
157 View Code Duplication
    public function onAfter($bEndRequest)
158
    {
159
        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...
160
        {
161
            call_user_func_array($xCallback, [$this->xTargetRequestPlugin->getTarget(), $bEndRequest]);
162
        }
163
    }
164
165
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sMessage should have a doc-comment as per coding-style.
Loading history...
166
     * This callback is called whenever an invalid request is processed.
167
     *
168
     * @return Jaxon\Response\Response  the Jaxon response
0 ignored issues
show
Documentation introduced by
Should the return type not be Jaxon\Response\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
169
     */
170
    public function onInvalid($sMessage)
171
    {
172
        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...
173
        {
174
            call_user_func_array($xCallback, [$sMessage]);
175
        }
176
    }
177
178
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xException 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 Jaxon\Response\Response  the Jaxon response
0 ignored issues
show
Documentation introduced by
Should the return type not be Jaxon\Response\Response|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
182
     */
183
    public function onError(Exception $xException)
184
    {
185
        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...
186
        {
187
            call_user_func_array($xCallback, [$xException]);
188
        }
189
        else
190
        {
191
            throw $xException;
192
        }
193
    }
194
195
    /**
196
     * Check if the current request can be processed
197
     *
198
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
199
     *
200
     * @return boolean
201
     */
202
    public function canProcessRequest()
203
    {
204
        // Return true if the request plugin was already found
205
        if(($this->xTargetRequestPlugin))
206
        {
207
            return true;
208
        }
209
210
        // Find a plugin to process the request
211
        foreach($this->xPluginManager->getRequestPlugins() as $xPlugin)
212
        {
213
            if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest())
214
            {
215
                $this->xTargetRequestPlugin = $xPlugin;
216
                return true;
217
            }
218
        }
219
220
        // If no other plugin than the upload plugin can process the request,
221
        // then it is a HTTP (not ajax) upload request
222
        $this->xUploadRequestPlugin->noRequestPluginFound();
223
        return $this->xUploadRequestPlugin->canProcessRequest();
224
    }
225
226
    /**
227
     * Process the current request and handle errors and exceptions.
228
     *
229
     * @return void
230
     */
231
    private function _processRequest()
232
    {
233
        try
234
        {
235
            // Process uploaded files
236
            $this->xUploadRequestPlugin->processRequest();
237
238
            // Process the request
239
            if(($this->xTargetRequestPlugin))
240
            {
241
                $this->xTargetRequestPlugin->processRequest();
242
            }
243
        }
244
        catch(Exception $e)
245
        {
246
            // An exception was thrown while processing the request.
247
            // The request missed the corresponding handler function,
248
            // or an error occurred while attempting to execute the handler.
249
250
            $this->xResponseManager->error($e->getMessage());
251
252
            if($e instanceof \Jaxon\Exception\Error)
253
            {
254
                $this->onInvalid($e->getMessage());
255
            }
256
            else
257
            {
258
                $this->onError($e);
259
            }
260
        }
261
    }
262
263
    /**
264
     * Clean output buffers.
265
     *
266
     * @return void
267
     */
268
    private function _cleanOutputBuffers()
269
    {
270
        $er = error_reporting(0);
271
        while(ob_get_level() > 0)
272
        {
273
            ob_end_clean();
274
        }
275
        error_reporting($er);
276
    }
277
278
    /**
279
     * Process the current request.
280
     *
281
     * Calls each of the request plugins to request that they process the current request.
282
     * If any plugin processes the request, it will return true.
283
     *
284
     * @return void
285
     */
286
    public function processRequest()
287
    {
288
        // Check if there is a plugin to process this request
289
        if(!$this->canProcessRequest())
290
        {
291
            return;
292
        }
293
294
        $bEndRequest = false;
295
296
        // Handle before processing event
297
        if(($this->xTargetRequestPlugin))
298
        {
299
            $this->onBefore($bEndRequest);
300
        }
301
302
        if(!$bEndRequest)
303
        {
304
            $this->_processRequest();
305
        }
306
307
        // Clean the processing buffer
308
        if(($this->getOption('core.process.clean')))
309
        {
310
            $this->_cleanOutputBuffers();
311
        }
312
313
        if(($this->xTargetRequestPlugin))
314
        {
315
            // Handle after processing event
316
            $this->onAfter($bEndRequest);
317
        }
318
319
        // If the called function returned no response, take the the global response
320
        if(!$this->xResponseManager->getResponse())
321
        {
322
            $this->xResponseManager->append(jaxon()->getResponse());
323
        }
324
325
        $this->xResponseManager->printDebug();
326
    }
327
}
328