Passed
Push — master ( 80df18...80df18 )
by Dāvis
04:16
created

StreamResponse::sendContent()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 9
nop 0
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class StreamResponse extends Response
9
{
10
    const BUFFER_SIZE = 4096;
11
12
    private $bufferSize;
13
14
    public function __construct(ResponseInterface $response, $bufferSize = self::BUFFER_SIZE)
15
    {
16
        parent::__construct(null, $response->getStatusCode(), $response->getHeaders());
17
18
        $this->content = $response->getBody();
19
        $this->bufferSize = $bufferSize;
20
    }
21
22
    public function sendContent()
23
    {
24
        $chunked = $this->headers->has('Transfer-Encoding');
25
        $this->content->seek(0);
26
27
        for (; ;) {
28
            $chunk = $this->content->read($this->bufferSize);
29
30
            if ($chunked) {
31
                echo sprintf("%x\r\n", \strlen($chunk));
32
            }
33
34
            echo $chunk;
35
36
            if ($chunked) {
37
                echo "\r\n";
38
            }
39
40
            flush();
41
42
            if (!$chunk) {
43
                return;
44
            }
45
        }
46
    }
47
48
    public function getContent()
49
    {
50
        return false;
51
    }
52
}
53