Passed
Push — master ( a07882...2b8759 )
by Thierry
02:21
created

CallableFunctionPlugin   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 220
rs 10
c 0
b 0
f 0
wmc 22

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getHash() 0 3 1
A getTarget() 0 7 2
A __construct() 0 17 3
A processRequest() 0 18 3
A getScript() 0 9 2
A getCallableScript() 0 6 1
A canProcessRequest() 0 8 3
A checkOptions() 0 15 4
A register() 0 13 2
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\Container\Container;
26
use Jaxon\Plugin\RequestPlugin;
27
use Jaxon\Request\Handler\RequestHandler;
28
use Jaxon\Request\Target;
29
use Jaxon\Request\Validator;
30
use Jaxon\Response\ResponseManager;
31
use Jaxon\Utils\Template\Engine as TemplateEngine;
32
use Jaxon\Utils\Translation\Translator;
33
use Jaxon\Exception\RequestException;
34
use Jaxon\Exception\SetupException;
35
36
use function implode;
37
use function is_array;
38
use function is_string;
39
use function md5;
40
use function trim;
41
42
class CallableFunctionPlugin extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableFunctionPlugin
Loading history...
43
{
44
    /**
45
     * @var Container
46
     */
47
    private $di;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
48
49
    /**
50
     * The request handler
51
     *
52
     * @var RequestHandler
53
     */
54
    protected $xRequestHandler;
55
56
    /**
57
     * The response manager
58
     *
59
     * @var ResponseManager
60
     */
61
    protected $xResponseManager;
62
63
    /**
64
     * The request data validator
65
     *
66
     * @var Validator
67
     */
68
    protected $xValidator;
69
70
    /**
71
     * @var TemplateEngine
72
     */
73
    protected $xTemplateEngine;
74
75
    /**
76
     * @var Translator
77
     */
78
    protected $xTranslator;
79
80
    /**
81
     * The registered user functions names
82
     *
83
     * @var array
84
     */
85
    protected $aFunctions = [];
86
87
    /**
88
     * The name of the function that is being requested (during the request processing phase)
89
     *
90
     * @var string
91
     */
92
    protected $sRequestedFunction = null;
93
94
    /**
95
     * The constructor
96
     *
97
     * @param Container  $di
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
98
     * @param RequestHandler  $xRequestHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
99
     * @param ResponseManager  $xResponseManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
100
     * @param TemplateEngine  $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     * @param Translator  $xTranslator
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
102
     * @param Validator  $xValidator
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
103
     */
104
    public function __construct(Container $di, RequestHandler $xRequestHandler, ResponseManager $xResponseManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
105
        TemplateEngine $xTemplateEngine, Translator $xTranslator, Validator $xValidator)
106
    {
107
        $this->di = $di;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 15 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...
108
        $this->xRequestHandler = $xRequestHandler;
109
        $this->xResponseManager = $xResponseManager;
110
        $this->xTemplateEngine = $xTemplateEngine;
111
        $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...
112
        $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...
113
114
        if(isset($_GET['jxnfun']))
115
        {
116
            $this->sRequestedFunction = $_GET['jxnfun'];
117
        }
118
        if(isset($_POST['jxnfun']))
119
        {
120
            $this->sRequestedFunction = $_POST['jxnfun'];
121
        }
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * @inheritDoc
126
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127
    public function getName(): string
128
    {
129
        return Jaxon::CALLABLE_FUNCTION;
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * @inheritDoc
134
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
135
    public function getTarget(): ?Target
136
    {
137
        if(!$this->sRequestedFunction)
138
        {
139
            return null;
140
        }
141
        return Target::makeFunction($this->sRequestedFunction);
142
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
143
144
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xOptions should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
145
     * @inheritDoc
146
     * @throws SetupException
147
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
148
    public function checkOptions(string $sCallable, $xOptions): array
149
    {
150
        if(!$this->xValidator->validateFunction(trim($sCallable)))
151
        {
152
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
153
        }
154
        if(is_string($xOptions))
155
        {
156
            $xOptions = ['include' => $xOptions];
157
        }
158
        elseif(!is_array($xOptions))
159
        {
160
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
161
        }
162
        return $xOptions;
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
164
165
    /**
166
     * Register a user defined function
167
     *
168
     * @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...
169
     * @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...
170
     * @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...
171
     *
172
     * @return bool
173
     */
174
    public function register(string $sType, string $sCallable, array $aOptions): bool
175
    {
176
        $sFunctionName = trim($sCallable);
177
        // Check if an alias is defined
178
        $sJsFunction = $sFunctionName;
179
        if(isset($aOptions['alias']))
180
        {
181
            $sJsFunction = (string)$aOptions['alias'];
182
            unset($aOptions['alias']);
183
        }
184
        $this->aFunctions[$sFunctionName] = $sFunctionName;
185
        $this->di->registerCallableFunction($sFunctionName, $sJsFunction, $aOptions);
186
        return true;
187
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
188
189
    /**
190
     * @inheritDoc
191
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
192
    public function getHash(): string
193
    {
194
        return md5(implode('', $this->aFunctions));
195
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
196
197
    /**
198
     * Generate the javascript function stub that is sent to the browser on initial page load
199
     *
200
     * @param CallableFunction $xFunction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
201
     *
202
     * @return string
203
     */
204
    private function getCallableScript(CallableFunction $xFunction): string
205
    {
206
        return $this->xTemplateEngine->render('jaxon::callables/function.js', [
207
            'sName' => $xFunction->getName(),
208
            'sJsName' => $xFunction->getJsName(),
209
            'aOptions' => $xFunction->getOptions(),
210
        ]);
211
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
212
213
    /**
214
     * @inheritDoc
215
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
216
    public function getScript(): string
217
    {
218
        $code = '';
219
        foreach($this->aFunctions as $sName)
220
        {
221
            $xFunction = $this->di->getCallableFunction($sName);
222
            $code .= $this->getCallableScript($xFunction);
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...
223
        }
224
        return $code;
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
228
     * @inheritDoc
229
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
230
    public function canProcessRequest(): bool
231
    {
232
        // Check the validity of the function name
233
        if(($this->sRequestedFunction) && !$this->xValidator->validateFunction($this->sRequestedFunction))
234
        {
235
            $this->sRequestedFunction = null;
236
        }
237
        return ($this->sRequestedFunction != null);
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * @inheritDoc
242
     * @throws RequestException
243
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
244
    public function processRequest(): bool
245
    {
246
        // Security check: make sure the requested function was registered.
247
        if(!isset($this->aFunctions[$this->sRequestedFunction]))
248
        {
249
            // Unable to find the requested function
250
            throw new RequestException($this->xTranslator->trans('errors.functions.invalid',
251
                ['name' => $this->sRequestedFunction]));
252
        }
253
254
        $xFunction = $this->di->getCallableFunction($this->sRequestedFunction);
255
        $aArgs = $this->xRequestHandler->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...
256
        $xResponse = $xFunction->call($aArgs);
257
        if(($xResponse))
258
        {
259
            $this->xResponseManager->append($xResponse);
260
        }
261
        return true;
262
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
263
}
264