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

handleServerException()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 28
Code Lines 19

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 5
nop 2
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
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
23
{
24
    public function __construct(GenericRouterGenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger)
25
    {
26
        parent::__construct($server, $requestFactory, $logger);
27
    }
28
29
    protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface
30
    {
31
        if($exception instanceof InvalidRequestException) {
32
            $this->logCaughtThrowableResultingInHTTPCode(400, $exception, LogLevel::INFO);
33
            return $response->withStatus(400, "Bad Request");
34
35
        } elseif(
36
            $exception instanceof ElementNotFoundException ||
37
            $exception instanceof UnsupportedMethodException ||
38
            $exception instanceof UnableToCreateEndpointException
39
        ) {
40
            $this->logCaughtThrowableResultingInHTTPCode(404, $exception, LogLevel::INFO);
41
            return $response->withStatus(404, "Not Found");
42
43
        } elseif($exception instanceof ElementConflictException) {
44
            $this->logCaughtThrowableResultingInHTTPCode(409, $exception, LogLevel::NOTICE);
45
            return $response->withStatus(409, "Conflict");
46
47
        } elseif(
48
            $exception instanceof MethodNotFoundException ||
49
            $exception instanceof EndpointExecutionException
50
        ) {
51
            $this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL);
52
            return $response->withStatus(500, "Internal Server Error");
53
        }
54
55
        return parent::handleServerException($exception, $response);
56
    }
57
}
58