StreamFactory::createStream()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Message;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\StreamFactoryInterface;
9
use Psr\Http\Message\StreamInterface;
10
11
use function is_resource;
12
13
final class StreamFactory implements StreamFactoryInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 5
    public function createStream(string $content = ''): StreamInterface
19
    {
20 5
        $stream = new Stream();
21 5
        $stream->write($content);
22 5
        $stream->rewind();
23 5
        return $stream;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 4
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
30
    {
31 4
        return new Stream($filename, $mode);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @psalm-suppress DocblockTypeContradiction
38
     */
39 2
    public function createStreamFromResource($resource): StreamInterface
40
    {
41 2
        if (!is_resource($resource)) {
42 1
            throw new InvalidArgumentException('Invalid stream provided. It must be a stream resource.');
43
        }
44
45 1
        return new Stream($resource);
46
    }
47
}
48