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

GenericServerHTTPServer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 43
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
C handleServerException() 24 24 7

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\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
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
    /**
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