Completed
Push — master ( 846411...c8b313 )
by John
01:51
created

HTTPServer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace LunixREST\Server;
3
4
use LunixREST\Endpoint\Exceptions\UnknownEndpointException;
5
use LunixREST\Exceptions\AccessDeniedException;
6
use LunixREST\Exceptions\InvalidAPIKeyException;
7
use LunixREST\Exceptions\ThrottleLimitExceededException;
8
use LunixREST\Request\BodyParser\Exceptions\InvalidRequestDataException;
9
use LunixREST\Request\BodyParser\Exceptions\UnknownContentTypeException;
10
use LunixREST\Request\RequestFactory\RequestFactory;
11
use LunixREST\Request\URLParser\Exceptions\InvalidRequestURLException;
12
use LunixREST\Response\Exceptions\NotAcceptableResponseTypeException;
13
14
//TODO: Unit test? Might be impossible (this is a weird global class, and hopefully the ONLY weird global class)
15
class HTTPServer {
16
    /**
17
     * @var Server
18
     */
19
    protected $server;
20
    /**
21
     * @var RequestFactory
22
     */
23
    private $requestFactory;
24
25
    /**
26
     * HTTPServer constructor.
27
     * @param Server $server
28
     * @param RequestFactory $requestFactory
29
     */
30
    //TODO: Add logging that we can pass through to log requests
31
    public function __construct(Server $server, RequestFactory $requestFactory) {
32
        $this->server = $server;
33
        $this->requestFactory = $requestFactory;
34
    }
35
36
    public function handleSAPIRequest() {
37
        try {
38
            $request = $this->requestFactory->create($_SERVER['REQUEST_METHOD'], getallheaders(),
39
                file_get_contents("php://input"), $_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI']);
40
41
            try {
42
                $response = $this->server->handleRequest($request);
43
                header("Content-Type: " . $response->getMIMEType());
44
                echo $response->getAsString();
45
            } catch(InvalidAPIKeyException $e){
46
                header('400 Bad Request', true, 400);
47
            } catch(UnknownEndpointException $e){
48
                header('404 Not Found', true, 404);
49
            } catch(NotAcceptableResponseTypeException $e){
50
                header('406 Not Acceptable', true, 406);
51
            } catch(AccessDeniedException $e){
52
                header('403 Access Denied', true, 403);
53
            } catch(ThrottleLimitExceededException $e){
54
                header('429 Too Many Requests', true, 429);
55
            } catch (\Exception $e) {
56
                header('500 Internal Server Error', true, 500);
57
            }
58
        } catch(InvalidRequestURLException $e){
59
            header('400 Bad Request', true, 400);
60
        } catch(UnknownContentTypeException $e){
61
            header('400 Bad Request', true, 400);
62
        } catch(InvalidRequestDataException $e){
63
            header('400 Bad Request', true, 400);
64
        } catch(\Exception $e){
65
            header('500 Internal Server Error', true, 500);
66
        }
67
    }
68
}
69