1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Routlt\Description\XhrParamsInterceptor |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/routlt |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Routlt\Interceptors; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Interceptor that set's all values found in a JSON encoded request body to the |
27
|
|
|
* action by using setter methods. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link http://github.com/appserver-io/routlt |
33
|
|
|
* @link http://www.appserver.io |
34
|
|
|
*/ |
35
|
|
|
class XhrParamsInterceptor extends AbstractInterceptor |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Tries to JSON decode the servlet request body content into parameters. |
40
|
|
|
* |
41
|
|
|
* Then it tries to find and invoke a setter with the param that matches |
42
|
|
|
* the setters name. |
43
|
|
|
* |
44
|
|
|
* @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method |
45
|
|
|
* |
46
|
|
|
* @return string|null The action result |
47
|
|
|
*/ |
48
|
1 |
|
protected function execute(MethodInvocationInterface $methodInvocation) |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
// get the action, methods and servlet request |
52
|
1 |
|
$action = $this->getAction(); |
53
|
1 |
|
$methods = $this->getActionMethods(); |
54
|
1 |
|
$servletRequest = $this->getServletRequest(); |
55
|
|
|
|
56
|
|
|
// query whether we've a body content |
57
|
1 |
|
if ($bodyContent = $servletRequest->getBodyContent()) { |
58
|
|
|
// only process if request has valid JSON |
59
|
1 |
|
if (is_object(json_decode($bodyContent))) { |
60
|
|
|
// try to inject the request parameters by using the class setters |
61
|
1 |
View Code Duplication |
foreach (json_decode($bodyContent) as $key => $value) { |
|
|
|
|
62
|
|
|
// prepare the setter method name |
63
|
1 |
|
$methodName = sprintf('set%s', ucfirst($key)); |
64
|
|
|
|
65
|
|
|
// query whether the class has the setter implemented |
66
|
1 |
|
if (in_array($methodName, $methods) === false) { |
67
|
1 |
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
// set the value by using the setter |
72
|
1 |
|
$action->$methodName($value); |
73
|
|
|
|
74
|
1 |
|
} catch (\Exception $e) { |
75
|
1 |
|
$action->addFieldError($key, $e->getMessage()); |
76
|
|
|
} |
77
|
1 |
|
} |
78
|
1 |
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
// proceed invocation chain |
82
|
1 |
|
return $methodInvocation->proceed(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.