SimpleErrorHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 55
loc 55
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A __invoke() 6 6 1
A logException() 16 16 2

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
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ErrorHandler;
6
7
use Psr\Http\Message\ResponseInterface as Response;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12 View Code Duplication
final class SimpleErrorHandler implements ErrorHandlerInterface
0 ignored issues
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...
13
{
14
    /**
15
     * @var ErrorResponseProviderInterface
16
     */
17
    private $provider;
18
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private $logger;
23
24
    /**
25
     * @param ErrorResponseProviderInterface $provider
26
     */
27 2
    public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null)
28
    {
29 2
        $this->provider = $provider;
30 2
        $this->logger = $logger ?? new NullLogger();
31 2
    }
32
33
    /**
34
     * @param Request    $request
35
     * @param Response   $response
36
     * @param \Exception $exception
37
     *
38
     * @return Response
39
     */
40 2
    public function __invoke(Request $request, Response $response, \Exception $exception): Response
41
    {
42 2
        $this->logException($exception);
43
44 2
        return $this->provider->get($request, $response, $exception);
45
    }
46
47
    /**
48
     * @param \Exception $exception
49
     */
50 2
    private function logException(\Exception $exception)
51
    {
52 2
        if ($exception instanceof HttpException) {
53 1
            $this->logger->warning(
54 1
                'error-handler: {code} {message}',
55 1
                ['status' => $exception->getCode(), 'message' => $exception->getMessage()]
56
            );
57
58 1
            return;
59
        }
60
61 1
        $this->logger->error(
62 1
            'error-handler: {code} {message}',
63 1
            ['status' => 500, 'message' => $exception->getMessage()]
64
        );
65 1
    }
66
}
67