Passed
Push — master ( 69b17c...082853 )
by Terry
02:44
created

StreamFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 27
c 0
b 0
f 0
dl 0
loc 90
ccs 36
cts 36
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNew() 0 6 1
A assertResource() 0 5 2
A createStreamFromResource() 0 9 2
A createStream() 0 10 1
A createStreamFromFile() 0 23 4
1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Psr17;
14
15
use Psr\Http\Message\StreamInterface;
16
use Psr\Http\Message\StreamFactoryInterface;
17
use Shieldon\Psr7\Stream;
18
use InvalidArgumentException;
19
use RuntimeException;
20
21
use function fopen;
22
use function fwrite;
23
use function is_resource;
24
use function preg_match;
25
use function rewind;
26
27
/**
28
 * PSR-17 Stream Factory
29
 */
30
class StreamFactory implements StreamFactoryInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 8
    public function createStream(string $content = ''): StreamInterface
36
    {
37 8
        $resource = @fopen('php://temp', 'r+');
38
39 8
        self::assertResource($resource);
40
41 8
        fwrite($resource, $content);
42 8
        rewind($resource);
43
44 8
        return $this->createStreamFromResource($resource);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 5
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
51
    {
52 5
        if ($mode === '' || !preg_match('/^[rwaxce]{1}[bt]{0,1}[+]{0,1}+$/', $mode)) {
53 1
            throw new InvalidArgumentException(
54 1
                sprintf(
55 1
                    'Invalid file opening mode "%s"',
56 1
                    $mode
57 1
                )
58 1
            );
59
        }
60
61 4
        $resource = @fopen($filename, $mode);
62
63 4
        if (!is_resource($resource)) {
64 1
            throw new RuntimeException(
65 1
                sprintf(
66 1
                    'Unable to open file at "%s"',
67 1
                    $filename
68 1
                )
69 1
            );
70
        }
71
72 3
        return new Stream($resource);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 9
    public function createStreamFromResource($resource): StreamInterface
79
    {
80 9
        if (!is_resource($resource)) {
81 1
            $resource = @fopen('php://temp', 'r+');
82
        }
83
84 9
        self::assertResource($resource);
85
86 9
        return new Stream($resource);
87
    }
88
89
    /*
90
    |--------------------------------------------------------------------------
91
    | Non PSR-7 Methods.
92
    |--------------------------------------------------------------------------
93
    */
94
95
    /**
96
     * Create a new Stream instance.
97
     *
98
     * @return StreamInterface
99
     */
100 1
    public static function fromNew(): StreamInterface
101
    {
102 1
        $resource = @fopen('php://temp', 'r+');
103 1
        self::assertResource($resource);
104
105 1
        return new Stream($resource);
106
    }
107
108
    /**
109
     * Throw an exception if input is not a valid PHP resource.
110
     *
111
     * @param mixed $resource
112
     *
113
     * @return void
114
     */
115 11
    protected static function assertResource($resource)
116
    {
117 11
        if (!is_resource($resource)) {
118 1
            throw new RuntimeException(
119 1
                'Unable to open "php://temp" resource.'
120 1
            );
121
        }
122
    }
123
}
124