Completed
Push — master ( a66e80...cc5f29 )
by Dominik
03:11
created

SimpleErrorHandler::logException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 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
final class SimpleErrorHandler implements ErrorHandlerInterface
15
{
16
    /**
17
     * @var ErrorResponseProviderInterface
18
     */
19
    private $provider;
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $logger;
25
26
    /**
27
     * @param ErrorResponseProviderInterface $provider
28
     */
29
    public function __construct(ErrorResponseProviderInterface $provider, LoggerInterface $logger = null)
30
    {
31
        $this->provider = $provider;
32
        $this->logger = $logger ?? new NullLogger();
33
    }
34
35
    /**
36
     * @param Request    $request
37
     * @param Response   $response
38
     * @param \Exception $exception
39
     *
40
     * @return Response
41
     */
42
    public function __invoke(Request $request, Response $response, \Exception $exception): Response
43
    {
44
        $this->logException($exception);
45
46
        return $this->provider->get($request, $response, $exception);
47
    }
48
49
    /**
50
     * @param \Exception $exception
51
     */
52 View Code Duplication
    private function logException(\Exception $exception)
0 ignored issues
show
Duplication introduced by
This method 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...
53
    {
54
        if ($exception instanceof HttpException) {
55
            $this->logger->warning(
56
                'error-handler: {code} {message}',
57
                ['status' => $exception->getCode(), 'message' => $exception->getMessage()]
58
            );
59
60
            return;
61
        }
62
63
        $this->logger->error(
64
            'error-handler: {code} {message}',
65
            ['status' => 500, 'message' => $exception->getMessage()]
66
        );
67
    }
68
}
69