DispatchAction::getServletResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * AppserverIo\Routlt\DispatchAction
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\Routlt\Util\ContextKeys;
24
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
25
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface;
26
27
/**
28
 * This class implements the functionality to invoke a method on its subclass specified
29
 * by the HTTPServletRequest path info.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      http://github.com/appserver-io/routlt
35
 * @link      http://www.appserver.io
36
 */
37
abstract class DispatchAction extends BaseAction implements DispatchActionInterface
38
{
39
40
    /**
41
     * The default action method suffix.
42
     *
43
     * @var string
44
     */
45
    const ACTION_SUFFIX = 'Action';
46
47
    /**
48
     * Holds the name of the default method to invoke if the parameter with the method name to invoke is not specified.
49
     *
50
     * @var string
51
     */
52
    const DEFAULT_METHOD_NAME = 'index';
53
54
    /**
55
     * The servlet request instance.
56
     *
57
     * @var \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface
58
     */
59
    protected $servletRequest;
60
61
    /**
62
     * The servlet response instance.
63
     *
64
     * @var \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface
65
     */
66
    protected $servletResponse;
67
68
    /**
69
     * Implemented to comply witht the interface.
70
     *
71
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
72
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
73
     *
74
     * @return string|null The action result
75
     */
76 1
    public function perform(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
77
    {
78
79
        // set servlet request/response
80 1
        $this->setServletRequest($servletRequest);
81 1
        $this->setServletResponse($servletResponse);
82
83
        // load the requested method name from the context
84 1
        $methodName = $this->getAttribute(ContextKeys::METHOD_NAME);
85
86
        // invoke the requested action method
87 1
        return $this->$methodName($servletRequest, $servletResponse);
88
    }
89
90
    /**
91
     * This method returns the default method name we'll invoke if the path info doesn't contain
92
     * the method name, that'll be the second element, when we explode the path info with a slash.
93
     *
94
     * @return string The default action method name that has to be invoked
95
     */
96 1
    public function getDefaultMethod()
97
    {
98 1
        return $this->getActionSuffix(DispatchAction::DEFAULT_METHOD_NAME);
99
    }
100
101
    /**
102
     * This method returns the action suffix, or prepends the action suffix to the passed action
103
     * method name and returns it.
104
     *
105
     * @param string $requestedMethodName The action method name to append the suffix to
106
     *
107
     * @return string The action suffix or the action method name, prepended with the action suffix
108
     */
109 1
    protected function getActionSuffix($requestedMethodName)
110
    {
111 1
        return $requestedMethodName . DispatchAction::ACTION_SUFFIX;
112
    }
113
114
    /**
115
     * Sets the servlet request instance.
116
     *
117
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The request instance
118
     *
119
     * @return void
120
     */
121 2
    public function setServletRequest(HttpServletRequestInterface $servletRequest)
122
    {
123 2
        $this->servletRequest = $servletRequest;
124 2
    }
125
126
    /**
127
     * Sets the servlet response instance.
128
     *
129
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The request instance
130
     *
131
     * @return void
132
     */
133 2
    public function setServletResponse(HttpServletResponseInterface $servletResponse)
134
    {
135 2
        $this->servletResponse = $servletResponse;
136 2
    }
137
138
    /**
139
     * Returns the servlet response instance.
140
     *
141
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface The request instance
142
     */
143 1
    public function getServletRequest()
144
    {
145 1
        return $this->servletRequest;
146
    }
147
148
    /**
149
     * Returns the servlet request instance.
150
     *
151
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface The response instance
152
     */
153 1
    public function getServletResponse()
154
    {
155 1
        return $this->servletResponse;
156
    }
157
}
158