Completed
Push — master ( a8cf40...db4edb )
by Dmitry
14:23
created

SkipExceptionsIn.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
3
namespace hiapi\Http\Psr15;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
/**
11
 * Class SkipExceptionsIn
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class SkipExceptionsIn implements MiddlewareInterface
16
{
17
    /**
18
     * @var MiddlewareInterface
19
     */
20
    private $middleware;
21
22
    public function __construct(MiddlewareInterface $middleware)
23
    {
24
        $this->middleware = $middleware;
25
    }
26
27
    /**
28
     * Process an incoming server request and return a response, optionally delegating
29
     * response creation to a handler.
30
     */
31
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        $isExecutingNextHandler = false;
34
        $next = new class($isExecutingNextHandler, $handler) implements RequestHandlerInterface {
35
            /**
36
             * @var bool
37
             */
38
            private $isExecutingNextHandler;
39
            /**
40
             * @var RequestHandlerInterface
41
             */
42
            private $nextHandler;
43
44
            public function __construct(bool &$isExecutingNextHandler, RequestHandlerInterface $realHandler)
45
            {
46
                $this->isExecutingNextHandler = &$isExecutingNextHandler;
47
                $this->nextHandler = $realHandler;
48
            }
49
50
            /**
51
             * Handle the request and return a response.
52
             */
53
            public function handle(ServerRequestInterface $request): ResponseInterface
54
            {
55
                $this->isExecutingNextHandler = true;
56
                $result = $this->nextHandler->handle($request);
57
                $this->isExecutingNextHandler = false;
58
59
                return $result;
60
            }
61
        };
62
63
        try {
64
            return $this->middleware->process($request, $next);
65
        } catch (\Throwable $throwable) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
66
            if ($isExecutingNextHandler) {
67
                throw $throwable;
68
            }
69
70
            return $handler->handle($request);
71
        }
72
    }
73
}
74