Passed
Push — main ( 69b1a6...b43944 )
by Daniel
02:36
created

StreamResponder::writeResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 2
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Usox\HyperSonic\FeatureSet\V1161\Responder;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamInterface;
9
10
final class StreamResponder extends AbstractBinaryResponder
11
{
12
    /**
13
     * @param array{
14
     *  contentType: string,
15
     *  stream: StreamInterface,
16
     *  estimatedContentLength?: int
17
     * } $streamData
18
     */
19 2
    public function __construct(
20
        private readonly array $streamData,
21
    ) {
22
    }
23
24 1
    public function writeResponse(ResponseInterface $response): ResponseInterface
25
    {
26 1
        if (array_key_exists('estimatedContentLength', $this->streamData)) {
27 1
            $response = $response->withHeader(
28
                'Content-Length',
29 1
                (string) $this->streamData['estimatedContentLength'],
30
            );
31
        }
32
33
        return $response
34 1
            ->withBody($this->streamData['stream'])
35 1
            ->withHeader('Content-Transfer-Encoding', 'binary')
36 1
            ->withHeader('Cache-Control', 'no-cache')
37 1
            ->withHeader('Content-Type', $this->streamData['contentType']);
38
    }
39
}
40