JsonApiExceptionTransformer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 36
ccs 12
cts 18
cp 0.6667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transformException() 0 21 3
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