AbstractInterceptor::execute()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
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\Routlt\ActionInterface;
24
use AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface;
25
26
/**
27
 * Abstract interceptor implementation providing basic interceptor 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
abstract class AbstractInterceptor implements InterceptorInterface
36
{
37
38
    /**
39
     * The key of the servlet request in the method invocation parameters.
40
     *
41
     * @var string
42
     */
43
    const SERVLET_REQUEST = 'servletRequest';
44
45
    /**
46
     * The key of the servlet response in the method invocation parameters.
47
     *
48
     * @var string
49
     */
50
    const SERVLET_RESPONSE = 'servletResponse';
51
52
    /**
53
     * The intercepted action instance.
54
     *
55
     * @var \AppserverIo\Routlt\ActionInterface
56
     */
57
    protected $action;
58
59
    /**
60
     * Sets the actual action instance.
61
     *
62
     * @param  \AppserverIo\Routlt\ActionInterface $action The actual action instance
63
     *
64
     * @return void
65
     */
66 8
    public function setAction(ActionInterface $action)
67
    {
68 8
        $this->action = $action;
69 8
    }
70
71
    /**
72
     * Returns the actual action instance.
73
     *
74
     * @return \AppserverIo\Routlt\ActionInterface The actual action instance
75
     */
76 7
    public function getAction()
77
    {
78 7
        return $this->action;
79
    }
80
81
    /**
82
     * The parameters of the actual method invocation.
83
     *
84
     * @var \AppserverIo\Psr\Servlet\ServletRequestInterface
85
     */
86
    protected $parameters = array();
87
88
    /**
89
     * Sets the method invocation parameters.
90
     *
91
     * @param array $parameters The method invocation parameters
92
     *
93
     * @return void
94
     */
95 10
    public function setParameters(array $parameters)
96
    {
97 10
        $this->parameters = $parameters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameters of type array is incompatible with the declared type object<AppserverIo\Psr\S...ervletRequestInterface> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98 10
    }
99
100
    /**
101
     * Returns the requsted method invocation parameter.
102
     *
103
     * @param string $name Name of the parameter to return
104
     *
105
     * @return mixed|null The requested parameter if available
106
     */
107 7
    public function getParameter($name)
108
    {
109 7
        if (isset($this->parameters[$name])) {
110 6
            return $this->parameters[$name];
111
        }
112 1
    }
113
114
    /**
115
     * Returns the instance of the actual servlet request.
116
     *
117
     * @return \AppserverIo\Psr\Servlet\ServletRequestInterface|null The actual servlet request instance
118
     */
119 5
    public function getServletRequest()
120
    {
121 5
        return $this->getParameter(AbstractInterceptor::SERVLET_REQUEST);
122
    }
123
124
    /**
125
     * Returns the instance of the actual servlet response.
126
     *
127
     * @return \AppserverIo\Psr\Servlet\ServletResponseInterface|null The actual servlet response instance
128
     */
129 1
    public function getServletResponse()
130
    {
131 1
        return $this->getParameter(AbstractInterceptor::SERVLET_RESPONSE);
132
    }
133
134
    /**
135
     * Returns the public methods of the actual action instance.
136
     *
137
     * @return array|null The public methods
138
     */
139 3
    public function getActionMethods()
140
    {
141 3
        if (($action = $this->getAction()) != null) {
142 2
            return get_class_methods($action);
143
        }
144 1
    }
145
146
    /**
147
     * Executes the custom interceptor functionality.
148
     *
149
     * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
150
     *
151
     * @return mixed The interceptors return value
152
     */
153
    abstract protected function execute(MethodInvocationInterface $methodInvocation);
154
155
    /**
156
     * Method that implements the interceptors functionality.
157
     *
158
     * @param \AppserverIo\Psr\MetaobjectProtocol\Aop\MethodInvocationInterface $methodInvocation Initially invoked method
159
     *
160
     * @return string|null The action result
161
     */
162 7
    public function intercept(MethodInvocationInterface $methodInvocation)
163
    {
164
165
        try {
166
            // load the action instance
167 7
            $this->setAction($methodInvocation->getContext());
168
169
            // load the method invocation parameters
170 7
            $this->setParameters($methodInvocation->getParameters());
171
172
            // execute the custom interceptor functionality
173 7
            return $this->execute($methodInvocation);
174
175 1
        } catch (\Exception $e) {
176
            // add the catched exception
177 1
            $this->getAction()->addFieldError('unknown', $e->getMessage());
178
179
            // return the key for a failed action invocation
180 1
            return ActionInterface::FAILURE;
181
        }
182
    }
183
}
184