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
|
|
|
public function __construct(GenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($server, $requestFactory, $logger); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param UnableToHandleRequestException $exception |
37
|
|
|
* @param ResponseInterface $response |
38
|
|
|
* @return ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface |
41
|
|
|
{ |
42
|
|
|
if($exception instanceof InvalidAPIKeyException || $exception instanceof AccessDeniedException) { |
43
|
|
|
$this->logCaughtThrowableResultingInHTTPCode(403, $exception, LogLevel::NOTICE); |
44
|
|
|
return $response->withStatus(403, "Access Denied"); |
45
|
|
|
|
46
|
|
|
} elseif($exception instanceof ThrottleLimitExceededException) { |
47
|
|
|
$this->logCaughtThrowableResultingInHTTPCode(429, $exception, LogLevel::WARNING); |
48
|
|
|
return $response->withStatus(429, "Too Many Requests"); |
49
|
|
|
|
50
|
|
|
} elseif($exception instanceof NotAcceptableResponseTypeException) { |
51
|
|
|
$this->logCaughtThrowableResultingInHTTPCode(406, $exception, LogLevel::INFO); |
52
|
|
|
return $response->withStatus(406, "Not Acceptable"); |
53
|
|
|
|
54
|
|
|
} elseif( |
55
|
|
|
$exception instanceof UnableToCreateAPIResponseException || |
56
|
|
|
$exception instanceof UnableToRouteRequestException |
57
|
|
|
) { |
58
|
|
|
$this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL); |
59
|
|
|
return $response->withStatus(500, "Internal Server Error"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return parent::handleServerException($exception, $response); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.