Completed
Push — master ( adc87c...18f2cf )
by Ankit
02:21
created

Response::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
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 = false;
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 global headers.
45
     *
46
     * @param array $headers
47
     *
48
     * @return Response
49
     */
50 1
    public function setHeaders(array $headers) : self
51
    {
52 1
        $this->headers = $headers;
53
54 1
        return $this;
55
    }
56
57
    /**
58
     * Get global headers.
59
     *
60
     * @return array
61
     */
62 1
    public function getHeaders() : array
63
    {
64 1
        return $this->headers;
65
    }
66
67
    /**
68
     * Get create only.
69
     *
70
     * @return bool
71
     */
72 1
    public function getCreateOnly() : bool
73
    {
74 1
        return $this->createOnly;
75
    }
76
77
    /**
78
     * Create and send a response.
79
     *
80
     * @param mixed $content Response data.
81
     * @param int   $status  Http status code.
82
     * @param array $headers Headers.
83
     *
84
     * @return HttpResponse
85
     */
86 1
    public function send($content, int $status = HttpResponse::HTTP_OK, array $headers = []) : HttpResponse
87
    {
88 1
        $headers = array_merge($this->headers, $headers);
89
90 1
        $response = $this->response->create($content, $status, $headers);
91
92 1
        return $this->createOnly ? $response : $response->send();
93
    }
94
95
    /**
96
     * Create a new file download response.
97
     *
98
     * @param \SplFileInfo|string $file
99
     * @param string              $name
100
     * @param array               $headers
101
     * @param string|null         $disposition
102
     *
103
     * @return BinaryFileResponse
104
     */
105 3
    public function download(
106
        $file,
107
        string $name = null,
108
        array $headers = [],
109
        string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT
110
    ) : BinaryFileResponse {
111 3
        $response = new BinaryFileResponse($file, HttpResponse::HTTP_OK, $headers, true, $disposition);
112
113 2
        $response->prepare(HttpRequest::createFromGlobals());
114
115 2
        if ( ! is_null($name)) {
116 1
            $response = $response->setContentDisposition(
117 1
                $disposition,
118 1
                $name,
119 1
                iconv('UTF-8', 'ASCII//TRANSLIT', $name)
120
            );
121
        }
122
123 2
        return $this->createOnly ? $response : $response->send();
124
    }
125
}
126