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

StreamFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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