Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

ErrorHandler::getException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr7Middlewares\Middleware;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Whoops\Run;
10
11
/**
12
 * Middleware to handle php errors and exceptions.
13
 */
14
class ErrorHandler
15
{
16
    const KEY = 'EXCEPTION';
17
18
    use Utils\HandlerTrait;
19
20
    /**
21
     * @var Run|null To handle errors using whoops
22
     */
23
    protected $whoops;
24
25
    /**
26
     * @var bool Whether or not catch exceptions
27
     */
28
    protected $catchExceptions = false;
29
30
    /**
31
     * Returns the exception throwed.
32
     *
33
     * @param ServerRequestInterface $request
34
     *
35
     * @return \Exception|null
36
     */
37
    public static function getException(ServerRequestInterface $request)
38
    {
39
        return Middleware::getAttribute($request, self::KEY);
40
    }
41
42
    /**
43
     * Set an instance of Whoops.
44
     *
45
     * @param Run $whoops
46
     *
47
     * @return self
48
     */
49
    public function whoops(Run $whoops)
50
    {
51
        $this->whoops = $whoops;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Configure the catchExceptions.
58
     *
59
     * @param bool $catch
60
     *
61
     * @return self
62
     */
63
    public function catchExceptions($catch = true)
64
    {
65
        $this->catchExceptions = (boolean) $catch;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Execute the middleware.
72
     *
73
     * @param ServerRequestInterface $request
74
     * @param ResponseInterface      $response
75
     * @param callable               $next
76
     *
77
     * @return ResponseInterface
78
     */
79
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
80
    {
81
        if ($this->whoops) {
82
            $this->whoops->pushHandler(function ($exception) use ($request, $response) {
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
                try {
84
                    echo $this->executeHandler(Middleware::setAttribute($request, self::KEY, $exception), $response)->getBody();
85
                } catch (\Exception $exception) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $exception) { } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
86
                    //ignored
87
                }
88
            });
89
        }
90
91
        ob_start();
92
93
        try {
94
            $response = $next($request, $response);
95
        } catch (\Exception $exception) {
96
            if (!$this->catchExceptions) {
97
                throw $exception;
98
            }
99
100
            $request = Middleware::setAttribute($request, self::KEY, $exception);
101
            $response = $response->withStatus(500);
102
        }
103
104
        ob_end_clean();
105
106
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
107
            try {
108
                return $this->executeHandler($request, $response);
109
            } catch (\Exception $exception) {
110
                //ignored
111
            }
112
        }
113
114
        if ($this->whoops) {
115
            $this->whoops->popHandler();
116
        }
117
118
        return $response;
119
    }
120
}
121