Emitter::emit()
last analyzed

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
c 0
b 0
f 0
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Http;
6
7
use Laminas\HttpHandlerRunner\Emitter\EmitterInterface;
8
use Laminas\HttpHandlerRunner\Emitter\EmitterStack;
9
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
10
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter;
11
use Psr\Http\Message\ResponseInterface;
12
13
class Emitter implements EmitterInterface
14
{
15
    protected EmitterStack $stack;
16
17 14
    public function __construct(int $maxBufferLength = 8192)
18
    {
19 14
        $sapiStreamEmitter = new SapiStreamEmitter($maxBufferLength);
20 14
        $conditionalEmitter = new class ($sapiStreamEmitter) implements EmitterInterface {
21
            private $emitter;
22
23
            public function __construct(EmitterInterface $emitter)
24
            {
25 14
                $this->emitter = $emitter;
26
            }
27
28
            public function emit(ResponseInterface $response): bool
29
            {
30
                if (
31 14
                    !$response->hasHeader('Content-Disposition')
32 14
                    && !$response->hasHeader('Content-Range')
33
                ) {
34 13
                    return false;
35
                }
36
37 1
                return $this->emitter->emit($response);
38
            }
39 14
        };
40
41 14
        $this->stack = new EmitterStack();
42 14
        $this->stack->push(new SapiEmitter());
43 14
        $this->stack->push($conditionalEmitter);
44
    }
45
46 14
    public function emit(ResponseInterface $response): bool
47
    {
48 14
        return $this->stack->emit($response);
49
    }
50
}
51