Completed
Push — di ( eef893...256938 )
by Tim
06:01
created

BaseAction::getServletContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
139
    }
140
141
    /**
142
     * Attaches the passed value with passed key in the context of the actual request.
143
     *
144
     * @param string $key   The key to attach the data under
145
     * @param mixed  $value The data to be attached
146
     *
147
     * @return void
148
     */
149
    public function setAttribute($key, $value)
150
    {
151
        $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...
152
    }
153
154
    /**
155
     * Returns the data with the passed key from the context of the actual request.
156
     *
157
     * @param string $key The key to return the data for
158
     *
159
     * @return mixed The requested data
160
     */
161
    public function getAttribute($key)
162
    {
163
        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...
164
    }
165
166
    /**
167
     * Adds the result to the action.
168
     *
169
     * @param \AppserverIo\Routlt\Results\ResultInterface $result The result that has to be added
170
     *
171
     * @return void
172
     * @see \AppserverIo\Routlt\ActionInterface::addResult()
173
     */
174
    public function addResult(ResultInterface $result)
175
    {
176
        $this->results[$result->getName()] = $result;
177
    }
178
179
    /**
180
     * Tries to find and return the result with the passed name.
181
     *
182
     * @param string $name The name of the result to return
183
     *
184
     * @return \AppserverIo\Routlt\Results\ResultInterface|null The requested result
185
     * @see \AppserverIo\Routlt\ActionInterface::findResult()
186
     */
187
    public function findResult($name)
188
    {
189
        if (isset($this->results[$name])) {
190
            return $this->results[$name];
191
        }
192
    }
193
194
    /**
195
     * Adds a field error with the passed name and message.
196
     *
197
     * @param string $name    The name to add the message with
198
     * @param string $message The message to add
199
     *
200
     * @return void
201
     * @see \AppserverIo\Routlt\Util\ValidationAware::addFieldError()
202
     */
203
    public function addFieldError($name, $message)
204
    {
205
        $this->errors[$name] = $message;
206
    }
207
208
    /**
209
     * Returns TRUE if validation found errors, else FALSE.
210
     *
211
     * @return boolean TRUE if validation found errors, else FALSE
212
     * @see \AppserverIo\Routlt\Util\ValidationAware::hasErrors()
213
     */
214
    public function hasErrors()
215
    {
216
        return sizeof($this->errors) > 0;
217
    }
218
219
    /**
220
     * Returns the array with action errors.
221
     *
222
     * @return array The array with action errors
223
     * @see \AppserverIo\Routlt\Util\ValidationAware::getErrors()
224
     */
225
    public function getErrors()
226
    {
227
        return $this->errors;
228
    }
229
}
230