Passed
Push — master ( fb282f...fe0d43 )
by Terry
01:45
created

StreamFactory::fromNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    public function createStream(string $content = ''): StreamInterface
36
    {
37
        $resource = @fopen('php://temp', 'r+');
38
39
        self::assertResource($resource);
40
41
        fwrite($resource, $content);
42
        rewind($resource);
43
44
        return $this->createStreamFromResource($resource);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
51
    {
52
        if ($mode === '' || ! preg_match('/^[rwaxce]{1}[bt]{0,1}[+]{0,1}+$/', $mode)) {
53
            throw new InvalidArgumentException(
54
                sprintf(
55
                    'Invalid file opening mode "%s"',
56
                    $mode
57
                )
58
            );
59
        }
60
61
        $resource = @fopen($filename, $mode);
62
63
        if (! is_resource($resource)) {
64
            throw new RuntimeException(
65
                sprintf(
66
                    'Unable to open file at "%s"',
67
                    $filename
68
                )
69
            );
70
        }
71
72
        return new Stream($resource);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function createStreamFromResource($resource): StreamInterface
79
    {
80
        if (! is_resource($resource)) {
81
            $resource = @fopen('php://temp', 'r+');
82
        }
83
84
        self::assertResource($resource);
85
86
        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
    public function fromNew(): StreamInterface
101
    {
102
        $resource = @fopen('php://temp', 'r+');
103
        self::assertResource($resource);
104
105
        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
    protected static function assertResource($resource)
116
    {
117
        if (! is_resource($resource)) {
118
            throw new RuntimeException(
119
                'Unable to open "php://temp" resource.'
120
            );
121
        }
122
    }
123
124
125
126
127
}
128