ApiServlet   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 85
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestHandler() 0 3 1
A getRequestHandler() 0 3 1
A doGet() 0 3 1
A init() 0 10 1
A getRequestHandlerFactory() 0 3 1
1
<?php
2
3
/**
4
 * AppserverIo\RestApi\Servlets\ApiServlet
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      https://github.com/appserver-io/restapi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RestApi\Servlets;
22
23
use AppserverIo\Psr\Servlet\Http\HttpServlet;
24
use AppserverIo\Psr\Servlet\ServletConfigInterface;
25
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
26
use AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface;
27
use AppserverIo\RestApi\Handlers\RequestHandlerInterface;
28
use AppserverIo\RestApi\Utils\InitParameterKeys;
29
30
/**
31
 * Controller servlet handling any API requests.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2015 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/restapi
37
 * @link      http://www.appserver.io
38
 */
39
class ApiServlet extends HttpServlet
40
{
41
42
    /**
43
     * The request handler factory instance to use.
44
     *
45
     * @var \AppserverIo\RestApi\Handlers\RequestHandlerFactory
46
     */
47
    protected $requestHandlerFactory;
48
49
    /**
50
     * The request handler instance to use.
51
     *
52
     * @var \AppserverIo\RestApi\Handlers\RequestHandlerInterface
53
     */
54
    protected $requestHandler;
55
56
    /**
57
     * Sets the request handler instance to use.
58
     *
59
     * @param \AppserverIo\RestApi\Handlers\RequestHandlerInterface $requestHandler The request handler instance to use
60
     *
61
     * @return void
62
     */
63
    protected function setRequestHandler(RequestHandlerInterface $requestHandler)
64
    {
65
        $this->requestHandler = $requestHandler;
66
    }
67
68
    /**
69
     * Returns the request handler instance.
70
     *
71
     * @return \AppserverIo\RestApi\Handlers\RequestHandlerInterface The parser instance
72
     */
73
    protected function getRequestHandler()
74
    {
75
        return $this->requestHandler;
76
    }
77
78
    /**
79
     * Returns the request handler factory instance.
80
     *
81
     * @return \AppserverIo\RestApi\Handlers\RequestHandlerFactory The request handler factory instance
82
     */
83
    protected function getRequestHandlerFactory()
84
    {
85
        return $this->requestHandlerFactory;
86
    }
87
88
    /**
89
     * Initializes the servlet with the passed configuration.
90
     *
91
     * @param \AppserverIo\Psr\Servlet\ServletConfigInterface $servletConfig The configuration to initialize the servlet with
92
     *
93
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the configuration has errors
94
     * @return void
95
     * @see \AppserverIo\Psr\Servlet\GenericServlet::init()
96
     */
97
    public function init(ServletConfigInterface $servletConfig)
98
    {
99
100
        // call parent method
101
        parent::init($servletConfig);
102
103
        // create and initialize a new request handler instance
104
        $this->setRequestHandler(
105
            $this->getRequestHandlerFactory()->createRequestHandler(
106
                $servletConfig->getInitParameter(InitParameterKeys::API)
107
            )
108
        );
109
    }
110
111
    /**
112
     * Handles a HTTP GET request.
113
     *
114
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface  $servletRequest  The request instance
115
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletResponseInterface $servletResponse The response instance
116
     *
117
     * @return void
118
     * @throws \AppserverIo\Psr\Servlet\ServletException Is thrown if the request method is not implemented
119
     * @see \AppserverIo\Psr\Servlet\Http\HttpServlet::doGet()
120
     */
121
    public function doGet(HttpServletRequestInterface $servletRequest, HttpServletResponseInterface $servletResponse)
122
    {
123
        $this->getRequestHandler()->handle($servletRequest, $servletResponse);
124
    }
125
}
126