Passed
Push — master ( dc0218...f79a4b )
by Thierry
02:43
created

CallableClass   B

Complexity

Total Complexity 43

Size/Duplication

Total Lines 349
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 116
c 4
b 0
f 0
dl 0
loc 349
rs 8.96
wmc 43

11 Methods

Rating   Name   Duplication   Size   Complexity  
A processRequest() 0 33 6
A __construct() 0 28 5
A register() 0 5 1
B getCallableScript() 0 33 7
A getHash() 0 17 3
A getName() 0 3 1
A getTarget() 0 7 3
A checkOptions() 0 15 4
A getScript() 0 26 3
A canProcessRequest() 0 10 6
A getNamespacesScript() 0 24 4

How to fix   Complexity   

Complex Class

Complex classes like CallableClass often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CallableClass, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * CallableClass.php - Jaxon callable class plugin
5
 *
6
 * This class registers user defined callable classes, generates client side javascript code,
7
 * and calls their methods on user request
8
 *
9
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
10
 * @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...
11
 * @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...
12
 * @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...
13
 * @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...
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @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...
16
 * @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...
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
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...
21
22
namespace Jaxon\Request\Plugin;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Request\Handler\Handler as RequestHandler;
26
use Jaxon\Response\Manager as ResponseManager;
27
use Jaxon\CallableClass as UserCallableClass;
28
use Jaxon\Plugin\Request as RequestPlugin;
29
use Jaxon\Request\Support\CallableObject;
30
use Jaxon\Request\Support\CallableRegistry;
31
use Jaxon\Request\Support\CallableRepository;
32
use Jaxon\Request\Target;
33
use Jaxon\Request\Validator;
34
use Jaxon\Exception\RequestException;
35
use Jaxon\Exception\SetupException;
36
use Jaxon\Utils\Config\Config;
37
use Jaxon\Utils\Template\Engine as TemplateEngine;
38
use Jaxon\Utils\Translation\Translator;
39
use ReflectionException;
40
41
use function trim;
42
use function strlen;
43
use function is_string;
44
use function is_array;
45
use function in_array;
46
use function uksort;
47
use function md5;
48
use function array_map;
49
use function array_merge;
50
use function is_subclass_of;
51
52
class CallableClass extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableClass
Loading history...
53
{
54
    /**
55
     * @var Config
56
     */
57
    protected $xConfig;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
58
59
    /**
60
     * The request handler
61
     *
62
     * @var RequestHandler
63
     */
64
    protected $xRequestHandler;
65
66
    /**
67
     * The response manager
68
     *
69
     * @var ResponseManager
70
     */
71
    protected $xResponseManager;
72
73
    /**
74
     * The callable registry
75
     *
76
     * @var CallableRegistry
77
     */
78
    protected $xRegistry;
79
80
    /**
81
     * The callable repository
82
     *
83
     * @var CallableRepository
84
     */
85
    protected $xRepository;
86
87
    /**
88
     * The request data validator
89
     *
90
     * @var Validator
91
     */
92
    protected $xValidator;
93
94
    /**
95
     * @var TemplateEngine
96
     */
97
    protected $xTemplateEngine;
98
99
    /**
100
     * @var Translator
101
     */
102
    protected $xTranslator;
103
104
    /**
105
     * The value of the class parameter of the incoming Jaxon request
106
     *
107
     * @var string
108
     */
109
    protected $sRequestedClass = '';
110
111
    /**
112
     * The value of the method parameter of the incoming Jaxon request
113
     *
114
     * @var string
115
     */
116
    protected $sRequestedMethod = '';
117
118
    /**
119
     * The class constructor
120
     *
121
     * @param Config  $xConfig
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 13 spaces after parameter type; 2 found
Loading history...
122
     * @param RequestHandler  $xRequestHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 2 found
Loading history...
123
     * @param ResponseManager  $xResponseManager
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
124
     * @param CallableRegistry $xRegistry    The callable class registry
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
125
     * @param CallableRepository $xRepository    The callable object repository
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 4 found
Loading history...
126
     * @param TemplateEngine  $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 2 found
Loading history...
127
     * @param Translator  $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 2 found
Loading history...
128
     * @param Validator  $xValidator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 2 found
Loading history...
129
     */
130
    public function __construct(Config $xConfig, RequestHandler $xRequestHandler,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
131
        ResponseManager $xResponseManager, CallableRegistry $xRegistry, CallableRepository $xRepository,
132
        TemplateEngine $xTemplateEngine, Translator $xTranslator, Validator $xValidator)
133
    {
134
        $this->xConfig = $xConfig;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
135
        $this->xRequestHandler = $xRequestHandler;
136
        $this->xResponseManager = $xResponseManager;
137
        $this->xRegistry = $xRegistry;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
138
        $this->xRepository = $xRepository;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
139
        $this->xTemplateEngine = $xTemplateEngine;
140
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
141
        $this->xValidator = $xValidator;
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...
142
143
        if(isset($_GET['jxncls']))
144
        {
145
            $this->sRequestedClass = trim($_GET['jxncls']);
146
        }
147
        if(isset($_GET['jxnmthd']))
148
        {
149
            $this->sRequestedMethod = trim($_GET['jxnmthd']);
150
        }
151
        if(isset($_POST['jxncls']))
152
        {
153
            $this->sRequestedClass = trim($_POST['jxncls']);
154
        }
155
        if(isset($_POST['jxnmthd']))
156
        {
157
            $this->sRequestedMethod = trim($_POST['jxnmthd']);
158
        }
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * @inheritDoc
163
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
164
    public function getName(): string
165
    {
166
        return Jaxon::CALLABLE_CLASS;
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * @inheritDoc
171
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
172
    public function getTarget(): ?Target
173
    {
174
        if(!$this->sRequestedClass || !$this->sRequestedMethod)
175
        {
176
            return null;
177
        }
178
        return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod);
179
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
180
181
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $xOptions should have a doc-comment as per coding-style.
Loading history...
182
     * @inheritDoc
183
     * @throws SetupException
184
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
185
    public function checkOptions(string $sCallable, $xOptions): array
186
    {
187
        if(!$this->xValidator->validateClass(trim($sCallable)))
188
        {
189
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
190
        }
191
        if(is_string($xOptions))
192
        {
193
            $xOptions = ['include' => $xOptions];
194
        }
195
        elseif(!is_array($xOptions))
196
        {
197
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
198
        }
199
        return $xOptions;
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Register a callable class
204
     *
205
     * @param string $sType    The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
206
     * @param string $sCallable    The name of the class being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
207
     * @param array $aOptions    The associated options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
208
     *
209
     * @return bool
210
     */
211
    public function register(string $sType, string $sCallable, array $aOptions): bool
212
    {
213
        $sClassName = trim($sCallable);
214
        $this->xRepository->addClass($sClassName, $aOptions);
215
        return true;
216
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
217
218
    /**
219
     * @inheritDoc
220
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
221
    public function getHash(): string
222
    {
223
        $this->xRegistry->parseCallableClasses();
224
        $aNamespaces = $this->xRepository->getNamespaces();
225
        $aClasses = $this->xRepository->getClasses();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
226
        $sHash = '';
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...
227
228
        foreach($aNamespaces as $sNamespace => $aOptions)
229
        {
230
            $sHash .= $sNamespace . $aOptions['separator'];
231
        }
232
        foreach($aClasses as $sClassName => $aOptions)
233
        {
234
            $sHash .= $sClassName . $aOptions['timestamp'];
235
        }
236
237
        return md5($sHash);
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Generate client side javascript code for namespaces
242
     *
243
     * @return string
244
     */
245
    private function getNamespacesScript(): string
246
    {
247
        $sCode = '';
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...
248
        $sPrefix = $this->xConfig->getOption('core.prefix.class');
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...
249
        $aJsClasses = [];
250
        $aNamespaces = array_keys($this->xRepository->getNamespaces());
251
        foreach($aNamespaces as $sNamespace)
252
        {
253
            $offset = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
254
            $sJsNamespace = str_replace('\\', '.', $sNamespace);
255
            $sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop.
256
            while(($dotPosition = strpos($sJsNamespace, '.', $offset)) !== false)
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
257
            {
258
                $sJsClass = substr($sJsNamespace, 0, $dotPosition);
259
                // Generate code for this object
260
                if(!isset($aJsClasses[$sJsClass]))
261
                {
262
                    $sCode .= "$sPrefix$sJsClass = {};\n";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 16 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...
263
                    $aJsClasses[$sJsClass] = $sJsClass;
264
                }
265
                $offset = $dotPosition + 1;
266
            }
267
        }
268
        return $sCode;
269
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
270
271
    /**
272
     * Generate client side javascript code for a callable class
273
     *
274
     * @param CallableObject $xCallableObject    The corresponding callable object
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
275
     * @param string $sClassName    The class name
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
276
     * @param array $aProtectedMethods    The protected methods
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
277
     *
278
     * @return string
279
     */
280
    private function getCallableScript(CallableObject $xCallableObject, string $sClassName, array $aProtectedMethods): string
281
    {
282
        $aConfig = $xCallableObject->getOptions();
283
284
        // Convert an option to string, to be displayed in the js script template.
285
        $fConvertOption = function($xOption) {
286
            return is_array($xOption) ? json_encode($xOption) : $xOption;
287
        };
288
        $aCommonConfig = isset($aConfig['*']) ? array_map($fConvertOption, $aConfig['*']) : [];
289
290
        $_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : [];
291
        $aMethods = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 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...
292
        foreach($xCallableObject->getMethods() as $sMethodName)
293
        {
294
            // Don't export methods of the CallableClass class
295
            if(in_array($sMethodName, $_aProtectedMethods))
296
            {
297
                continue;
298
            }
299
            // Specific options for this method
300
            $aMethodConfig = isset($aConfig[$sMethodName]) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
301
                array_map($fConvertOption, $aConfig[$sMethodName]) : [];
302
            $aMethods[] = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
303
                'name' => $sMethodName,
304
                'config' => array_merge($aCommonConfig, $aMethodConfig),
305
            ];
306
        }
307
308
        $sPrefix = $this->xConfig->getOption('core.prefix.class');
309
        return $this->xTemplateEngine->render('jaxon::support/object.js', [
310
            'sPrefix' => $sPrefix,
311
            'sClass' => $xCallableObject->getJsName(),
312
            'aMethods' => $aMethods,
313
        ]);
314
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
315
316
    /**
317
     * Generate client side javascript code for the registered callable objects
318
     *
319
     * @return string
320
     */
321
    public function getScript(): string
322
    {
323
        $this->xRegistry->registerCallableObjects();
324
325
        // The methods of the \Jaxon\CallableClass class must not be exported
326
        $xCallableClass = new \ReflectionClass(UserCallableClass::class);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
327
        $aProtectedMethods = [];
328
        foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod)
329
        {
330
            $aProtectedMethods[] = $xMethod->getName();
331
        }
332
333
        $sCode = $this->getNamespacesScript();
334
335
        $aClassNames = $this->xRepository->getClassNames();
336
        // Sort the options by key length asc
337
        uksort($aClassNames, function($name1, $name2) {
338
            return strlen($name1) - strlen($name2);
339
        });
340
        foreach($aClassNames as $sClassName)
341
        {
342
            $xCallableObject = $this->xRegistry->getCallableObject($sClassName);
343
            $sCode .= $this->getCallableScript($xCallableObject, $sClassName, $aProtectedMethods);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
344
        }
345
346
        return $sCode;
347
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
348
349
    /**
350
     * @inheritDoc
351
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
352
    public function canProcessRequest(): bool
353
    {
354
        // Check the validity of the class name
355
        if(($this->sRequestedClass !== null && !$this->xValidator->validateClass($this->sRequestedClass)) ||
356
            ($this->sRequestedMethod !== null && !$this->xValidator->validateMethod($this->sRequestedMethod)))
357
        {
358
            $this->sRequestedClass = null;
359
            $this->sRequestedMethod = null;
360
        }
361
        return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null);
362
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
363
364
    /**
365
     * @inheritDoc
366
     * @throws RequestException
367
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
368
    public function processRequest(): bool
369
    {
370
        if(!$this->canProcessRequest())
371
        {
372
            return false;
373
        }
374
375
        // Find the requested method
376
        $xCallableObject = $this->xRegistry->getCallableObject($this->sRequestedClass);
377
        if(!$xCallableObject || !$xCallableObject->hasMethod($this->sRequestedMethod))
0 ignored issues
show
introduced by
$xCallableObject is of type Jaxon\Request\Support\CallableObject, thus it always evaluated to true.
Loading history...
378
        {
379
            // Unable to find the requested object or method
380
            throw new RequestException($this->xTranslator->trans('errors.objects.invalid',
381
                ['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod]));
382
        }
383
384
        // Call the requested method
385
        $aArgs = $this->xRequestHandler->processArguments();
386
        try
387
        {
388
            $xResponse = $xCallableObject->call($this->sRequestedMethod, $aArgs);
389
            if(($xResponse))
390
            {
391
                $this->xResponseManager->append($xResponse);
392
            }
393
        }
394
        catch(ReflectionException $e)
395
        {
396
            // Unable to find the requested class
397
            throw new RequestException($this->xTranslator->trans('errors.objects.invalid',
398
                ['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod]));
399
        }
400
        return true;
401
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
402
}
403