Passed
Push — master ( b34d93...aea763 )
by Thierry
03:02 queued 24s
created

CallableFunctionPlugin::canProcessRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
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
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
21
22
namespace Jaxon\Plugin\Request\CallableFunction;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Exception\RequestException;
26
use Jaxon\Exception\SetupException;
27
use Jaxon\Plugin\RequestPlugin;
28
use Jaxon\Request\Handler\ParameterReader;
29
use Jaxon\Request\Target;
30
use Jaxon\Request\Validator;
31
use Jaxon\Response\ResponseInterface;
32
use Jaxon\Utils\Template\TemplateEngine;
33
use Jaxon\Utils\Translation\Translator;
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 request data validator
59
     *
60
     * @var Validator
61
     */
62
    protected $xValidator;
63
64
    /**
65
     * @var TemplateEngine
66
     */
67
    protected $xTemplateEngine;
68
69
    /**
70
     * @var Translator
71
     */
72
    protected $xTranslator;
73
74
    /**
75
     * The registered functions names
76
     *
77
     * @var array
78
     */
79
    protected $aFunctions = [];
80
81
    /**
82
     * The registered functions options
83
     *
84
     * @var array
85
     */
86
    protected $aOptions = [];
87
88
    /**
89
     * The constructor
90
     *
91
     * @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...
92
     * @param ParameterReader $xParameterReader
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
93
     * @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...
94
     * @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...
95
     * @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...
96
     */
97
    public function __construct(string $sPrefix, ParameterReader $xParameterReader,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
98
        TemplateEngine $xTemplateEngine, Translator $xTranslator, Validator $xValidator)
99
    {
100
        $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...
101
        $this->xParameterReader = $xParameterReader;
102
        $this->xTemplateEngine = $xTemplateEngine;
103
        $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...
104
        $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...
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * @inheritDoc
109
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
110
    public function getName(): string
111
    {
112
        return Jaxon::CALLABLE_FUNCTION;
113
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
114
115
    /**
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...
116
     * @inheritDoc
117
     * @throws SetupException
118
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
119
    public function checkOptions(string $sCallable, $xOptions): array
120
    {
121
        if(!$this->xValidator->validateFunction(trim($sCallable)))
122
        {
123
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
124
        }
125
        if(is_string($xOptions))
126
        {
127
            $xOptions = ['include' => $xOptions];
128
        }
129
        elseif(!is_array($xOptions))
130
        {
131
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
132
        }
133
        return $xOptions;
134
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
135
136
    /**
137
     * Register a user defined function
138
     *
139
     * @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...
140
     * @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...
141
     * @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...
142
     *
143
     * @return bool
144
     */
145
    public function register(string $sType, string $sCallable, array $aOptions): bool
146
    {
147
        $sPhpFunction = trim($sCallable);
148
        $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...
149
        // Check if an alias is defined
150
        if(isset($aOptions['alias']))
151
        {
152
            $sFunction = (string)$aOptions['alias'];
153
            unset($aOptions['alias']);
154
        }
155
        $this->aFunctions[$sFunction] = $sPhpFunction;
156
        $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...
157
        return true;
158
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
159
160
    /**
161
     * @inheritDoc
162
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
163
    public function getHash(): string
164
    {
165
        return md5(implode('', array_keys($this->aFunctions)));
166
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
167
168
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
169
     * @inheritDoc
170
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
171
    public function getCallable(string $sCallable)
172
    {
173
        $sFunction = trim($sCallable);
174
        if(!isset($this->aFunctions[$sFunction]))
175
        {
176
            return null;
177
        }
178
        $xCallable = new CallableFunction($sFunction, $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]);
179
        foreach($this->aOptions[$sFunction] as $sName => $sValue)
180
        {
181
            $xCallable->configure($sName, $sValue);
182
        }
183
        return $xCallable;
184
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
185
186
    /**
187
     * Generate the javascript function stub that is sent to the browser on initial page load
188
     *
189
     * @param CallableFunction $xFunction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
190
     *
191
     * @return string
192
     */
193
    private function getCallableScript(CallableFunction $xFunction): string
194
    {
195
        return $this->xTemplateEngine->render('jaxon::callables/function.js', [
196
            'sName' => $xFunction->getName(),
197
            'sJsName' => $xFunction->getJsName(),
198
            'aOptions' => $xFunction->getOptions(),
199
        ]);
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * @inheritDoc
204
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
205
    public function getScript(): string
206
    {
207
        $code = '';
208
        foreach(array_keys($this->aFunctions) as $sFunction)
209
        {
210
            $xFunction = $this->getCallable($sFunction);
211
            $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\Plugin\Request\Cal...in::getCallableScript() does only seem to accept Jaxon\Plugin\Request\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

211
            $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...
212
        }
213
        return $code;
214
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
215
216
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xRequest should have a doc-comment as per coding-style.
Loading history...
217
     * @inheritDoc
218
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
219
    public static function canProcessRequest(ServerRequestInterface $xRequest): bool
220
    {
221
        $aBody = $xRequest->getParsedBody();
222
        if(is_array($aBody))
223
        {
224
            return isset($aBody['jxnfun']);
225
        }
226
        $aParams = $xRequest->getQueryParams();
227
        return isset($aParams['jxnfun']);
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 function setTarget(ServerRequestInterface $xRequest)
234
    {
235
        $aBody = $xRequest->getParsedBody();
236
        if(is_array($aBody))
237
        {
238
            $this->xTarget = Target::makeFunction(trim($aBody['jxnfun']));
239
            return;
240
        }
241
        $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...
242
        $this->xTarget = Target::makeFunction(trim($aParams['jxnfun']));
243
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
244
245
    /**
246
     * @inheritDoc
247
     * @throws RequestException
248
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
249
    public function processRequest(): ?ResponseInterface
250
    {
251
        $sRequestedFunction = $this->xTarget->getFunctionName();
252
253
        // Security check: make sure the requested function was registered.
254
        if(!$this->xValidator->validateFunction($sRequestedFunction) ||
255
            !isset($this->aFunctions[$sRequestedFunction]))
256
        {
257
            // Unable to find the requested function
258
            throw new RequestException($this->xTranslator->trans('errors.functions.invalid',
259
                ['name' => $sRequestedFunction]));
260
        }
261
262
        $xFunction = $this->getCallable($sRequestedFunction);
263
        return $xFunction->call($this->xParameterReader->args());
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
265
}
266