Completed
Push — master ( 728b33...068584 )
by John
02:38
created

HTTPServer::handleRequest()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 11
nc 5
nop 2
crap 3
1
<?php
2
namespace LunixREST\Server;
3
4
use LunixREST\APIRequest\APIRequest;
5
use LunixREST\Endpoint\Exceptions\UnknownEndpointException;
6
use LunixREST\Exceptions\AccessDeniedException;
7
use LunixREST\Exceptions\InvalidAPIKeyException;
8
use LunixREST\Exceptions\ThrottleLimitExceededException;
9
use LunixREST\APIRequest\RequestFactory\RequestFactory;
10
use LunixREST\APIRequest\URLParser\Exceptions\InvalidRequestURLException;
11
use LunixREST\APIResponse\Exceptions\NotAcceptableResponseTypeException;
12
use LunixREST\Server\Exceptions\MethodNotFoundException;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Psr\Log\LoggerAwareTrait;
16
use Psr\Log\LoggerInterface;
17
18
class HTTPServer
19
{
20
    use LoggerAwareTrait;
21
22
    /**
23
     * @var Server
24
     */
25
    protected $server;
26
    /**
27
     * @var RequestFactory
28
     */
29
    private $requestFactory;
30
31
    /**
32
     * HTTPServer constructor.
33
     * @param Server $server
34
     * @param RequestFactory $requestFactory
35
     * @param LoggerInterface $logger
36
     */
37 10
    public function __construct(Server $server, RequestFactory $requestFactory, LoggerInterface $logger)
38
    {
39 10
        $this->server = $server;
40 10
        $this->requestFactory = $requestFactory;
41 10
        $this->logger = $logger;
42 10
    }
43
44
    /**
45
     * Clones a response, changing contents based on the handling of a given request
46
     * Taking in a response allows us not to define a specific response implementation to create
47
     * @param ServerRequestInterface $serverRequest
48
     * @param ResponseInterface $response
49
     * @return ResponseInterface
50
     */
51 10
    public function handleRequest(ServerRequestInterface $serverRequest, ResponseInterface $response): ResponseInterface
52
    {
53 10
        $response = $response->withProtocolVersion($serverRequest->getProtocolVersion());
54
55
        try {
56 10
            $APIRequest = $this->requestFactory->create($serverRequest);
57
58 8
            return $this->handleAPIRequest($APIRequest, $response);
59 2
        } catch (InvalidRequestURLException $e) {
60 1
            $this->logger->notice($e->getMessage());
61 1
            return $response->withStatus(400, "Bad Request");
62 1
        } catch (\Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
63 1
            $this->logger->critical($e->getMessage());
64 1
            return $response->withStatus(500, "Internal Server Error");
65
        }
66
    }
67
68 8
    protected function handleAPIRequest(APIRequest $APIRequest, ResponseInterface $response)
69
    {
70
        try {
71 8
            $APIResponse = $this->server->handleRequest($APIRequest);
72
73 1
            $response = $response->withStatus(200, "200 OK");
74 1
            $response = $response->withAddedHeader("Content-Type", $APIResponse->getMIMEType());
75 1
            $response = $response->withAddedHeader("Content-Length", $APIResponse->getAsDataStream()->getSize());
76 1
            $this->logger->debug("Responding to request successfully");
77 1
            return $response->withBody($APIResponse->getAsDataStream());
78 7
        } catch (InvalidAPIKeyException $e) {
79 1
            $this->logger->notice($e->getMessage());
80 1
            return $response->withStatus(400, "Bad Request");
81 6
        } catch (UnknownEndpointException $e) {
82 1
            $this->logger->notice($e->getMessage());
83 1
            return $response->withStatus(404, "Not Found");
84 5
        } catch (NotAcceptableResponseTypeException $e) {
85 1
            $this->logger->notice($e->getMessage());
86 1
            return $response->withStatus(406, "Not Acceptable");
87 4
        } catch (AccessDeniedException $e) {
88 1
            $this->logger->notice($e->getMessage());
89 1
            return $response->withStatus(403, "Access Denied");
90 3
        } catch (ThrottleLimitExceededException $e) {
91 1
            $this->logger->warning($e->getMessage());
92 1
            return $response->withStatus(429, "Too Many Requests");
93 2
        } catch (MethodNotFoundException | \Throwable $e) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
94 2
            $this->logger->critical($e->getMessage());
95 2
            return $response->withStatus(500, "Internal Server Error");
96
        }
97
    }
98
99 3
    public static function dumpResponse(ResponseInterface $response) {
100 3
        $statusLine = sprintf(
101 3
            "HTTP/%s %d %s",
102 3
            $response->getProtocolVersion(),
103 3
            $response->getStatusCode(),
104 3
            $response->getReasonPhrase()
105
        );
106
107 3
        header($statusLine, true, $response->getStatusCode());
108
109 3
        foreach ($response->getHeaders() as $name => $values) {
110 1
            foreach ($values as $value) {
111 1
                header(sprintf('%s: %s', $name, $value), false);
112
            }
113
        }
114
115 3
        $body = $response->getBody();
116 3
        while(!$body->eof()) {
117 3
            echo $body->read(1024);
118
        }
119 3
    }
120
}
121