Passed
Push — main ( 120722...2226e2 )
by Thierry
10:15
created

RequestHandler::onInvalid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
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\Di\Container;
24
use Jaxon\Exception\RequestException;
25
use Jaxon\Plugin\Manager\PluginManager;
26
use Jaxon\Plugin\RequestHandlerInterface;
27
use Jaxon\Plugin\Response\DataBag\DataBagPlugin;
28
use Jaxon\Request\Upload\UploadHandlerInterface;
29
use Jaxon\Response\Manager\ResponseManager;
30
31
use Exception;
32
33
class RequestHandler
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class RequestHandler
Loading history...
34
{
35
    /**
36
     * @var Container
37
     */
38
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
39
40
    /**
41
     * The plugin manager.
42
     *
43
     * @var PluginManager
44
     */
45
    private $xPluginManager;
46
47
    /**
48
     * The response manager.
49
     *
50
     * @var ResponseManager
51
     */
52
    private $xResponseManager;
53
54
    /**
55
     * The callbacks to run while processing the request
56
     *
57
     * @var CallbackManager
58
     */
59
    private $xCallbackManager;
60
61
    /**
62
     * @var UploadHandlerInterface
63
     */
64
    private $xUploadHandler;
65
66
    /**
67
     * The data bag response plugin
68
     *
69
     * @var DataBagPlugin
70
     */
71
    private $xDataBagPlugin;
72
73
    /**
74
     * The request plugin that is able to process the current request
75
     *
76
     * @var RequestHandlerInterface
77
     */
78
    private $xRequestPlugin = null;
79
80
    /**
81
     * The constructor
82
     *
83
     * @param Container $di
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
84
     * @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; 1 found
Loading history...
85
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     * @param CallbackManager $xCallbackManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
87
     * @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; 1 found
Loading history...
88
     */
89
    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...
90
        CallbackManager $xCallbackManager, DataBagPlugin $xDataBagPlugin)
91
    {
92
        $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...
93
        $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...
94
        $this->xResponseManager = $xResponseManager;
95
        $this->xCallbackManager = $xCallbackManager;
96
        $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...
97
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
98
99
    /**
100
     * Check if the current request can be processed
101
     *
102
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
103
     *
104
     * @return bool
105
     */
106
    public function canProcessRequest(): bool
107
    {
108
        $this->xUploadHandler = $this->di->getUploadHandler();
109
110
        // Return true if the request plugin was already found
111
        if($this->xRequestPlugin !== null)
112
        {
113
            return true;
114
        }
115
116
        // The HTTP request
117
        $xRequest = $this->di->getRequest();
118
        // Find a plugin to process the request
119
        foreach($this->xPluginManager->getRequestHandlers() as $sClassName)
120
        {
121
            if($sClassName::canProcessRequest($xRequest))
122
            {
123
                $this->xRequestPlugin = $this->di->g($sClassName);
124
                $this->xRequestPlugin->setTarget($xRequest);
125
                return true;
126
            }
127
        }
128
129
        // Check if the upload plugin is enabled
130
        if($this->xUploadHandler === null)
131
        {
132
            return false;
133
        }
134
135
        // If no other plugin than the upload plugin can process the request,
136
        // then it is an HTTP (not ajax) upload request
137
        $this->xUploadHandler->isHttpUpload();
138
        return $this->xUploadHandler->canProcessRequest($xRequest);
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * Process the current request and handle errors and exceptions.
143
     *
144
     * @return void
145
     * @throws RequestException
146
     */
147
    private function _processRequest()
148
    {
149
        // The HTTP request
150
        $xRequest = $this->di->getRequest();
151
        // Process uploaded files, if the upload plugin is enabled
152
        if($this->xUploadHandler !== null && $this->xUploadHandler->canProcessRequest($xRequest))
153
        {
154
            $this->xUploadHandler->processRequest($xRequest);
155
        }
156
        // Process the request
157
        if(($this->xRequestPlugin))
158
        {
159
            $xResponse = $this->xRequestPlugin->processRequest();
160
            if(($xResponse))
161
            {
162
                $this->xResponseManager->append($xResponse);
163
            }
164
            // Process the databag
165
            $this->xDataBagPlugin->writeCommand();
166
        }
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Process the current request.
171
     *
172
     * Calls each of the request plugins to request that they process the current request.
173
     * If any plugin processes the request, it will return true.
174
     *
175
     * @return void
176
     * @throws RequestException
177
     */
178
    public function processRequest()
179
    {
180
        // Check if there is a plugin to process this request
181
        if(!$this->canProcessRequest())
182
        {
183
            return;
184
        }
185
186
        try
187
        {
188
            $bEndRequest = false;
189
            // Handle before processing event
190
            if(($this->xRequestPlugin))
191
            {
192
                $this->xCallbackManager->onBefore($this->xRequestPlugin->getTarget(), $bEndRequest);
193
            }
194
            if($bEndRequest)
195
            {
196
                return;
197
            }
198
199
            $this->_processRequest();
200
201
            // Handle after processing event
202
            if(($this->xRequestPlugin))
203
            {
204
                $this->xCallbackManager->onAfter($this->xRequestPlugin->getTarget(), $bEndRequest);
205
            }
206
        }
207
        // An exception was thrown while processing the request.
208
        // The request missed the corresponding handler function,
209
        // or an error occurred while attempting to execute the handler.
210
        catch(RequestException $e)
211
        {
212
            $this->xResponseManager->error($e->getMessage());
213
            $this->xCallbackManager->onInvalid($e);
214
        }
215
        catch(Exception $e)
216
        {
217
            $this->xResponseManager->error($e->getMessage());
218
            $this->xCallbackManager->onError($e);
219
        }
220
221
        // Print the debug messages
222
        $this->xResponseManager->printDebug();
223
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
224
}
225