GenericRouterGenericServerHTTPServer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 36
loc 36
ccs 20
cts 20
cp 1
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
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