Passed
Branch master (7f48e0)
by Felipe
06:05
created

ErrorHandlerMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 12
c 1
b 0
f 0
dl 0
loc 44
ccs 10
cts 10
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A process() 0 12 2
1
<?php
2
3
/**
4
 * This file is part of coisa/error-handler.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\ErrorHandler\Http\Middleware;
15
16
use CoiSA\ErrorHandler\ErrorHandlerInterface;
17
use CoiSA\ErrorHandler\Http\Message\ThrowableResponseFactoryInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Psr\Http\Server\MiddlewareInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
23
/**
24
 * Class ErrorHandlerMiddleware
25
 *
26
 * @package CoiSA\ErrorHandler\Middleware
27
 */
28
final class ErrorHandlerMiddleware implements MiddlewareInterface
29
{
30
    /**
31
     * @var ErrorHandlerInterface
32
     */
33
    private $errorHandler;
34
35
    /**
36
     * @var ThrowableResponseFactoryInterface
37
     */
38
    private $throwableResponseFactory;
39
40
    /**
41
     * ErrorHandlerMiddleware constructor.
42
     *
43
     * @param ErrorHandlerInterface             $errorHandler
44
     * @param ThrowableResponseFactoryInterface $throwableResponseFactory
45
     */
46 3
    public function __construct(
47
        ErrorHandlerInterface $errorHandler,
48
        ThrowableResponseFactoryInterface $throwableResponseFactory
49
    ) {
50 3
        $this->errorHandler             = $errorHandler;
51 3
        $this->throwableResponseFactory = $throwableResponseFactory;
52 3
    }
53
54
    /**
55
     * @param ServerRequestInterface  $request
56
     * @param RequestHandlerInterface $handler
57
     *
58
     * @return ResponseInterface
59
     */
60 2
    public function process(
61
        ServerRequestInterface $request,
62
        RequestHandlerInterface $handler
63
    ): ResponseInterface {
64 2
        $this->errorHandler->register();
65
66
        try {
67 2
            return $handler->handle($request);
68 2
        } catch (\Throwable $throwable) {
69 2
            return $this->throwableResponseFactory->createResponseFromThrowable($throwable);
70
        } finally {
71 2
            $this->errorHandler->unregister();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
72
        }
73
    }
74
}
75