SimpleErrorHandler::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 6
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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