ParamsInterceptor::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 17
Ratio 56.67 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 17
loc 30
rs 9.44
c 0
b 0
f 0
ccs 13
cts 13
cp 1
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\Description\ParamsInterceptor
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 request parameters to the action by using setter methods.
27
 *
28
 * @author     Tim Wagner <[email protected]>
29
 * @copyright  2015 TechDivision GmbH <[email protected]>
30
 * @license    http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link       http://github.com/appserver-io/routlt
32
 * @link       http://www.appserver.io
33
 */
34
class ParamsInterceptor extends AbstractInterceptor
35
{
36
37
    /**
38
     * Iterates over all servlet request parameters and tries to find and
39
     * invoke a setter with the param that matches the setters name.
40
     *
41
     * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
42
     *
43
     * @return string|null The action result
44
     */
45 1
    protected function execute(MethodInvocationInterface $methodInvocation)
46
    {
47
48
        // get the action, methods and servlet request
49 1
        $action = $this->getAction();
50 1
        $methods = $this->getActionMethods();
51 1
        $servletRequest = $this->getServletRequest();
52
53
        // try to inject the request parameters by using the class setters
54 1 View Code Duplication
        foreach ($servletRequest->getParameterMap() 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...
55
            // prepare the setter method name
56 1
            $methodName = sprintf('set%s', ucfirst($key));
57
58
            // query whether the class has the setter implemented
59 1
            if (in_array($methodName, $methods) === false) {
60 1
                continue;
61
            }
62
63
            try {
64
                // set the value by using the setter
65 1
                $action->$methodName($value);
66
67 1
            } catch (\Exception $e) {
68 1
                $action->addFieldError($key, $e->getMessage());
69
            }
70 1
        }
71
72
        // proceed invocation chain
73 1
        return $methodInvocation->proceed();
74
    }
75
}
76