Test Failed
Push — master ( 231921...a32b7c )
by Andy
02:05
created

Downloader::download()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Palmtree\Csv;
4
5
class Downloader extends Writer
6
{
7
    /**
8
     * Default headers used to tell client the response is
9
     * a downloadable, non-cacheable file.
10
     * @var array
11
     */
12
    protected $responseHeaders = [
13
        'Content-Type'              => 'application/octet-stream',
14
        'Content-Description'       => 'File Transfer',
15
        'Content-Transfer-Encoding' => 'Binary',
16
        'Content-Disposition'       => 'attachment; filename="%s"',
17
        'Expires'                   => '0',
18
        'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',
19
        'Pragma'                    => 'public',
20
        'Content-Length'            => '%s',
21
    ];
22
23
    protected $filename;
24
25
    public function __construct($filename = '', $responseHeaders = [])
26
    {
27
        if (!$filename) {
28
            $filename = time() . '.csv';
29
        }
30
31
        $this->setFilename($filename);
32
        $this->addResponseHeaders($responseHeaders);
33
34
        parent::__construct('php://temp');
35
    }
36
37
    public static function download($file, $data)
38
    {
39
        $downloader = new static($file);
40
        $downloader->setData($data);
41
        $downloader->sendResponse();
42
    }
43
44
    public function getResponseHeaders()
45
    {
46
        return $this->responseHeaders;
47
    }
48
49
    public function addResponseHeaders($userHeaders = [])
50
    {
51
        foreach ($userHeaders as $key => $value) {
52
            $this->addResponseHeader($key, $value);
53
        }
54
55
        return $this;
56
    }
57
58
    public function addResponseHeader($key, $value)
59
    {
60
        $this->responseHeaders[$key] = $value;
61
    }
62
63
    public function removeResponseHeader($key)
64
    {
65
        unset($this->responseHeaders[$key]);
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getFilename()
72
    {
73
        return $this->filename;
74
    }
75
76
    /**
77
     * @param string $filename
78
     *
79
     * @return Downloader
80
     */
81
    public function setFilename($filename)
82
    {
83
        $this->filename = $filename;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Attempts to send the file as a download to the client.
90
     *
91
     * @throws \Exception
92
     */
93
    public function sendResponse()
94
    {
95
        $body = $this->getResponseBody();
96
97
        if (!headers_sent()) {
98
            $headers = $this->getResponseHeaders();
99
100
            foreach ($headers as $key => $value) {
101
                if ($key === 'Content-Length') {
102
                    $value = sprintf($value, mb_strlen($body));
103
                } elseif ($key === 'Content-Disposition') {
104
                    $value = sprintf($value, $this->getFilename());
105
                }
106
107
                header(sprintf('%s: %s', $key, $value));
108
            }
109
        }
110
111
        print $body;
112
113
        $this->closeFileHandle();
114
    }
115
116
    protected function getResponseBody()
117
    {
118
        $this->trimTrailingLineEnding();
119
120
        rewind($this->getFileHandle());
121
122
        $body = stream_get_contents($this->getFileHandle());
123
124
        return $body;
125
    }
126
}
127