Passed
Push — master ( b65012...5ddc87 )
by Zlatin
01:29
created

StreamFactory::createStream()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use DevOp\Core\Http\Stream;
5
use Psr\Http\Message\StreamInterface;
6
use Interop\Http\Factory\StreamFactoryInterface;
7
8
class StreamFactory implements StreamFactoryInterface
9
{
10
11
    /**
12
     * @param string $content
13
     * @return StreamInterface
14
     */
15 48
    public function createStream($content = '')
16
    {
17 48
        $resource = fopen("php://temp", "w+b");
18
19 48
        if (!is_resource($resource)) {
20
            throw new \InvalidArgumentException('Error while creating PHP stream');
21
        }
22
23 48
        fwrite($resource, $content);
24 48
        rewind($resource);
25
26 48
        return $this->createStreamFromResource($resource);
27
    }
28
29
    /**
30
     * @param string $filename
31
     * @param string $mode
32
     * @return StreamInterface
33
     */
34 30
    public function createStreamFromFile($filename, $mode = 'r')
35
    {
36 30
        $resource = fopen($filename, $mode);
37
38 30
        if (!is_resource($resource)) {
39
            throw new \InvalidArgumentException('Error while creating PHP stream');
40
        }
41
42 30
        return $this->createStreamFromResource($resource);
43
    }
44
45
    /**
46
     * @param resource $resource
47
     * @return StreamInterface
48
     */
49 80
    public function createStreamFromResource($resource)
50
    {
51 80
        return new Stream($resource);
52
    }
53
}
54