Completed
Push — master ( f459d5...0fcc77 )
by Hannes
03:16
created

SymfonyResponse::fileDownload()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 6
cts 9
cp 0.6667
rs 9.3142
cc 3
eloc 12
nc 2
nop 2
crap 3.3332
1
<?php
2
namespace Madewithlove\Export\Http;
3
4
use Psr\Http\Message\StreamInterface;
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
7
use Symfony\Component\HttpFoundation\StreamedResponse;
8
9
trait SymfonyResponse
10
{
11
    /**
12
     * @param string $content
13
     * @param string $filename
14
     *
15
     * @return \Symfony\Component\HttpFoundation\Response
16
     */
17 3
    private function fileDownload($content, $filename)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
18
    {
19 3
        if ($content instanceof StreamInterface) {
20
            $response = new StreamedResponse();
21
22
            $response->setCallback(function () use ($content) {
23
                $content->rewind();
24
25
                while (! $content->eof()) {
26
                    echo $content->read(8192);
27
                }
28
            });
29
        } else {
30 3
            $response = new Response($content);
31
        }
32
33 3
        $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
34 3
        $response->headers->set('Content-Disposition', $disposition);
35
36 3
        return $response;
37
    }
38
}
39