StreamedFileResponse   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 30
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getStreamedFileFunction() 0 16 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