handleServerException()   C
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 28
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
dl 28
loc 28
ccs 17
cts 17
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 5
nop 2
crap 8
1
<?php
2
namespace LunixREST;
3
4
use LunixREST\RequestFactory\RequestFactory;
5
use LunixREST\Server\Exceptions\UnableToHandleRequestException;
6
use LunixREST\Server\GenericRouterGenericServer;
7
use LunixREST\Server\Router\Endpoint\Exceptions\ElementConflictException;
8
use LunixREST\Server\Router\Endpoint\Exceptions\ElementNotFoundException;
9
use LunixREST\Server\Router\Endpoint\Exceptions\EndpointExecutionException;
10
use LunixREST\Server\Router\Endpoint\Exceptions\InvalidRequestException;
11
use LunixREST\Server\Router\Endpoint\Exceptions\UnsupportedMethodException;
12
use LunixREST\Server\Router\EndpointFactory\Exceptions\UnableToCreateEndpointException;
13
use LunixREST\Server\Router\Exceptions\MethodNotFoundException;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LogLevel;
17
18
/**
19
 * Class GenericRouterGenericServerHTTPServer
20
 * @package LunixREST
21
 */
22 View Code Duplication
class GenericRouterGenericServerHTTPServer extends GenericServerHTTPServer
23
{
24 18
    public function __construct(GenericRouterGenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger)
25
    {
26 18
        parent::__construct($server, $requestFactory, $logger);
27 18
    }
28
29 14
    protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface
30
    {
31 14
        if($exception instanceof InvalidRequestException) {
32 1
            $this->logCaughtThrowableResultingInHTTPCode(400, $exception, LogLevel::INFO);
33 1
            return $response->withStatus(400, "Bad Request");
34
35
        } elseif(
36 13
            $exception instanceof ElementNotFoundException ||
37 12
            $exception instanceof UnsupportedMethodException ||
38 11
            $exception instanceof UnableToCreateEndpointException
39
        ) {
40 3
            $this->logCaughtThrowableResultingInHTTPCode(404, $exception, LogLevel::INFO);
41 3
            return $response->withStatus(404, "Not Found");
42
43 10
        } elseif($exception instanceof ElementConflictException) {
44 1
            $this->logCaughtThrowableResultingInHTTPCode(409, $exception, LogLevel::NOTICE);
45 1
            return $response->withStatus(409, "Conflict");
46
47
        } elseif(
48 9
            $exception instanceof MethodNotFoundException ||
49 8
            $exception instanceof EndpointExecutionException
50
        ) {
51 2
            $this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL);
52 2
            return $response->withStatus(500, "Internal Server Error");
53
        }
54
55 7
        return parent::handleServerException($exception, $response);
56
    }
57
}
58