Completed
Push — di ( f3b698...546a0f )
by Tim
03:40
created

BaseAction::getAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\BaseAction
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;
22
23
use AppserverIo\Lang\Object;
24
use AppserverIo\Routlt\Results\ResultInterface;
25
use AppserverIo\Routlt\Util\ValidationAware;
26
use AppserverIo\Routlt\Util\DescriptorAware;
27
use AppserverIo\Routlt\Util\ServletContextAware;
28
use AppserverIo\Psr\Deployment\DescriptorInterface;
29
use AppserverIo\Psr\Servlet\ServletContextInterface;
30
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
31
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface;
32
33
/**
34
 * This class is the abstract base class for all Actions.
35
 *
36
 * @author    Tim Wagner <[email protected]>
37
 * @copyright 2015 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      http://github.com/appserver-io/routlt
40
 * @link      http://www.appserver.io
41
 */
42
abstract class BaseAction extends Object implements ActionInterface, ValidationAware, ServletContextAware, DescriptorAware
43
{
44
45
    /**
46
     * Holds the name of the default method to invoke if the parameter with the method name to invoke is not specified.
47
     *
48
     * @var string
49
     */
50
    const DEFAULT_METHOD_NAME = 'perform';
51
52
    /**
53
     * The context for the actual request.
54
     *
55
     * @var \AppserverIo\Psr\Servlet\ServletContextInterface
56
     */
57
    protected $servletContext = null;
58
59
    /**
60
     * The array with the action errors.
61
     *
62
     * @var array
63
     */
64
    protected $errors = array();
65
66
    /**
67
     * The array with the action results.
68
     *
69
     * @var array
70
     */
71
    protected $results = array();
72
73
    /**
74
     * The descriptor instance.
75
     *
76
     * @var \AppserverIo\Psr\Deployment\DescriptorInterface
77
     */
78
    protected $descriptor;
79
80
    /**
81
     * Sets the actual servlet context instance.
82
     *
83
     * @param \AppserverIo\Psr\Servlet\ServletContextInterface $servletContext The servlet context instance
84
     *
85
     * @return void
86
     */
87
    public function setServletContext(ServletContextInterface $servletContext)
88
    {
89
        $this->servletContext = $servletContext;
90
    }
91
92
    /**
93
     * Returns the servlet context instance.
94
     *
95
     * @return \AppserverIo\Psr\Servlet\ServletContextInterface The servlet context instance
96
     */
97
    public function getServletContext()
98
    {
99
        return $this->servletContext;
100
    }
101
102 1
    /**
103
     * Method that will be invoked before we dispatch the request.
104 1
     *
105
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
106
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
107
     *
108
     * @return void
109
     * @see \AppserverIo\Routlt\ActionInterface::preDispatch()
110
     */
111
    public function preDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
112
    {
113
        return;
114
    }
115
116 1
    /**
117
     * Method that will be invoked after we've dispatched the request.
118 1
     *
119
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
120
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
121
     *
122
     * @return void
123
     * @see \AppserverIo\Routlt\ActionInterface::postDispatch()
124
     */
125
    public function postDispatch(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
126 1
    {
127
        return;
128 1
    }
129
130
    /**
131
     * This method returns the default action method name that has to be invoked .
132
     *
133
     * @return string The default action method name that has to be invoked
134
     */
135
    public function getDefaultMethod()
136
    {
137
        return BaseAction::DEFAULT_METHOD_NAME;
138
    }
139
140
    /**
141
     * Attaches the passed value with passed key in the context of the actual request.
142
     *
143
     * @param string $key   The key to attach the data under
144
     * @param mixed  $value The data to be attached
145
     *
146
     * @return void
147
     */
148
    public function setAttribute($key, $value)
149 1
    {
150
        $this->getServletContext()->setAttribute($key, $value);
0 ignored issues
show
Bug introduced by
The method setAttribute() does not seem to exist on object<AppserverIo\Psr\S...ervletContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151 1
    }
152 1
153
    /**
154
     * Returns the data with the passed key from the context of the actual request.
155
     *
156
     * @param string $key The key to return the data for
157
     *
158
     * @return mixed The requested data
159
     */
160
    public function getAttribute($key)
161 2
    {
162
        return $this->getServletContext()->getAttribute($key);
0 ignored issues
show
Bug introduced by
The method getAttribute() does not seem to exist on object<AppserverIo\Psr\S...ervletContextInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
163 2
    }
164
165
    /**
166
     * Adds the result to the action.
167
     *
168
     * @param \AppserverIo\Routlt\Results\ResultInterface $result The result that has to be added
169
     *
170
     * @return void
171
     * @see \AppserverIo\Routlt\ActionInterface::addResult()
172
     */
173
    public function addResult(ResultInterface $result)
174 1
    {
175
        $this->results[$result->getName()] = $result;
176 1
    }
177 1
178
    /**
179
     * Tries to find and return the result with the passed name.
180
     *
181
     * @param string $name The name of the result to return
182
     *
183
     * @return \AppserverIo\Routlt\Results\ResultInterface|null The requested result
184
     * @see \AppserverIo\Routlt\ActionInterface::findResult()
185
     */
186
    public function findResult($name)
187 2
    {
188
        if (isset($this->results[$name])) {
189 2
            return $this->results[$name];
190 1
        }
191
    }
192 1
193
    /**
194
     * Adds a field error with the passed name and message.
195
     *
196
     * @param string $name    The name to add the message with
197
     * @param string $message The message to add
198
     *
199
     * @return void
200
     * @see \AppserverIo\Routlt\Util\ValidationAware::addFieldError()
201
     */
202
    public function addFieldError($name, $message)
203 1
    {
204
        $this->errors[$name] = $message;
205 1
    }
206 1
207
    /**
208
     * Returns TRUE if validation found errors, else FALSE.
209
     *
210
     * @return boolean TRUE if validation found errors, else FALSE
211
     * @see \AppserverIo\Routlt\Util\ValidationAware::hasErrors()
212
     */
213
    public function hasErrors()
214 1
    {
215
        return sizeof($this->errors) > 0;
216 1
    }
217
218
    /**
219
     * Returns the array with action errors.
220
     *
221
     * @return array The array with action errors
222
     * @see \AppserverIo\Routlt\Util\ValidationAware::getErrors()
223
     */
224
    public function getErrors()
225 1
    {
226
        return $this->errors;
227 1
    }
228
229
    /**
230
     * Sets the descriptor instance.
231
     *
232
     * @param \AppserverIo\Psr\Deployment\DescriptorInterface $descriptor The descriptor instance
233
     *
234
     * @return void
235
     */
236
    public function setDescriptor(DescriptorInterface $descriptor)
237
    {
238
        $this->descriptor = $descriptor;
239
    }
240
241
    /**
242
     * Returns the descriptor instance.
243
     *
244
     * @return \AppserverIo\Psr\Deployment\DescriptorInterface The descriptor instance
245
     */
246
    public function getDescriptor()
247
    {
248
        return $this->descriptor;
249
    }
250
}
251