StreamedFileResponse::getStreamedFileFunction()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.7333
cc 3
nc 1
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the CRUDlex package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CRUDlex;
13
14
/**
15
 * Small utility class to generate functions for streamed responses returning
16
 * a file.
17
 */
18
class StreamedFileResponse
19
{
20
21
    /**
22
     * Generates a lambda which is streaming the given file to standard output.
23
     *
24
     * @param string $file
25
     * the filename to stream
26
     *
27
     * @return \Closure
28
     * the generated lambda
29
     */
30 2
    public function getStreamedFileFunction($file)
31
    {
32
        return function() use ($file) {
33 2
            set_time_limit(0);
34 2
            $handle = fopen($file, 'rb');
35 2
            if ($handle !== false) {
36 2
                $chunkSize = 1024 * 1024;
37 2
                while (!feof($handle)) {
38 2
                    $buffer = fread($handle, $chunkSize);
39 2
                    echo $buffer;
40 2
                    flush();
41
                }
42 2
                fclose($handle);
43
            }
44 2
        };
45
    }
46
47
}
48