Passed
Push — main ( 75e993...93dd37 )
by Daniel
04:08
created

SimpleFileStreamable::createStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Io;
6
7
use Nyholm\Psr7\Factory\Psr17Factory;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\StreamInterface;
10
use Uxmp\Core\Orm\Model\SongInterface;
11
12
final class SimpleFileStreamable implements StreamableInterface
13
{
14 2
    public function __construct(
15
        private readonly Psr17Factory $psr17Factory
16
    ) {
17
    }
18
19 1
    public function stream(
20
        ResponseInterface $response,
21
        SongInterface $song,
22
    ): ResponseInterface {
23 1
        $size = $song->getFileSize();
24
25
        return $response
26 1
            ->withHeader('Content-Type', (string) $song->getMimeType())
27 1
            ->withHeader('Content-Disposition', sprintf('attachment; filename=song%d.mp3', $song->getId()))
28 1
            ->withHeader('Content-Length', (string) $size)
29 1
            ->withHeader('Cache-Control', 'no-cache')
30 1
            ->withHeader('Content-Range', 'bytes 0-'.$size)
31
            //->withHeader('Accept-Ranges', 'bytes') @todo add seek
32 1
            ->withBody(
33 1
                $this->createStream($song)
34
            );
35
    }
36
37 1
    public function createStream(SongInterface $song): StreamInterface
38
    {
39 1
        return $this->psr17Factory->createStreamFromFile($song->getFilename());
40
    }
41
}
42