FallbackExceptionHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 51
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A manages() 0 4 1
A handle() 0 7 1
A constructError() 0 10 2
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi\Exception\Handler;
13
14
use Exception;
15
16
class FallbackExceptionHandler implements ExceptionHandlerInterface
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $debug;
22
23
    /**
24
     * @param bool $debug
25
     */
26 9
    public function __construct($debug)
27
    {
28 9
        $this->debug = $debug;
29 9
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 3
    public function manages(Exception $e)
35
    {
36 3
        return true;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 6
    public function handle(Exception $e)
43
    {
44 6
        $status = 500;
45 6
        $error = $this->constructError($e, $status);
46
47 6
        return new ResponseBag($status, [$error]);
48
    }
49
50
    /**
51
     * @param \Exception $e
52
     * @param $status
53
     *
54
     * @return array
55
     */
56 6
    private function constructError(Exception $e, $status)
57
    {
58 6
        $error = ['code' => $status, 'title' => 'Internal server error'];
59
60 6
        if ($this->debug) {
61 3
            $error['detail'] = (string) $e;
62 3
        }
63
64 6
        return $error;
65
    }
66
}
67