XhrParamsInterceptor::execute()   B
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 36

Duplication

Lines 17
Ratio 47.22 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
dl 17
loc 36
rs 8.7217
c 0
b 0
f 0
ccs 17
cts 17
cp 1
cc 6
nc 3
nop 1
crap 6
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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