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

SimpleFileStreamable   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 28
ccs 12
cts 12
cp 1
rs 10
c 2
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createStream() 0 3 1
A stream() 0 15 1
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