AdvancedErrorHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 93
loc 93
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 2
A addProvider() 4 4 1
A __invoke() 12 12 2
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\Slim;
6
7
use Chubbyphp\ErrorHandler\ContentTypeResolverInterface;
8
use Chubbyphp\ErrorHandler\ErrorResponseProviderInterface;
9
use Chubbyphp\ErrorHandler\HttpException;
10
use Psr\Http\Message\ServerRequestInterface as Request;
11
use Psr\Http\Message\ResponseInterface as Response;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
15
/**
16
 * @deprecated use Chubbyphp\ErrorHandler\AdvancedErrorHandler
17
 */
18 View Code Duplication
final class AdvancedErrorHandler implements ErrorHandlerInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Chubbyphp\ErrorHandler\Slim\ErrorHandlerInterface has been deprecated with message: use Chubbyphp\ErrorHandler\ErrorHandlerInterface

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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...
19
{
20
    /**
21
     * @var ContentTypeResolverInterface
22
     */
23
    private $contentTypeResolver;
24
25
    /**
26
     * @var ErrorResponseProviderInterface
27
     */
28
    private $fallbackProvider;
29
30
    /**
31
     * @var ErrorResponseProviderInterface[]
32
     */
33
    private $providers = [];
34
35
    /**
36
     * @var LoggerInterface
37
     */
38
    private $logger;
39
40
    /**
41
     * @param ContentTypeResolverInterface   $contentTypeResolver
42
     * @param ErrorResponseProviderInterface $fallbackProvider
43
     * @param array                          $providers
44
     * @param LoggerInterface|null           $logger
45
     */
46 2
    public function __construct(
47
        ContentTypeResolverInterface $contentTypeResolver,
48
        ErrorResponseProviderInterface $fallbackProvider,
49
        array $providers = [],
50
        LoggerInterface $logger = null
51
    ) {
52 2
        $this->contentTypeResolver = $contentTypeResolver;
53 2
        $this->fallbackProvider = $fallbackProvider;
54 2
        $this->addProvider($fallbackProvider);
55 2
        foreach ($providers as $provider) {
56 2
            $this->addProvider($provider);
57
        }
58 2
        $this->logger = $logger ?? new NullLogger();
59 2
    }
60
61
    /**
62
     * @param ErrorResponseProviderInterface $provider
63
     */
64 2
    private function addProvider(ErrorResponseProviderInterface $provider)
65
    {
66 2
        $this->providers[$provider->getContentType()] = $provider;
67 2
    }
68
69
    /**
70
     * @param Request    $request
71
     * @param Response   $response
72
     * @param \Exception $exception
73
     *
74
     * @return Response
75
     *
76
     * @throws \LogicException
77
     */
78 2
    public function __invoke(Request $request, Response $response, \Exception $exception): Response
79
    {
80 2
        $contentType = $this->contentTypeResolver->getContentType($request, array_keys($this->providers));
81
82 2
        $this->logException($exception);
83
84 2
        if (isset($this->providers[$contentType])) {
85 1
            return $this->providers[$contentType]->get($request, $response, $exception);
86
        }
87
88 1
        return $this->fallbackProvider->get($request, $response, $exception);
89
    }
90
91
    /**
92
     * @param \Exception $exception
93
     */
94 2
    private function logException(\Exception $exception)
95
    {
96 2
        if ($exception instanceof HttpException) {
97 1
            $this->logger->warning(
98 1
                'error-handler: {code} {message}',
99 1
                ['status' => $exception->getCode(), 'message' => $exception->getMessage()]
100
            );
101
102 1
            return;
103
        }
104
105 1
        $this->logger->error(
106 1
            'error-handler: {code} {message}',
107 1
            ['status' => 500, 'message' => $exception->getMessage()]
108
        );
109 1
    }
110
}
111