StreamFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 8
c 0
b 0
f 0
dl 0
loc 33
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createStreamFromFile() 0 3 1
A createStream() 0 6 1
A createStreamFromResource() 0 7 2
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