Completed
Push — master ( 34bc1a...41d5a5 )
by Thierry
02:48 queued 02:48
created

CallableFunctionPlugin::processRequest()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.9
1
<?php
2
3
/**
4
 * CallableFunctionPlugin.php - Jaxon user function plugin
5
 *
6
 * This class registers user defined functions, generates client side javascript code,
7
 * and calls them 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
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
21
22
namespace Jaxon\Request\Plugin\CallableFunction;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Plugin\RequestPlugin;
26
use Jaxon\Request\Handler\ParameterReader;
27
use Jaxon\Request\Target;
28
use Jaxon\Request\Validator;
29
use Jaxon\Response\ResponseManager;
30
use Jaxon\Utils\Template\TemplateEngine;
31
use Jaxon\Utils\Translation\Translator;
32
use Jaxon\Exception\RequestException;
33
use Jaxon\Exception\SetupException;
34
use Psr\Http\Message\ServerRequestInterface;
35
36
use function array_keys;
37
use function implode;
38
use function is_array;
39
use function is_string;
40
use function md5;
41
use function trim;
42
43
class CallableFunctionPlugin extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableFunctionPlugin
Loading history...
44
{
45
    /**
46
     * @var string
47
     */
48
    private $sPrefix;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
49
50
    /**
51
     * The parameter reader
52
     *
53
     * @var ParameterReader
54
     */
55
    protected $xParameterReader;
56
57
    /**
58
     * The response manager
59
     *
60
     * @var ResponseManager
61
     */
62
    protected $xResponseManager;
63
64
    /**
65
     * The request data validator
66
     *
67
     * @var Validator
68
     */
69
    protected $xValidator;
70
71
    /**
72
     * @var TemplateEngine
73
     */
74
    protected $xTemplateEngine;
75
76
    /**
77
     * @var Translator
78
     */
79
    protected $xTranslator;
80
81
    /**
82
     * The registered functions names
83
     *
84
     * @var array
85
     */
86
    protected $aFunctions = [];
87
88
    /**
89
     * The registered functions options
90
     *
91
     * @var array
92
     */
93
    protected $aOptions = [];
94
95
    /**
96
     * @var Target
97
     */
98
    protected $xTarget = null;
99
100
    /**
101
     * The constructor
102
     *
103
     * @param string $sPrefix
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...
104
     * @param ParameterReader $xParameterReader
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @param ResponseManager $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
106
     * @param TemplateEngine $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
107
     * @param Translator $xTranslator
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...
108
     * @param Validator $xValidator
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...
109
     */
110
    public function __construct(string $sPrefix, ParameterReader $xParameterReader, ResponseManager $xResponseManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
111
        TemplateEngine $xTemplateEngine, Translator $xTranslator, Validator $xValidator)
112
    {
113
        $this->sPrefix = $sPrefix;
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...
114
        $this->xParameterReader = $xParameterReader;
115
        $this->xResponseManager = $xResponseManager;
116
        $this->xTemplateEngine = $xTemplateEngine;
117
        $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...
118
        $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...
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * @inheritDoc
123
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
124
    public function getName(): string
125
    {
126
        return Jaxon::CALLABLE_FUNCTION;
127
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
128
129
    /**
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...
130
     * @inheritDoc
131
     * @throws SetupException
132
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
133
    public function checkOptions(string $sCallable, $xOptions): array
134
    {
135
        if(!$this->xValidator->validateFunction(trim($sCallable)))
136
        {
137
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
138
        }
139
        if(is_string($xOptions))
140
        {
141
            $xOptions = ['include' => $xOptions];
142
        }
143
        elseif(!is_array($xOptions))
144
        {
145
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
146
        }
147
        return $xOptions;
148
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
149
150
    /**
151
     * Register a user defined function
152
     *
153
     * @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...
154
     * @param string $sCallable    The name of the function being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
155
     * @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...
156
     *
157
     * @return bool
158
     */
159
    public function register(string $sType, string $sCallable, array $aOptions): bool
160
    {
161
        $sPhpFunction = trim($sCallable);
162
        $sFunction = $sPhpFunction;
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...
163
        // Check if an alias is defined
164
        if(isset($aOptions['alias']))
165
        {
166
            $sFunction = (string)$aOptions['alias'];
167
            unset($aOptions['alias']);
168
        }
169
        $this->aFunctions[$sFunction] = $sPhpFunction;
170
        $this->aOptions[$sFunction] = $aOptions;
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...
171
        return true;
172
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
173
174
    /**
175
     * @inheritDoc
176
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
177
    public function getHash(): string
178
    {
179
        return md5(implode('', array_keys($this->aFunctions)));
180
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
181
182
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
183
     * @inheritDoc
184
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
185
    public function getCallable(string $sCallable)
186
    {
187
        $sFunction = trim($sCallable);
188
        if(!isset($this->aFunctions[$sFunction]))
189
        {
190
            return null;
191
        }
192
        $xCallable = new CallableFunction($sFunction, $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]);
193
        foreach($this->aOptions[$sFunction] as $sName => $sValue)
194
        {
195
            $xCallable->configure($sName, $sValue);
196
        }
197
        return $xCallable;
198
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
199
200
    /**
201
     * Generate the javascript function stub that is sent to the browser on initial page load
202
     *
203
     * @param CallableFunction $xFunction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
204
     *
205
     * @return string
206
     */
207
    private function getCallableScript(CallableFunction $xFunction): string
208
    {
209
        return $this->xTemplateEngine->render('jaxon::callables/function.js', [
210
            'sName' => $xFunction->getName(),
211
            'sJsName' => $xFunction->getJsName(),
212
            'aOptions' => $xFunction->getOptions(),
213
        ]);
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
217
     * @inheritDoc
218
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
219
    public function getScript(): string
220
    {
221
        $code = '';
222
        foreach(array_keys($this->aFunctions) as $sFunction)
223
        {
224
            $xFunction = $this->getCallable($sFunction);
225
            $code .= $this->getCallableScript($xFunction);
0 ignored issues
show
Bug introduced by
It seems like $xFunction can also be of type null; however, parameter $xFunction of Jaxon\Request\Plugin\Cal...in::getCallableScript() does only seem to accept Jaxon\Request\Plugin\Cal...nction\CallableFunction, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

225
            $code .= $this->getCallableScript(/** @scrutinizer ignore-type */ $xFunction);
Loading history...
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...
226
        }
227
        return $code;
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
229
230
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xRequest should have a doc-comment as per coding-style.
Loading history...
231
     * @inheritDoc
232
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
233
    public static function canProcessRequest(ServerRequestInterface $xRequest): bool
234
    {
235
        $aBody = $xRequest->getParsedBody();
236
        if(is_array($aBody))
237
        {
238
            return isset($aBody['jxnfun']);
239
        }
240
        $aParams = $xRequest->getQueryParams();
241
        return isset($aParams['jxnfun']);
242
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
243
244
    /**
245
     * @inheritDoc
246
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
247
    public function getTarget(): ?Target
248
    {
249
        return $this->xTarget;
250
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
251
252
    /**
253
     * @param ServerRequestInterface $xRequest
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
254
     *
255
     * @return void
256
     */
257
    private function setTarget(ServerRequestInterface $xRequest)
258
    {
259
        $aBody = $xRequest->getParsedBody();
260
        if(is_array($aBody))
261
        {
262
            $this->xTarget = Target::makeFunction(trim($aBody['jxnfun']));
263
            return;
264
        }
265
        $aParams = $xRequest->getQueryParams();
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...
266
        $this->xTarget = Target::makeFunction(trim($aParams['jxnfun']));
267
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
268
269
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xRequest should have a doc-comment as per coding-style.
Loading history...
270
     * @inheritDoc
271
     * @throws RequestException
272
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
273
    public function processRequest(ServerRequestInterface $xRequest): bool
274
    {
275
        $this->setTarget($xRequest);
276
        $sRequestedFunction = $this->xTarget->getFunctionName();
277
278
        // Security check: make sure the requested function was registered.
279
        if(!$this->xValidator->validateFunction($sRequestedFunction) ||
280
            !isset($this->aFunctions[$sRequestedFunction]))
281
        {
282
            // Unable to find the requested function
283
            throw new RequestException($this->xTranslator->trans('errors.functions.invalid',
284
                ['name' => $sRequestedFunction]));
285
        }
286
287
        $xFunction = $this->getCallable($sRequestedFunction);
288
        $xResponse = $xFunction->call($this->xParameterReader->args());
289
        if(($xResponse))
290
        {
291
            $this->xResponseManager->append($xResponse);
292
        }
293
        return true;
294
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
295
}
296