StreamFactory::createStreamFromResource()   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 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message;
13
14
use Psr\Http\Message\StreamFactoryInterface;
15
use Psr\Http\Message\StreamInterface;
16
use Sunrise\Http\Message\Stream\FileStream;
17
use Sunrise\Http\Message\Stream\PhpTempStream;
18
19
/**
20
 * @psalm-suppress ClassMustBeFinal
21
 */
22
class StreamFactory implements StreamFactoryInterface
23
{
24
    /**
25
     * @inheritDoc
26
     */
27 8
    public function createStream(string $content = ''): StreamInterface
28
    {
29 8
        $stream = new PhpTempStream();
30 8
        if ($content === '') {
31 1
            return $stream;
32
        }
33
34 7
        $stream->write($content);
35 7
        $stream->rewind();
36
37 7
        return $stream;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43 3
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
44
    {
45 3
        return new FileStream($filename, $mode);
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 2
    public function createStreamFromResource($resource): StreamInterface
52
    {
53 2
        return new Stream($resource);
54
    }
55
}
56