1
|
|
|
<?php |
2
|
|
|
namespace LunixREST; |
3
|
|
|
|
4
|
|
|
use LunixREST\RequestFactory\RequestFactory; |
5
|
|
|
use LunixREST\Server\Exceptions\AccessDeniedException; |
6
|
|
|
use LunixREST\Server\Exceptions\InvalidAPIKeyException; |
7
|
|
|
use LunixREST\Server\Exceptions\ThrottleLimitExceededException; |
8
|
|
|
use LunixREST\Server\Exceptions\UnableToHandleRequestException; |
9
|
|
|
use LunixREST\Server\GenericServer; |
10
|
|
|
use LunixREST\Server\ResponseFactory\Exceptions\NotAcceptableResponseTypeException; |
11
|
|
|
use LunixREST\Server\ResponseFactory\Exceptions\UnableToCreateAPIResponseException; |
12
|
|
|
use LunixREST\Server\Router\Exceptions\UnableToRouteRequestException; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Psr\Log\LogLevel; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An HTTPServer that requires that the passed Server is a GenericServer |
19
|
|
|
* Class GenericServerHTTPServer |
20
|
|
|
* @package LunixREST |
21
|
|
|
*/ |
22
|
|
View Code Duplication |
class GenericServerHTTPServer extends HTTPServer |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* GenericServerHTTPServer constructor. |
26
|
|
|
* @param GenericServer $server |
27
|
|
|
* @param RequestFactory $requestFactory |
28
|
|
|
* @param LoggerInterface $logger |
29
|
|
|
*/ |
30
|
29 |
|
public function __construct(GenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger) |
31
|
|
|
{ |
32
|
29 |
|
parent::__construct($server, $requestFactory, $logger); |
33
|
29 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param UnableToHandleRequestException $exception |
37
|
|
|
* @param ResponseInterface $response |
38
|
|
|
* @return ResponseInterface |
39
|
|
|
*/ |
40
|
14 |
|
protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface |
41
|
|
|
{ |
42
|
14 |
|
if($exception instanceof InvalidAPIKeyException || $exception instanceof AccessDeniedException) { |
43
|
4 |
|
$this->logCaughtThrowableResultingInHTTPCode(403, $exception, LogLevel::NOTICE); |
44
|
4 |
|
return $response->withStatus(403, "Access Denied"); |
45
|
|
|
|
46
|
10 |
|
} elseif($exception instanceof ThrottleLimitExceededException) { |
47
|
2 |
|
$this->logCaughtThrowableResultingInHTTPCode(429, $exception, LogLevel::WARNING); |
48
|
2 |
|
return $response->withStatus(429, "Too Many Requests"); |
49
|
|
|
|
50
|
8 |
|
} elseif($exception instanceof NotAcceptableResponseTypeException) { |
51
|
2 |
|
$this->logCaughtThrowableResultingInHTTPCode(406, $exception, LogLevel::INFO); |
52
|
2 |
|
return $response->withStatus(406, "Not Acceptable"); |
53
|
|
|
|
54
|
|
|
} elseif( |
55
|
6 |
|
$exception instanceof UnableToCreateAPIResponseException || |
56
|
4 |
|
$exception instanceof UnableToRouteRequestException |
57
|
|
|
) { |
58
|
4 |
|
$this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL); |
59
|
4 |
|
return $response->withStatus(500, "Internal Server Error"); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
return parent::handleServerException($exception, $response); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|