Completed
Push — master ( a41010...0fba1e )
by Oscar
04:06
created

Whoops::__invoke()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 17
nc 8
nop 3
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Whoops\Run;
9
use Whoops\Handler\PrettyPageHandler;
10
use Whoops\Handler\PlainTextHandler;
11
use Whoops\Handler\JsonResponseHandler;
12
use Whoops\Handler\XmlResponseHandler;
13
14
/**
15
 * Middleware to use whoops as error handler.
16
 */
17
class Whoops
18
{
19
    /**
20
     * @var Run|null To handle errors using whoops
21
     */
22
    private $whoops;
23
24
    /**
25
     * @var bool Whether catch errors or not
26
     */
27
    private $catchErrors = true;
28
29
    /**
30
     * Set the whoops instance.
31
     *
32
     * @param Run|null $whoops
33
     */
34
    public function __construct(Run $whoops = null)
35
    {
36
        $this->whoops = $whoops;
37
    }
38
39
    /**
40
     * Whether catch errors or not.
41
     *
42
     * @param bool $catchErrors
43
     *
44
     * @return self
45
     */
46
    public function catchErrors($catchErrors = true)
47
    {
48
        $this->catchErrors = (bool) $catchErrors;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Execute the middleware.
55
     *
56
     * @param ServerRequestInterface $request
57
     * @param ResponseInterface      $response
58
     * @param callable               $next
59
     *
60
     * @return ResponseInterface
61
     */
62
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
63
    {
64
        $whoops = $this->getWhoopsInstance($request);
65
66
        //Catch errors means register whoops globally
67
        if ($this->catchErrors) {
68
            $whoops->register();
69
        }
70
71
        try {
72
            $response = $next($request, $response);
73
        } catch (\Exception $exception) {
74
            $method = Run::EXCEPTION_HANDLER;
75
76
            $whoops->allowQuit(false);
77
            $whoops->writeToOutput(false);
78
            $whoops->sendHttpCode(false);
79
80
            $body = Middleware::createStream();
81
            $body->write($whoops->$method($exception));
82
83
            $response = $response->withStatus(500)->withBody($body);
84
        }
85
86
        if ($this->catchErrors) {
87
            $whoops->unregister();
88
        }
89
90
        return $response;
91
    }
92
93
    /**
94
     * Returns the whoops instance or create one.
95
     * 
96
     * @param ServerRequestInterface $request
97
     *
98
     * @return Run
99
     */
100
    private function getWhoopsInstance(ServerRequestInterface $request)
101
    {
102
        if ($this->whoops) {
103
            return $this->whoops;
104
        }
105
106
        $whoops = new Run();
107
108
        if (php_sapi_name() === 'cli') {
109
            $whoops->pushHandler(new PlainTextHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PlainTextHandler() is of type object<Whoops\Handler\PlainTextHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
111
            return $whoops;
112
        }
113
114
        $format = FormatNegotiator::getFormat($request);
115
116
        switch ($format) {
117
            case 'json':
118
                $whoops->pushHandler(new JsonResponseHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\JsonResponseHandler() is of type object<Whoops\Handler\JsonResponseHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
                break;
120
121
            case 'html':
122
                $whoops->pushHandler(new PrettyPageHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
                break;
124
125
            case 'xml':
126
                $whoops->pushHandler(new XmlResponseHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\XmlResponseHandler() is of type object<Whoops\Handler\XmlResponseHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
                break;
128
129
            default:
130
                if (empty($format)) {
131
                    $whoops->pushHandler(new PrettyPageHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
                } else {
133
                    $whoops->pushHandler(new PlainTextHandler());
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PlainTextHandler() is of type object<Whoops\Handler\PlainTextHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
                }
135
136
                break;
137
        }
138
139
        return $whoops;
140
    }
141
}
142