Completed
Pull Request — master (#9)
by Iakov
03:57
created

ProcessorResponse   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 3 1
A toHttpResponse() 0 6 2
A createResponse() 0 8 1
A getContentTypeByFormat() 0 12 3
A getData() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
namespace Kami\ApiCoreBundle\RequestProcessor;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
8
9
class ProcessorResponse implements ResponseInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $data;
15
16
    /**
17
     * @var Request
18
     */
19
    protected $request;
20
21
    /**
22
     * @var int
23
     */
24
    protected $status;
25
26
    /**
27
     * @var bool
28
     */
29
    protected $isHttpReady = false;
30
31
    public function __construct(Request $request, array $data, $httpReady = false, $status = 200)
32
    {
33
        $this->request = $request;
34
        $this->data = $data;
35
        $this->isHttpReady = $httpReady;
36
        $this->status = $status;
37
    }
38
39
    public function toHttpResponse()
40
    {
41
        if (!$this->isHttpReady) {
42
            throw new ProcessingException('Response is not ready yet to be set as http');
43
        }
44
        return $this->createResponse($this->request);
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getData()
51
    {
52
        return $this->data;
53
    }
54
55
    /**
56
     * @return int
57
     */
58
    public function getStatus()
59
    {
60
        return $this->status;
61
    }
62
63
    /**
64
     * @param Request $request
65
     *
66
     * @return Response
67
     */
68
    private function createResponse(Request $request)
69
    {
70
        $format = $request->attributes->get('_format');
71
72
        return new Response(
73
            $this->data['response_data'],
74
            $this->status,
75
            ['Content-type' => $this->getContentTypeByFormat($format)]
76
        );
77
    }
78
79
    /**
80
     * @param string $format
81
     * @return string
82
     */
83
    private function getContentTypeByFormat($format)
84
    {
85
        switch ($format) {
86
            case 'json':
87
                return 'application/json';
88
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
            case 'xml':
90
                return 'application/xml';
91
                break;
92
            default:
93
                return 'text/plain';
94
                break;
95
        }
96
    }
97
}