Completed
Branch 1.x (9055fe)
by Akihito
03:17
created

StreamResponder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 23 4
1
<?php
2
/**
3
 * This file is part of the BEAR.Streamer package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Streamer;
8
9
use BEAR\Resource\ResourceObject;
10
use BEAR\Sunday\Extension\Transfer\TransferInterface;
11
12
class StreamResponder implements TransferInterface
13
{
14
    /**
15
     * @var StreamerInterface
16
     */
17
    private $streamer;
18
19 1
    public function __construct(StreamerInterface $streamer)
20
    {
21 1
        $this->streamer = $streamer;
22 1
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 1
    public function __invoke(ResourceObject $resourceObject, array $server)
28
    {
29 1
        unset($server);
30
        // render
31 1
        if (! $resourceObject->view) {
32 1
            $resourceObject->toString();
33
        }
34
35
        // code
36 1
        http_response_code($resourceObject->code);
37
38
        // header
39 1
        foreach ($resourceObject->headers as $label => $value) {
40 1
            header("{$label}: {$value}", false);
41
        }
42
43
        // stream body
44 1
        $stream = $this->streamer->getStream($resourceObject->view);
45 1
        rewind($stream);
46 1
        while (! feof($stream)) {
47 1
            echo fread($stream, 8192);
48
        }
49 1
    }
50
}
51