Passed
Pull Request — master (#31)
by Ankit
02:23
created

Response::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TusPhp;
4
5
use Illuminate\Http\Request as HttpRequest;
6
use Illuminate\Http\Response as HttpResponse;
7
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
8
use Symfony\Component\HttpFoundation\BinaryFileResponse;
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
     * Response constructor.
23
     */
24 2
    public function __construct()
25
    {
26 2
        $this->response = new HttpResponse;
27 2
    }
28
29
    /**
30
     * Set create only.
31
     *
32
     * @param bool $state
33
     *
34
     * @return self
35
     */
36 1
    public function createOnly(bool $state) : self
37
    {
38 1
        $this->createOnly = $state;
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * Set headers.
45
     *
46
     * @param array $headers
47
     *
48
     * @return Response
49
     */
50 2
    public function setHeaders(array $headers) : self
51
    {
52 2
        $this->headers += $headers;
53
54 2
        return $this;
55
    }
56
57
    /**
58
     * Replace headers.
59
     *
60
     * @param array $headers
61
     *
62
     * @return Response
63
     */
64 1
    public function replaceHeaders(array $headers) : self
65
    {
66 1
        $this->headers = $headers;
67
68 1
        return $this;
69
    }
70
71
    /**
72
     * Get global headers.
73
     *
74
     * @return array
75
     */
76 2
    public function getHeaders() : array
77
    {
78 2
        return $this->headers;
79
    }
80
81
    /**
82
     * Get create only.
83
     *
84
     * @return bool
85
     */
86 1
    public function getCreateOnly() : bool
87
    {
88 1
        return $this->createOnly;
89
    }
90
91
    /**
92
     * Create and send a response.
93
     *
94
     * @param mixed $content Response data.
95
     * @param int   $status  Http status code.
96
     * @param array $headers Headers.
97
     *
98
     * @return HttpResponse
99
     */
100 1
    public function send($content, int $status = HttpResponse::HTTP_OK, array $headers = []) : HttpResponse
101
    {
102 1
        $headers = array_merge($this->headers, $headers);
103
104 1
        $response = $this->response->create($content, $status, $headers);
105
106 1
        return $this->createOnly ? $response : $response->send();
107
    }
108
109
    /**
110
     * Create a new file download response.
111
     *
112
     * @param \SplFileInfo|string $file
113
     * @param string              $name
114
     * @param array               $headers
115
     * @param string|null         $disposition
116
     *
117
     * @return BinaryFileResponse
118
     */
119 3
    public function download(
120
        $file,
121
        string $name = null,
122
        array $headers = [],
123
        string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT
124
    ) : BinaryFileResponse {
125 3
        $response = new BinaryFileResponse($file, HttpResponse::HTTP_OK, $headers, true, $disposition);
126
127 2
        $response->prepare(HttpRequest::createFromGlobals());
128
129 2
        if ( ! is_null($name)) {
130 1
            $response = $response->setContentDisposition(
131 1
                $disposition,
132 1
                $name,
133 1
                iconv('UTF-8', 'ASCII//TRANSLIT', $name)
134
            );
135
        }
136
137 2
        return $this->createOnly ? $response : $response->send();
138
    }
139
}
140