Passed
Pull Request — master (#27)
by Anatoly
39:10
created

StreamFactory::createStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
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
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\StreamFactoryInterface;
18
use Psr\Http\Message\StreamInterface;
19
use Sunrise\Http\Message\Stream\FileStream;
20
use Sunrise\Http\Message\Stream\PhpTempStream;
21
22
/**
23
 * StreamFactory
24
 *
25
 * @link https://www.php-fig.org/psr/psr-17/
26
 */
27
class StreamFactory implements StreamFactoryInterface
28
{
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 5
    public function createStream(string $content = ''): StreamInterface
34
    {
35 5
        $stream = new PhpTempStream();
36 5
        if ($content === '') {
37 1
            return $stream;
38
        }
39
40 4
        $stream->write($content);
41 4
        $stream->rewind();
42
43 4
        return $stream;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
50
    {
51 3
        return new FileStream($filename, $mode);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function createStreamFromResource($resource): StreamInterface
58
    {
59 2
        return new Stream($resource);
60
    }
61
}
62