Passed
Push — master ( 75715f...53a757 )
by Andy
05:10
created

Downloader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 100
ccs 0
cts 53
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A download() 0 6 1
A sendResponse() 0 15 3
A getResponseHeaders() 0 4 1
A addResponseHeaders() 0 8 2
A addResponseHeader() 0 4 1
A removeResponseHeader() 0 4 1
A getFilename() 0 4 1
A setFilename() 0 6 1
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'              => 'text/csv',
14
        'Content-Description'       => 'File Transfer',
15
        'Content-Transfer-Encoding' => 'Binary',
16
        'Expires'                   => '0',
17
        'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',
18
        'Pragma'                    => 'public',
19
    ];
20
21
    protected $filename;
22
23
    public function __construct($filename, $responseHeaders = [])
24
    {
25
        $this->setFilename($filename);
26
27
        $this->addResponseHeader('Content-Disposition', sprintf('attachment; filename="%s"', $this->getFilename()));
28
        $this->addResponseHeaders($responseHeaders);
29
30
        parent::__construct('php://temp');
31
    }
32
33
    public static function download($file, $data)
34
    {
35
        $downloader = new static($file);
36
        $downloader->setData($data);
37
        $downloader->sendResponse();
38
    }
39
40
    /**
41
     * Attempts to send the file as a download to the client.
42
     *
43
     * @throws \Exception
44
     */
45
    public function sendResponse()
46
    {
47
        if (!headers_sent()) {
48
            $headers = $this->getResponseHeaders();
49
50
            foreach ($headers as $key => $value) {
51
                header(sprintf('%s: %s', $key, $value));
52
            }
53
        }
54
55
        $this->getDocument()->trimFinalLineEnding();
56
57
        $this->getDocument()->fseek(0);
58
        $this->getDocument()->fpassthru();
59
    }
60
61
    public function getResponseHeaders()
62
    {
63
        return $this->responseHeaders;
64
    }
65
66
    public function addResponseHeaders($headers = [])
67
    {
68
        foreach ($headers as $key => $value) {
69
            $this->addResponseHeader($key, $value);
70
        }
71
72
        return $this;
73
    }
74
75
    public function addResponseHeader($key, $value)
76
    {
77
        $this->responseHeaders[$key] = $value;
78
    }
79
80
    public function removeResponseHeader($key)
81
    {
82
        unset($this->responseHeaders[$key]);
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getFilename()
89
    {
90
        return $this->filename;
91
    }
92
93
    /**
94
     * @param string $filename
95
     *
96
     * @return Downloader
97
     */
98
    public function setFilename($filename)
99
    {
100
        $this->filename = $filename;
101
102
        return $this;
103
    }
104
}
105