StreamFactory::createStreamFromResource()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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