Completed
Push — master ( 46e11d...3a869f )
by John
10:22
created

HTTPServer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 131
rs 10
c 0
b 0
f 0
ccs 60
cts 60
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A logCaughtThrowableResultingInHTTPCode() 0 4 1
A __construct() 0 6 1
B handleRequest() 0 23 4
A buildResponse() 0 8 1
A handleRequestFactoryException() 0 5 1
A handleServerException() 0 5 1
B dumpResponse() 0 22 4
1
<?php
2
namespace LunixREST;
3
4
use LunixREST\RequestFactory\Exceptions\UnableToCreateRequestException;
5
use LunixREST\Server\APIResponse\APIResponse;
6
use LunixREST\Server\Exceptions\UnableToHandleRequestException;
7
use LunixREST\RequestFactory\RequestFactory;
8
use LunixREST\Server\Server;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Log\LoggerAwareTrait;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\LogLevel;
14
15
/**
16
 * A class that interfaces PSR-7 with our APIRequests and uses a Server to handle the APIRequest. Handles the PSR-7 response building as well.
17
 * Class HTTPServer
18
 * @package LunixREST
19
 */
20
class HTTPServer
21
{
22
    use LoggerAwareTrait;
23
24
    /**
25
     * @var Server
26
     */
27
    protected $server;
28
    /**
29
     * @var RequestFactory
30
     */
31
    protected $requestFactory;
32
33
    /**
34
     * HTTPServer constructor.
35
     * @param Server $server
36
     * @param RequestFactory $requestFactory
37
     * @param LoggerInterface $logger
38
     */
39
    public function __construct(Server $server, RequestFactory $requestFactory, LoggerInterface $logger)
40
    {
41
        $this->server = $server;
42
        $this->requestFactory = $requestFactory;
43
        $this->logger = $logger;
44
    }
45
46
    /**
47
     * Clones a response, changing contents based on the handling of a given request.
48 14
     * Taking in a response allows us not to define a specific response implementation to create.
49
     * @param ServerRequestInterface $serverRequest
50 14
     * @param ResponseInterface $response
51 14
     * @return ResponseInterface
52 14
     */
53 14
    public function handleRequest(ServerRequestInterface $serverRequest, ResponseInterface $response): ResponseInterface
54
    {
55
        $response = $response->withProtocolVersion($serverRequest->getProtocolVersion());
56
57
        try {
58
            try {
59
                $APIRequest = $this->requestFactory->create($serverRequest);
60
            } catch (UnableToCreateRequestException $exception) {
61
                return $this->handleRequestFactoryException($exception, $response);
62 14
            }
63
64 14
            try {
65
                $APIResponse = $this->server->handleRequest($APIRequest);
66
            } catch (UnableToHandleRequestException $exception) {
67 14
                return $this->handleServerException($exception, $response);
68
            }
69 12
70 3
            return $this->buildResponse($APIResponse, $response);
71 1
        } catch (\Throwable $e) {
72 1
            $this->logCaughtThrowableResultingInHTTPCode(500, $e, LogLevel::CRITICAL);
73 2
            return $response->withStatus(500, "Internal Server Error");
74 2
        }
75 2
    }
76
77
    /**
78
     * Takes an APIResponse and builds a PSR-7 Response
79
     * @param APIResponse $APIResponse
80
     * @param ResponseInterface $response
81
     * @return ResponseInterface
82
     */
83
    protected function buildResponse(APIResponse $APIResponse, ResponseInterface $response): ResponseInterface
84
    {
85 12
        $response = $response->withStatus(200, "200 OK");
86
        $response = $response->withAddedHeader("Content-Type", $APIResponse->getMIMEType());
87
        $response = $response->withAddedHeader("Content-Length", $APIResponse->getAsDataStream()->getSize());
88 12
        $this->logger->debug("Responding to request successfully");
89
        return $response->withBody($APIResponse->getAsDataStream());
90 2
    }
91 2
92 2
    /**
93 2
     * @param UnableToCreateRequestException $exception
94 2
     * @param ResponseInterface $response
95 10
     * @return ResponseInterface
96 1
     */
97 1
    protected function handleRequestFactoryException(UnableToCreateRequestException $exception, ResponseInterface $response): ResponseInterface
98 9
    {
99 2
        $this->logCaughtThrowableResultingInHTTPCode(400, $exception, LogLevel::INFO);
100 2
        return $response->withStatus(400, "Bad Request");
101 7
    }
102 1
103 1
    /**
104 6
     * @param UnableToHandleRequestException $exception
105 2
     * @param ResponseInterface $response
106 2
     * @return ResponseInterface
107 4
     */
108 1
    protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface
109 1
    {
110 3
        $this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL);
111 1
        return $response->withStatus(500, "Internal Server Error");
112 1
    }
113 2
114 2
    /**
115 2
     * Dumps a PSR-7 ResponseInterface to the SAPI.
116
     * @param ResponseInterface $response
117
     */
118
    public static function dumpResponse(ResponseInterface $response)
119
    {
120
        $statusLine = sprintf(
121
            "HTTP/%s %d %s",
122
            $response->getProtocolVersion(),
123 3
            $response->getStatusCode(),
124
            $response->getReasonPhrase()
125 3
        );
126 3
127 3
        header($statusLine, true, $response->getStatusCode());
128 3
129 3
        foreach ($response->getHeaders() as $name => $values) {
130
            foreach ($values as $value) {
131
                header(sprintf('%s: %s', $name, $value), false);
132 3
            }
133
        }
134 3
135 1
        $body = $response->getBody();
136 1
        while(!$body->eof()) {
137
            echo $body->read(1024);
138
        }
139
    }
140 3
141 3
    /**
142 3
     * @param int $code
143
     * @param \Throwable $exception
144 3
     * @param $level
145
     */
146 13
    protected function logCaughtThrowableResultingInHTTPCode(int $code, \Throwable $exception, $level): void
147
    {
148 13
        $this->logger->log($level, "Returning HTTP {code}: {message}", ["code" => $code, "message" => $exception->getMessage()]);
149 13
    }
150
}
151