Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

CallableClass::processRequest()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 25
rs 9.5555
cc 5
nc 4
nop 0
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\CallableClass as UserCallableClass;
26
use Jaxon\Plugin\Request as RequestPlugin;
27
use Jaxon\Request\Support\CallableObject;
28
use Jaxon\Request\Support\CallableRegistry;
29
use Jaxon\Request\Support\CallableRepository;
30
use Jaxon\Request\Target;
31
32
class CallableClass extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableClass
Loading history...
33
{
34
    use \Jaxon\Features\Config;
35
    use \Jaxon\Features\Template;
36
    use \Jaxon\Features\Validator;
37
    use \Jaxon\Features\Translator;
38
39
    /**
40
     * The callable registry
41
     *
42
     * @var CallableRegistry
43
     */
44
    protected $xRegistry;
45
46
    /**
47
     * The callable repository
48
     *
49
     * @var CallableRepository
50
     */
51
    protected $xRepository;
52
53
    /**
54
     * The value of the class parameter of the incoming Jaxon request
55
     *
56
     * @var string
57
     */
58
    protected $sRequestedClass = '';
59
60
    /**
61
     * The value of the method parameter of the incoming Jaxon request
62
     *
63
     * @var string
64
     */
65
    protected $sRequestedMethod = '';
66
67
    /**
68
     * The class constructor
69
     *
70
     * @param CallableRegistry      $xRegistry      The callable class registry
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 6 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
71
     * @param CallableRepository    $xRepository    The callable object repository
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
72
     */
73
    public function __construct(CallableRegistry $xRegistry, CallableRepository $xRepository)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
74
    {
75
        $this->xRegistry = $xRegistry;
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...
76
        $this->xRepository = $xRepository;
77
78
        if(!empty($_GET['jxncls']))
79
        {
80
            $this->sRequestedClass = trim($_GET['jxncls']);
81
        }
82
        if(!empty($_GET['jxnmthd']))
83
        {
84
            $this->sRequestedMethod = trim($_GET['jxnmthd']);
85
        }
86
        if(!empty($_POST['jxncls']))
87
        {
88
            $this->sRequestedClass = trim($_POST['jxncls']);
89
        }
90
        if(!empty($_POST['jxnmthd']))
91
        {
92
            $this->sRequestedMethod = trim($_POST['jxnmthd']);
93
        }
94
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
95
96
    /**
97
     * @inheritDoc
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function getName()
100
    {
101
        return Jaxon::CALLABLE_CLASS;
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * @inheritDoc
106
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
107
    public function getTarget()
108
    {
109
        if(!$this->sRequestedClass || !$this->sRequestedMethod)
110
        {
111
            return null;
112
        }
113
        return Target::makeClass($this->sRequestedClass, $this->sRequestedMethod);
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * Register a callable class
118
     *
119
     * @param string        $sType          The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter name; 10 found
Loading history...
120
     * @param string        $sClassName     The name of the class being registered
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
121
     * @param array|string  $aOptions       The associated options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
122
     *
123
     * @return boolean
124
     */
125
    public function register($sType, $sClassName, $aOptions)
126
    {
127
        $sType = trim($sType);
128
        if($sType != $this->getName())
129
        {
130
            return false;
131
        }
132
133
        if(!is_string($sClassName))
0 ignored issues
show
introduced by
The condition is_string($sClassName) is always true.
Loading history...
134
        {
135
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
136
        }
137
        if(is_string($aOptions))
138
        {
139
            $aOptions = ['include' => $aOptions];
140
        }
141
        if(!is_array($aOptions))
0 ignored issues
show
introduced by
The condition is_array($aOptions) is always true.
Loading history...
142
        {
143
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid-declaration'));
144
        }
145
146
        $this->xRepository->addClass(trim($sClassName), $aOptions);
147
148
        return true;
149
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
150
151
    /**
152
     * @inheritDoc
153
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
154
    public function getHash()
155
    {
156
        $this->xRegistry->registerCallableClasses();
157
        $aNamespaces = $this->xRepository->getNamespaces();
158
        $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...
159
        $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...
160
161
        foreach($aNamespaces as $sNamespace => $aOptions)
162
        {
163
            $sHash .= $sNamespace . $aOptions['separator'];
164
        }
165
        foreach($aClasses as $sClassName => $aOptions)
166
        {
167
            $sHash .= $sClassName . $aOptions['timestamp'];
168
        }
169
170
        return md5($sHash);
171
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
172
173
    /**
174
     * Generate client side javascript code for namespaces
175
     *
176
     * @return string
177
     */
178
    private function getNamespacesScript()
179
    {
180
        $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...
181
        $sPrefix = $this->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...
182
        $aJsClasses = [];
183
        $aNamespaces = array_keys($this->xRepository->getNamespaces());
184
        foreach($aNamespaces as $sNamespace)
185
        {
186
            $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...
187
            $sJsNamespace = str_replace('\\', '.', $sNamespace);
188
            $sJsNamespace .= '.Null'; // This is a sentinel. The last token is not processed in the while loop.
189
            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...
190
            {
191
                $sJsClass = substr($sJsNamespace, 0, $dotPosition);
192
                // Generate code for this object
193
                if(!key_exists($sJsClass, $aJsClasses))
194
                {
195
                    $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...
196
                    $aJsClasses[$sJsClass] = $sJsClass;
197
                }
198
                $offset = $dotPosition + 1;
199
            }
200
        }
201
        return $sCode;
202
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
203
204
    /**
205
     * Generate client side javascript code for a callable class
206
     *
207
     * @param string            $sClassName         The class name
0 ignored issues
show
Coding Style introduced by
Expected 9 spaces after parameter type; 12 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
208
     * @param CallableObject    $xCallableObject    The corresponding callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
209
     * @param array             $aProtectedMethods  The protected methods
0 ignored issues
show
Coding Style introduced by
Expected 10 spaces after parameter type; 13 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
210
     *
211
     * @return string
212
     */
213
    private function getCallableScript($sClassName, CallableObject $xCallableObject, array $aProtectedMethods)
214
    {
215
        $aCallableOptions = $this->xRepository->getCallableOptions();
216
        $aConfig = $aCallableOptions[$sClassName];
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...
217
        $aCommonConfig = key_exists('*', $aConfig) ? $aConfig['*'] : [];
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...
218
219
        $_aProtectedMethods = is_subclass_of($sClassName, UserCallableClass::class) ? $aProtectedMethods : [];
220
        $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...
221
        foreach($xCallableObject->getMethods() as $sMethodName)
222
        {
223
            // Don't export methods of the CallableClass class
224
            if(in_array($sMethodName, $_aProtectedMethods))
225
            {
226
                continue;
227
            }
228
            // Specific options for this method
229
            $aMethodConfig = key_exists($sMethodName, $aConfig) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
230
                array_merge($aCommonConfig, $aConfig[$sMethodName]) : $aCommonConfig;
231
            $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...
232
                'name' => $sMethodName,
233
                'config' => $aMethodConfig,
234
            ];
235
        }
236
237
        $sPrefix = $this->getOption('core.prefix.class');
238
        return $this->render('jaxon::support/object.js', [
239
            'sPrefix' => $sPrefix,
240
            'sClass' => $xCallableObject->getJsName(),
241
            'aMethods' => $aMethods,
242
        ]);
243
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
244
245
    /**
246
     * Generate client side javascript code for the registered callable objects
247
     *
248
     * @return string
249
     */
250
    public function getScript()
251
    {
252
        $this->xRegistry->createCallableObjects();
253
254
        // The methods of the \Jaxon\CallableClass class must not be exported
255
        $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...
256
        $aProtectedMethods = [];
257
        foreach($xCallableClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $xMethod)
258
        {
259
            $aProtectedMethods[] = $xMethod->getName();
260
        }
261
262
        $sCode = $this->getNamespacesScript();
263
264
        $aCallableObjects = $this->xRepository->getCallableObjects();
265
        foreach($aCallableObjects as $sClassName => $xCallableObject)
266
        {
267
            $sCode .= $this->getCallableScript($sClassName, $xCallableObject, $aProtectedMethods);
268
        }
269
270
        return $sCode;
271
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
272
273
    /**
274
     * @inheritDoc
275
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
276
    public function canProcessRequest()
277
    {
278
        // Check the validity of the class name
279
        if(($this->sRequestedClass !== null && !$this->validateClass($this->sRequestedClass)) ||
280
            ($this->sRequestedMethod !== null && !$this->validateMethod($this->sRequestedMethod)))
281
        {
282
            $this->sRequestedClass = null;
283
            $this->sRequestedMethod = null;
284
        }
285
        return ($this->sRequestedClass !== null && $this->sRequestedMethod !== null);
286
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
287
288
    /**
289
     * @inheritDoc
290
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
291
    public function processRequest()
292
    {
293
        if(!$this->canProcessRequest())
294
        {
295
            return false;
296
        }
297
298
        // Find the requested method
299
        $xCallableObject = $this->xRegistry->getCallableObject($this->sRequestedClass);
300
        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...
301
        {
302
            // Unable to find the requested object or method
303
            throw new \Jaxon\Exception\Error($this->trans('errors.objects.invalid',
304
                ['class' => $this->sRequestedClass, 'method' => $this->sRequestedMethod]));
305
        }
306
307
        // Call the requested method
308
        $di = jaxon()->di();
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...
309
        $aArgs = $di->getRequestHandler()->processArguments();
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...
310
        $xResponse = $xCallableObject->call($this->sRequestedMethod, $aArgs);
311
        if(($xResponse))
312
        {
313
            $di->getResponseManager()->append($xResponse);
314
        }
315
        return true;
316
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
317
}
318