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

GenericRouterGenericServerHTTPServer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 36
loc 36
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
C handleServerException() 28 28 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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