Completed
Push — master ( ef87bf...ce0503 )
by Andy
02:16
created

Downloader::addResponseHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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 function getResponseHeaders()
38
    {
39
        return $this->responseHeaders;
40
    }
41
42
    public function addResponseHeaders($userHeaders = [])
43
    {
44
        foreach ($userHeaders as $key => $value) {
45
            $this->addResponseHeader($key, $value);
46
        }
47
48
        return $this;
49
    }
50
51
    public function addResponseHeader($key, $value)
52
    {
53
        $this->responseHeaders[$key] = $value;
54
    }
55
56
    public function removeResponseHeader($key)
57
    {
58
        unset($this->responseHeaders[$key]);
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getFilename()
65
    {
66
        return $this->filename;
67
    }
68
69
    /**
70
     * @param string $filename
71
     *
72
     * @return Downloader
73
     */
74
    public function setFilename($filename)
75
    {
76
        $this->filename = $filename;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Attempts to send the file as a download to the client.
83
     *
84
     * @throws \Exception
85
     */
86
    public function download()
87
    {
88
        $body = $this->getResponseBody();
89
90
        if (!headers_sent()) {
91
            $headers = $this->getResponseHeaders();
92
93
            foreach ($headers as $key => $value) {
94
                if ($key === 'Content-Length') {
95
                    $value = sprintf($value, mb_strlen($body));
96
                } elseif ($key === 'Content-Disposition') {
97
                    $value = sprintf($value, $this->getFilename());
98
                }
99
100
                header(sprintf('%s: %s', $key, $value));
101
            }
102
        }
103
104
        print $body;
105
106
        $this->closeFileHandle();
107
    }
108
109
    protected function getResponseBody()
110
    {
111
        $this->trimTrailingLineEnding();
112
113
        rewind($this->getFileHandle());
114
115
        $body = stream_get_contents($this->getFileHandle());
116
117
        return $body;
118
    }
119
}
120