JsonApiExceptionTransformer::transformException()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.576
Metric Value
dl 0
loc 21
ccs 9
cts 15
cp 0.6
rs 9.3142
cc 3
eloc 12
nc 4
nop 1
crap 3.576
1
<?php
2
3
namespace Alchemy\RestBundle\Rest\Response\ExceptionTransformer;
4
5
use Alchemy\Rest\Response\ExceptionTransformer;
6
use Symfony\Component\HttpKernel\Exception\HttpException;
7
8
class JsonApiExceptionTransformer implements ExceptionTransformer
9
{
10
11
    private $debug = false;
12
13 4
    public function __construct($debug = false)
14
    {
15 4
        $this->debug = (bool) $debug;
16 4
    }
17
18
    /**
19
     * @param \Exception $exception
20
     * @return array
21
     */
22 4
    public function transformException(\Exception $exception)
23
    {
24
        $data = array(
25 4
            'code' => sprintf('%d', $exception->getCode()),
26 4
            'title' => $exception->getMessage()
27 4
        );
28
29 4
        if ($exception instanceof HttpException) {
30 2
            $data['status'] = (string) $exception->getStatusCode();
31 2
        }
32
33 4
        if ($this->debug) {
34
            $data['meta'] = array(
35
                'trace' => $exception->getTraceAsString(),
36
                'file' => $exception->getFile(),
37
                'line' => $exception->getLine()
38
            );
39
        }
40
41 4
        return array('errors' => array($data));
42
    }
43
}
44