ByteString::getLastModifiedDate()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\FileWaiter\Adapter;
6
7
use Psr\Http\Message\StreamFactoryInterface;
8
use Psr\Http\Message\StreamInterface;
9
use Stadly\FileWaiter\Adapter;
10
use Stadly\Http\Header\Value\Date;
11
use Stadly\Http\Header\Value\EntityTag\EntityTag;
12
use Stadly\Http\Header\Value\MediaType\MediaType;
13
use finfo;
14
15
/**
16
 * Adapter for handling files represented as a byte string.
17
 */
18
final class ByteString implements Adapter
19
{
20
    /**
21
     * @var string Content of the byte string.
22
     */
23
    private $content;
24
25
    /**
26
     * @var StreamFactoryInterface Factory for creating streams.
27
     */
28
    private $streamFactory;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string $content Content of the byte string.
34
     * @param StreamFactoryInterface $streamFactory Factory for creating streams.
35
     */
36 2
    public function __construct(string $content, StreamFactoryInterface $streamFactory)
37
    {
38 2
        $this->content = $content;
39 2
        $this->streamFactory = $streamFactory;
40 2
    }
41
42
    /**
43
     * @return StreamInterface File stream that can be used to read from the file.
44
     */
45 1
    public function getFileStream(): StreamInterface
46
    {
47 1
        return $this->streamFactory->createStream($this->content);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function getFileName(): ?string
54
    {
55 1
        return null;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 2
    public function getFileSize(): ?int
62
    {
63 2
        return mb_strlen($this->content, /*encoding*/'8bit');
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 2
    public function getMediaType(): ?MediaType
70
    {
71 2
        $fileInfo = new finfo(FILEINFO_MIME_TYPE);
72 2
        $mimeType = $fileInfo->buffer($this->content);
73
74 2
        if ($mimeType === false) {
75
            // @codeCoverageIgnoreStart
76
            return null;
77
            // @codeCoverageIgnoreEnd
78
        }
79
80 2
        return MediaType::fromString($mimeType);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 1
    public function getLastModifiedDate(): ?Date
87
    {
88 1
        return null;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function getEntityTag(): ?EntityTag
95
    {
96 1
        return new EntityTag(md5($this->content));
97
    }
98
}
99