Response   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 24
c 2
b 0
f 0
dl 0
loc 123
ccs 27
cts 27
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 3 1
A replaceHeaders() 0 5 1
A getCreateOnly() 0 3 1
A setHeaders() 0 5 1
A createOnly() 0 5 1
A send() 0 11 3
A download() 0 18 3
1
<?php
2
3
namespace TusPhp;
4
5
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
6
use Symfony\Component\HttpFoundation\BinaryFileResponse;
7
use Symfony\Component\HttpFoundation\Request as HttpRequest;
8
use Symfony\Component\HttpFoundation\Response as HttpResponse;
9
10
class Response
11
{
12
    /** @var HttpResponse */
13
    protected $response;
14
15
    /** @var bool */
16
    protected $createOnly = true;
17
18
    /** @var array */
19
    protected $headers = [];
20
21
    /**
22
     * Set create only.
23
     *
24
     * @param bool $state
25
     *
26
     * @return self
27
     */
28 1
    public function createOnly(bool $state) : self
29
    {
30 1
        $this->createOnly = $state;
31
32 1
        return $this;
33
    }
34
35
    /**
36
     * Set headers.
37
     *
38
     * @param array $headers
39
     *
40
     * @return Response
41
     */
42 2
    public function setHeaders(array $headers) : self
43
    {
44 2
        $this->headers += $headers;
45
46 2
        return $this;
47
    }
48
49
    /**
50
     * Replace headers.
51
     *
52
     * @param array $headers
53
     *
54
     * @return Response
55
     */
56 1
    public function replaceHeaders(array $headers) : self
57
    {
58 1
        $this->headers = $headers;
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Get global headers.
65
     *
66
     * @return array
67
     */
68 2
    public function getHeaders() : array
69
    {
70 2
        return $this->headers;
71
    }
72
73
    /**
74
     * Get create only.
75
     *
76
     * @return bool
77
     */
78 1
    public function getCreateOnly() : bool
79
    {
80 1
        return $this->createOnly;
81
    }
82
83
    /**
84
     * Create and send a response.
85
     *
86
     * @param mixed $content Response data.
87
     * @param int   $status  Http status code.
88
     * @param array $headers Headers.
89
     *
90
     * @return HttpResponse
91
     */
92 2
    public function send($content, int $status = HttpResponse::HTTP_OK, array $headers = []) : HttpResponse
93
    {
94 2
        $headers = array_merge($this->headers, $headers);
95
96 2
        if (\is_array($content)) {
97 1
            $content = json_encode($content);
98
        }
99
100 2
        $response = new HttpResponse($content, $status, $headers);
101
102 2
        return $this->createOnly ? $response : $response->send();
103
    }
104
105
    /**
106
     * Create a new file download response.
107
     *
108
     * @param \SplFileInfo|string $file
109
     * @param string              $name
110
     * @param array               $headers
111
     * @param string|null         $disposition
112
     *
113
     * @return BinaryFileResponse
114
     */
115 3
    public function download(
116
        $file,
117
        string $name = null,
118
        array $headers = [],
119
        string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT
120
    ) : BinaryFileResponse {
121 3
        $response = new BinaryFileResponse($file, HttpResponse::HTTP_OK, $headers, true, $disposition);
122
123 2
        $response->prepare(HttpRequest::createFromGlobals());
124
125 2
        if ($name !== null) {
126 1
            $response = $response->setContentDisposition(
127 1
                $disposition,
128 1
                $name
129
            );
130
        }
131
132 2
        return $this->createOnly ? $response : $response->send();
133
    }
134
}
135