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

StreamFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 51
rs 10

3 Methods

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