Passed
Branch request-processor (31ada2)
by Iakov
02:59
created

ProcessorResponse::createResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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)
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public function __construct(Request $request, array $data, $httpReady = false, /** @scrutinizer ignore-unused */ $status = 200)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        $this->request = $request;
34
        $this->data = $data;
35
        $this->isHttpReady = $httpReady;
36
    }
37
38
    public function toHttpResponse()
39
    {
40
        if (!$this->isHttpReady) {
41
           throw new ProcessingException('Response is not ready yet to be set as http');
42
        }
43
        $this->createResponse($this->request);
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function getData()
50
    {
51
       return $this->data;
52
    }
53
54
    /**
55
     * @param Request $request
56
     *
57
     * @return Response
58
     */
59
    private function createResponse(Request $request)
60
    {
61
        $format = $request->attributes->get('_format');
62
63
        return new Response(
64
            $this->data['response_data'],
65
            $this->status,
66
            ['Content-type' => $this->getContentTypeByFormat($format)]
67
        );
68
    }
69
70
    /**
71
     * @param string $format
72
     * @return string
73
     */
74
    private function getContentTypeByFormat($format)
75
    {
76
        switch ($format) {
77
            case 'json':
78
                return 'application/json';
79
                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...
80
            case 'xml':
81
                return 'application/xml';
82
                break;
83
            default:
84
                return 'text/plain';
85
                break;
86
        }
87
    }
88
}