createZIPResponseFromData()   A
last analyzed

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
use RuntimeException;
22
use Symfony\Component\HttpFoundation\BinaryFileResponse;
23
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
24
use ZipArchive;
25
26
class ZIPBinaryFileResponseFacade
27
{
28
    /**
29
     * Creates a ZIP file as Response from given data.
30
     *
31
     * @param string[]  $data                 The data as associative array of the form ['filename.txt' => content]
32
     * @param string $disposition_filename The name of the file that will be downloaded.
33
     */
34
    public static function createZIPResponseFromData(array $data, string $disposition_filename): BinaryFileResponse
35
    {
36
        $zip = new ZipArchive();
37
        $file_path = tempnam(sys_get_temp_dir(), 'stura');
38
        if (true === $zip->open($file_path, ZipArchive::OVERWRITE)) {
39
            foreach ($data as $name => $content) {
40
                $zip->addFromString($name, $content);
41
            }
42
            $zip->close();
43
            $response = new BinaryFileResponse($file_path);
44
            //This line is important to delete the temp file afterwards
45
            $response->deleteFileAfterSend();
46
            $response->setPrivate();
47
            $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $disposition_filename);
48
49
            return $response;
50
        }
51
52
        throw new RuntimeException('Could the temporay ZIP archive.');
53
    }
54
55
    /**
56
     * Creates a ZIP file as Response from given data.
57
     *
58
     * @param \SplFileInfo[] $data                 The data as associative array of the form ['filename.txt' =>  '/path/on/dir']
59
     * @param string $disposition_filename The name of the file that will be downloaded.
60
     */
61
    public static function createZIPResponseFromFiles(array $data, string $disposition_filename): BinaryFileResponse
62
    {
63
        $zip = new ZipArchive();
64
        $file_path = tempnam(sys_get_temp_dir(), 'stura');
65
        if (true === $zip->open($file_path, ZipArchive::CREATE)) {
66
            foreach ($data as $name => $file) {
67
                $zip->addFile($file, $name);
68
            }
69
            $zip->close();
70
            $response = new BinaryFileResponse($file_path);
71
            //This line is important to delete the temp file afterwards
72
            $response->deleteFileAfterSend();
73
            $response->setPrivate();
74
            $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $disposition_filename);
75
76
            return $response;
77
        }
78
79
        throw new RuntimeException('Could the temporay ZIP archive.');
80
    }
81
}
82