Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Handler   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 304
Duplicated Lines 5.59 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
dl 17
loc 304
rs 9.84
c 0
b 0
f 0
wmc 32
lcom 2
cbo 9

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getRequestMethod() 0 4 1
A requestMethodIsGet() 0 4 1
A processArguments() 0 4 1
A getCallbackManager() 0 4 1
A onBefore() 9 9 2
A onAfter() 8 8 2
A onInvalid() 0 8 2
A onError() 0 12 2
A canProcessRequest() 0 23 5
F processRequest() 0 92 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Handler.php - Jaxon Request Handler
5
 *
6
 * This class processes an incoming jaxon request.
7
 *
8
 * @package jaxon-core
9
 * @author Jared White
10
 * @author J. Max Wilson
11
 * @author Joseph Woolley
12
 * @author Steffen Konerow
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
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
 */
20
21
namespace Jaxon\Request;
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
31
{
32
    use \Jaxon\Features\Config;
33
    use \Jaxon\Features\Translator;
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 Handler\Argument
53
     */
54
    private $xArgumentManager;
55
56
    /**
57
     * The callbacks to run while processing the request
58
     *
59
     * @var 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 \Jaxon\Request\Plugin\Upload
74
     */
75
    private $xUploadRequestPlugin = null;
76
77
    /**
78
     * The constructor
79
     *
80
     * Get and decode the arguments of the HTTP request
81
     *
82
     * @param PluginManager         $xPluginManager
83
     * @param ResponseManager       $xResponseManager
84
     * @param FileUpload            $xUploadRequestPlugin
85
     */
86
    public function __construct(PluginManager $xPluginManager,
87
        ResponseManager $xResponseManager, FileUpload $xUploadRequestPlugin)
88
    {
89
        $this->xPluginManager = $xPluginManager;
90
        $this->xResponseManager = $xResponseManager;
91
        $this->xUploadRequestPlugin = $xUploadRequestPlugin;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xUploadRequestPlugin of type object<Jaxon\Request\Plugin\FileUpload> is incompatible with the declared type object<Jaxon\Request\Plugin\Upload> of property $xUploadRequestPlugin.

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...
92
93
        $this->xArgumentManager = new Handler\Argument();
94
        $this->xCallbackManager = new Handler\Callback();
95
    }
96
97
    /**
98
     * Return the method that was used to send the arguments from the client
99
     *
100
     * The method is one of: Handler\Argument::METHOD_UNKNOWN, Handler\Argument::METHOD_GET, Handler\Argument::METHOD_POST.
101
     *
102
     * @return integer
103
     */
104
    public function getRequestMethod()
105
    {
106
        return $this->xArgumentManager->getRequestMethod();
107
    }
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() == Handler\Argument::METHOD_GET);
117
    }
118
119
    /**
120
     * Return the array of arguments that were extracted and parsed from the GET or POST data
121
     *
122
     * @return array
123
     */
124
    public function processArguments()
125
    {
126
        return $this->xArgumentManager->process();
127
    }
128
129
    /**
130
     * Get the callback handler
131
     *
132
     * @return Handler\Callback
133
     */
134
    public function getCallbackManager()
135
    {
136
        return $this->xCallbackManager;
137
    }
138
139
    /**
140
     * This is the pre-request processing callback passed to the Jaxon library.
141
     *
142
     * @param  boolean  &$bEndRequest if set to true, the request processing is interrupted.
143
     *
144
     * @return Jaxon\Response\Response  the Jaxon response
145
     */
146 View Code Duplication
    public function onBefore(&$bEndRequest)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        // Call the user defined callback
149
        if(($xCallback = $this->xCallbackManager->before()))
150
        {
151
            call_user_func_array($xCallback, [$this->xTargetRequestPlugin->getTarget(), &$bEndRequest]);
152
        }
153
        // return $this->xResponse;
154
    }
155
156
    /**
157
     * This is the post-request processing callback passed to the Jaxon library.
158
     *
159
     * @return Jaxon\Response\Response  the Jaxon response
160
     */
161 View Code Duplication
    public function onAfter($bEndRequest)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163
        if(($xCallback = $this->xCallbackManager->after()))
164
        {
165
            call_user_func_array($xCallback, [$this->xTargetRequestPlugin->getTarget(), $bEndRequest]);
166
        }
167
        // return $this->xResponse;
168
    }
169
170
    /**
171
     * This callback is called whenever an invalid request is processed.
172
     *
173
     * @return Jaxon\Response\Response  the Jaxon response
174
     */
175
    public function onInvalid($sMessage)
176
    {
177
        if(($xCallback = $this->xCallbackManager->invalid()))
178
        {
179
            call_user_func_array($xCallback, [$sMessage]);
180
        }
181
        // return $this->xResponse;
182
    }
183
184
    /**
185
     * This callback is called whenever an invalid request is processed.
186
     *
187
     * @return Jaxon\Response\Response  the Jaxon response
188
     */
189
    public function onError(Exception $xException)
190
    {
191
        if(($xCallback = $this->xCallbackManager->error()))
192
        {
193
            call_user_func_array($xCallback, [$xException]);
194
        }
195
        else
196
        {
197
            throw $xException;
198
        }
199
        // return $this->xResponse;
200
    }
201
202
    /**
203
     * Check if the current request can be processed
204
     *
205
     * Calls each of the request plugins and determines if the current request can be processed by one of them.
206
     *
207
     * @return boolean
208
     */
209
    public function canProcessRequest()
210
    {
211
        // Return true if the request plugin was already found
212
        if(($this->xTargetRequestPlugin))
213
        {
214
            return true;
215
        }
216
217
        // Find a plugin to process the request
218
        foreach($this->xPluginManager->getRequestPlugins() as $xPlugin)
219
        {
220
            if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest())
221
            {
222
                $this->xTargetRequestPlugin = $xPlugin;
223
                return true;
224
            }
225
        }
226
227
        // If no other plugin than the upload plugin can process the request,
228
        // then it is a HTTP (not ajax) upload request
229
        $this->xUploadRequestPlugin->noRequestPluginFound();
230
        return $this->xUploadRequestPlugin->canProcessRequest();
231
    }
232
233
    /**
234
     * Process the current request
235
     *
236
     * Calls each of the request plugins to request that they process the current request.
237
     * If any plugin processes the request, it will return true.
238
     *
239
     * @return boolean
240
     */
241
    public function processRequest()
242
    {
243
        // Check to see if headers have already been sent out, in which case we can't do our job
244
        if(headers_sent($filename, $linenumber))
245
        {
246
            echo $this->trans('errors.output.already-sent', [
247
                'location' => $filename . ':' . $linenumber
248
            ]), "\n", $this->trans('errors.output.advice');
249
            exit();
250
        }
251
252
        // Check if there is a plugin to process this request
253
        if(!$this->canProcessRequest())
254
        {
255
            return;
256
        }
257
258
        $bEndRequest = false;
259
260
        // Handle before processing event
261
        if(($this->xTargetRequestPlugin))
262
        {
263
            $this->onBefore($bEndRequest);
264
        }
265
266
        if(!$bEndRequest)
267
        {
268
            try
269
            {
270
                // Process uploaded files
271
                $this->xUploadRequestPlugin->processRequest();
272
273
                // Process the request
274
                if(($this->xTargetRequestPlugin))
275
                {
276
                    $this->xTargetRequestPlugin->processRequest();
277
                }
278
            }
279
            catch(Exception $e)
280
            {
281
                // An exception was thrown while processing the request.
282
                // The request missed the corresponding handler function,
283
                // or an error occurred while attempting to execute the handler.
284
285
                $this->xResponseManager->error($e->getMessage());
286
287
                if($e instanceof \Jaxon\Exception\Error)
288
                {
289
                    $this->onInvalid($e->getMessage());
290
                }
291
                else
292
                {
293
                    $this->onError($e);
294
                }
295
            }
296
        }
297
298
        // Clean the processing buffer
299
        if(($this->getOption('core.process.clean')))
300
        {
301
            $er = error_reporting(0);
302
            while (ob_get_level() > 0)
303
            {
304
                ob_end_clean();
305
            }
306
            error_reporting($er);
307
        }
308
309
        if(($this->xTargetRequestPlugin))
310
        {
311
            // Handle after processing event
312
            $this->onAfter($bEndRequest);
313
        }
314
315
        // If the called function returned no response, take the the global response
316
        if(!$this->xResponseManager->getResponse())
317
        {
318
            $this->xResponseManager->append(jaxon()->getResponse());
319
        }
320
321
        $this->xResponseManager->printDebug();
322
323
        if(($this->getOption('core.response.send')))
324
        {
325
            $this->xResponseManager->sendOutput();
326
        }
327
328
        if(($this->getOption('core.process.exit')))
329
        {
330
            exit();
331
        }
332
    }
333
}
334