Passed
Push — master ( e82656...5198fb )
by Thierry
03:06
created

RequestHandler::onBefore()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 5
nop 1
dl 0
loc 14
rs 10
c 1
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\Di\Container;
24
use Jaxon\Exception\RequestException;
25
use Jaxon\Plugin\Contract\RequestHandlerInterface;
26
use Jaxon\Plugin\Manager\PluginManager;
27
use Jaxon\Response\ResponseInterface;
28
use Jaxon\Response\Manager\ResponseManager;
29
use Jaxon\Response\Plugin\DataBag\DataBagPlugin;
30
31
use Exception;
32
33
use function call_user_func;
34
use function call_user_func_array;
35
36
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
37
{
38
    /**
39
     * @var Container
40
     */
41
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
42
43
    /**
44
     * The plugin manager.
45
     *
46
     * @var PluginManager
47
     */
48
    private $xPluginManager;
49
50
    /**
51
     * The response manager.
52
     *
53
     * @var ResponseManager
54
     */
55
    private $xResponseManager;
56
57
    /**
58
     * The callbacks to run while processing the request
59
     *
60
     * @var CallbackManager
61
     */
62
    private $xCallbackManager;
63
64
    /**
65
     * @var UploadHandler
66
     */
67
    private $xUploadHandler;
68
69
    /**
70
     * The data bag response plugin
71
     *
72
     * @var DataBagPlugin
73
     */
74
    private $xDataBagPlugin;
75
76
    /**
77
     * The request plugin that is able to process the current request
78
     *
79
     * @var RequestHandlerInterface
80
     */
81
    private $xRequestPlugin = null;
82
83
    /**
84
     * The constructor
85
     *
86
     * @param Container $di
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...
87
     * @param PluginManager $xPluginManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
88
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
89
     * @param CallbackManager $xCallbackManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
90
     * @param UploadHandler|null $xUploadHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
91
     * @param DataBagPlugin $xDataBagPlugin
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
92
     */
93
    public function __construct(Container $di, PluginManager $xPluginManager, ResponseManager $xResponseManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
94
        CallbackManager $xCallbackManager, ?UploadHandler $xUploadHandler, DataBagPlugin $xDataBagPlugin)
95
    {
96
        $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...
97
        $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...
98
        $this->xResponseManager = $xResponseManager;
99
        $this->xCallbackManager = $xCallbackManager;
100
        $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...
101
        $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...
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * These are the pre-request processing callbacks passed to the Jaxon library.
106
     *
107
     * @param bool $bEndRequest If set to true, the request processing is interrupted.
108
     *
109
     * @return void
110
     * @throws RequestException
111
     */
112
    public function onBefore(bool &$bEndRequest)
113
    {
114
        $xTarget = $this->xRequestPlugin->getTarget();
115
        // Call the user defined callback
116
        foreach($this->xCallbackManager->getBeforeCallbacks() as $xCallback)
117
        {
118
            $xReturn = call_user_func_array($xCallback, [$xTarget, &$bEndRequest]);
119
            if($xReturn instanceof ResponseInterface)
120
            {
121
                $this->xResponseManager->append($xReturn);
122
            }
123
            if($bEndRequest)
124
            {
125
                return;
126
            }
127
        }
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $bEndRequest should have a doc-comment as per coding-style.
Loading history...
131
     * These are the post-request processing callbacks passed to the Jaxon library.
132
     *
133
     * @return void
134
     * @throws RequestException
135
     */
136
    public function onAfter(bool $bEndRequest)
137
    {
138
        foreach($this->xCallbackManager->getAfterCallbacks() as $xCallback)
139
        {
140
            $xReturn = call_user_func_array($xCallback,
141
                [$this->xRequestPlugin->getTarget(), $bEndRequest]);
142
            if($xReturn instanceof ResponseInterface)
143
            {
144
                $this->xResponseManager->append($xReturn);
145
            }
146
        }
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * These callbacks are called whenever an invalid request is processed.
151
     *
152
     * @param RequestException $xException
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
153
     *
154
     * @return void
155
     * @throws RequestException
156
     */
157
    public function onInvalid(RequestException $xException)
158
    {
159
        foreach($this->xCallbackManager->getInvalidCallbacks() as $xCallback)
160
        {
161
            $xReturn = call_user_func($xCallback, $xException);
162
            if($xReturn instanceof ResponseInterface)
163
            {
164
                $this->xResponseManager->append($xReturn);
165
            }
166
        }
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * These callbacks are called whenever an invalid request is processed.
171
     *
172
     * @param Exception $xException
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
173
     *
174
     * @return void
175
     * @throws RequestException
176
     */
177
    public function onError(Exception $xException)
178
    {
179
        foreach($this->xCallbackManager->getErrorCallbacks() as $xCallback)
180
        {
181
            $xReturn = call_user_func($xCallback, $xException);
182
            if($xReturn instanceof ResponseInterface)
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
    /**
190
     * Check if the current request can be processed
191
     *
192
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
193
     *
194
     * @return bool
195
     */
196
    public function canProcessRequest(): bool
197
    {
198
        // Return true if the request plugin was already found
199
        if($this->xRequestPlugin !== null)
200
        {
201
            return true;
202
        }
203
204
        // The HTTP request
205
        $xRequest = $this->di->getRequest();
206
        // Find a plugin to process the request
207
        foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
208
        {
209
            if($sClassName::canProcessRequest($xRequest))
210
            {
211
                $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

211
                $this->xRequestPlugin = $this->di->g(/** @scrutinizer ignore-type */ $sClassName);
Loading history...
212
                $this->xRequestPlugin->setTarget($xRequest);
213
                return true;
214
            }
215
        }
216
217
        // Check if the upload plugin is enabled
218
        if($this->xUploadHandler === null)
219
        {
220
            return false;
221
        }
222
223
        // If no other plugin than the upload plugin can process the request,
224
        // then it is an HTTP (not ajax) upload request
225
        $this->xUploadHandler->isHttpUpload();
226
        return $this->xUploadHandler->canProcessRequest($xRequest);
227
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
228
229
    /**
230
     * Process the current request and handle errors and exceptions.
231
     *
232
     * @return void
233
     * @throws RequestException
234
     */
235
    private function _processRequest()
236
    {
237
        // The HTTP request
238
        $xRequest = $this->di->getRequest();
239
        // Process uploaded files, if the upload plugin is enabled
240
        if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($xRequest))
241
        {
242
            $this->xUploadHandler->processRequest($xRequest);
243
        }
244
        // Process the request
245
        if(($this->xRequestPlugin))
246
        {
247
            $xResponse = $this->xRequestPlugin->processRequest();
248
            if(($xResponse))
249
            {
250
                $this->xResponseManager->append($xResponse);
251
            }
252
            // Process the databag
253
            $this->xDataBagPlugin->writeCommand();
254
        }
255
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
256
257
    /**
258
     * Process the current request.
259
     *
260
     * Calls each of the request plugins to request that they process the current request.
261
     * If any plugin processes the request, it will return true.
262
     *
263
     * @return void
264
     * @throws RequestException
265
     */
266
    public function processRequest()
267
    {
268
        // Check if there is a plugin to process this request
269
        if(!$this->canProcessRequest())
270
        {
271
            return;
272
        }
273
274
        try
275
        {
276
            $bEndRequest = false;
277
            // Handle before processing event
278
            if(($this->xRequestPlugin))
279
            {
280
                $this->onBefore($bEndRequest);
281
            }
282
            if($bEndRequest)
283
            {
284
                return;
285
            }
286
287
            $this->_processRequest();
288
289
            // Handle after processing event
290
            if(($this->xRequestPlugin))
291
            {
292
                $this->onAfter($bEndRequest);
293
            }
294
        }
295
        // An exception was thrown while processing the request.
296
        // The request missed the corresponding handler function,
297
        // or an error occurred while attempting to execute the handler.
298
        catch(RequestException $e)
299
        {
300
            $this->xResponseManager->error($e->getMessage());
301
            $this->onInvalid($e);
302
            throw $e;
303
        }
304
        catch(Exception $e)
305
        {
306
            $this->xResponseManager->error($e->getMessage());
307
            $this->onError($e);
308
            throw $e;
309
        }
310
311
        // Print the debug messages
312
        $this->xResponseManager->printDebug();
313
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
314
}
315