Completed
Push — master ( ea9ffc...4b17ca )
by Daniel
19:33
created

RestController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 7 1
A handleError() 0 12 1
1
<?php
2
3
namespace ApiBundle\Controller;
4
5
use Application\Domain\ValueObjectInterface;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\Response;
8
9
abstract class RestController extends Controller
10
{
11
    /**
12
     * @param ValueObjectInterface $response
13
     * @param $contentType
14
     * @return Response
15
     */
16
    public function handle(ValueObjectInterface $response, $contentType = 'application/json')
17
    {
18
        $response = new Response($response);
19
        $response->headers->set('Content-Type', $contentType);
20
21
        return $response;
22
    }
23
24
    /**
25
     * @param \Exception $e
26
     * @param $contentType
27
     * @return Response
28
     */
29
    public function handleError(\Exception $e, $contentType = 'application/json')
30
    {
31
        $this->get('logger')->error($e->getMessage());
32
        $content = [
33
            'error' => 'Failed process request',
34
            'message' => $e->getMessage()
35
        ];
36
        $response = new Response($content, Response::HTTP_INTERNAL_SERVER_ERROR);
37
        $response->headers->Set('Content-Type', $contentType);
38
39
        return $response;
40
    }
41
}