Passed
Push — master ( 1d79f0...af841a )
by Jan
03:19
created

createZIPResponseFromFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 19
rs 9.8666
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Helpers;
20
21
22
use RuntimeException;
23
use Symfony\Component\HttpFoundation\BinaryFileResponse;
24
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
25
use ZipArchive;
26
27
class ZIPBinaryFileResponseFacade
28
{
29
30
    /**
31
     * Creates a ZIP file as Response from given data.
32
     * @param  array  $data The data as associative array of the form ['filename.txt' => content]
33
     * @param  string  $disposition_filename The name of the file that will be downloaded.
34
     * @return BinaryFileResponse
35
     */
36
    public static function createZIPResponseFromData(array $data, string $disposition_filename): BinaryFileResponse
37
    {
38
        $zip = new ZipArchive();
39
        $file_path = tempnam(sys_get_temp_dir(), 'stura');
40
        if (true === $zip->open($file_path, ZipArchive::CREATE)) {
41
            foreach ($data as $name => $content) {
42
                $zip->addFromString($name, $content);
43
            }
44
            $zip->close();
45
            $response = new BinaryFileResponse($file_path);
46
            //This line is important to delete the temp file afterwards
47
            $response->deleteFileAfterSend();
48
            $response->setPrivate();
49
            $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $disposition_filename);
50
51
            return $response;
52
        }
53
54
        throw new RuntimeException('Could the temporay ZIP archive.');
55
    }
56
57
    /**
58
     * Creates a ZIP file as Response from given data.
59
     * @param  array  $files The data as associative array of the form ['filename.txt' =>  '/path/on/dir']
60
     * @param  string  $disposition_filename The name of the file that will be downloaded.
61
     * @return BinaryFileResponse
62
     */
63
    public static function createZIPResponseFromFiles(array $data, string $disposition_filename): BinaryFileResponse
64
    {
65
        $zip = new ZipArchive();
66
        $file_path = tempnam(sys_get_temp_dir(), 'stura');
67
        if (true === $zip->open($file_path, ZipArchive::CREATE)) {
68
            foreach ($data as $name => $file) {
69
                $zip->addFile($file, $name);
70
            }
71
            $zip->close();
72
            $response = new BinaryFileResponse($file_path);
73
            //This line is important to delete the temp file afterwards
74
            $response->deleteFileAfterSend();
75
            $response->setPrivate();
76
            $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $disposition_filename);
77
78
            return $response;
79
        }
80
81
        throw new RuntimeException('Could the temporay ZIP archive.');
82
    }
83
}