Completed
Push — master ( 4ca44a...67b898 )
by Denis
117:55 queued 114:41
created

JsonResponseHandler::isAjaxRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Whoops - php errors for cool kids
4
 * @author Filipe Dobreira <http://github.com/filp>
5
 */
6
7
namespace Whoops\Handler;
8
9
use Whoops\Exception\Formatter;
10
11
/**
12
 * Catches an exception and converts it to a JSON
13
 * response. Additionally can also return exception
14
 * frames for consumption by an API.
15
 */
16
class JsonResponseHandler extends Handler
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $returnFrames = false;
22
23
    /**
24
     * @param  bool|null  $returnFrames
25
     * @return bool|$this
26
     */
27 2 View Code Duplication
    public function addTraceToOutput($returnFrames = null)
28
    {
29 2
        if (func_num_args() == 0) {
30 2
            return $this->returnFrames;
31
        }
32
33 2
        $this->returnFrames = (bool) $returnFrames;
34 2
        return $this;
35
    }
36
37
    /**
38
     * @return int
39
     */
40 2
    public function handle()
41
    {
42
        $response = array(
43 2
            'error' => Formatter::formatExceptionAsDataArray(
44 2
                $this->getInspector(),
45 2
                $this->addTraceToOutput()
46 2
            ),
47 2
        );
48
49 2
        if (\Whoops\Util\Misc::canSendHeaders()) {
50
            header('Content-Type: application/json');
51
        }
52
53 2
        echo json_encode($response, defined('JSON_PARTIAL_OUTPUT_ON_ERROR') ? JSON_PARTIAL_OUTPUT_ON_ERROR : 0);
54
55 2
        return Handler::QUIT;
56
    }
57
}
58