Completed
Push — master ( b8b57a...0791f9 )
by Josh
10s
created

StreamFactory::createStreamFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace CodeJet\Http\Factory;
4
5
use CodeJet\Http\Stream;
6
use Interop\Http\Factory\StreamFactoryInterface;
7
use Psr\Http\Message\StreamInterface;
8
9
class StreamFactory implements StreamFactoryInterface
10
{
11
    /**
12
     * Create a new stream from a string.
13
     *
14
     * The stream SHOULD be created with a temporary resource.
15
     *
16
     * @param string $content
17
     *
18
     * @return StreamInterface
19
     */
20
    public function createStream($content = '')
21
    {
22
        $handle = fopen('php://temp', 'w+');
23
        fwrite($handle, $content);
24
25
        return new Stream($handle);
26
    }
27
28
    /**
29
     * Create a stream from an existing file.
30
     *
31
     * The file MUST be opened using the given mode, which may be any mode
32
     * supported by the `fopen` function.
33
     *
34
     * @param string $file
35
     * @param string $mode
36
     *
37
     * @return StreamInterface
38
     */
39
    public function createStreamFromFile($file, $mode = 'r')
40
    {
41
        return new Stream(
42
            fopen($file, $mode)
43
        );
44
    }
45
46
    /**
47
     * Create a new stream from an existing resource.
48
     *
49
     * The stream MUST be readable and may be writable.
50
     *
51
     * @param resource $resource
52
     *
53
     * @return StreamInterface
54
     */
55
    public function createStreamFromResource($resource)
56
    {
57
        return new Stream($resource);
58
    }
59
}
60