StreamWrapper   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 199
ccs 0
cts 42
cp 0
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A stream_tell() 0 3 1
A stream_open() 0 12 2
A stream_seek() 0 5 1
A stream_stat() 0 24 2
A register() 0 4 2
A createStreamContext() 0 4 1
A getResource() 0 17 4
A stream_write() 0 3 1
A url_stat() 0 16 1
A stream_cast() 0 5 1
A stream_eof() 0 3 1
A stream_read() 0 3 1
1
<?php
2
// phpcs:ignoreFile
3
4
/**
5
 * This file has been taken from guzzle/psr7 so that the library can be used
6
 * without the guzzle dependency.
7
 *
8
 * @copyright Copyright (c) 2015 Michael Dowling
9
 * @author    Michael Dowling <[email protected]>
10
 * @link      https://github.com/mtdowling
11
 * @license   https://opensource.org/licenses/MIT MIT License
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phauthentic\Infrastructure\Storage\Utility;
17
18
use InvalidArgumentException;
19
use Psr\Http\Message\StreamInterface;
20
21
use function \Phauthentic\Infrastructure\Storage\fopen;
0 ignored issues
show
introduced by
The function \Phauthentic\Infrastructure\Storage\fopen was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
22
23
/**
24
 * Converts Guzzle streams into PHP stream resources.
25
 */
26
class StreamWrapper
27
{
28
    /** @var resource */
29
    public $context;
30
31
    /** @var StreamInterface */
32
    private StreamInterface $stream;
33
34
    /** @var string r, r+, or w */
35
    private string $mode;
36
37
    /**
38
     * Returns a resource representing the stream.
39
     *
40
     * @param StreamInterface $stream The stream to get a resource for
41
     *
42
     * @return resource
43
     * @throws \InvalidArgumentException if stream is not readable or writable
44
     */
45
    public static function getResource(StreamInterface $stream)
46
    {
47
        self::register();
48
49
        if ($stream->isReadable()) {
50
            $mode = $stream->isWritable() ? 'r+' : 'r';
51
        } elseif ($stream->isWritable()) {
52
            $mode = 'w';
53
        } else {
54
            throw new InvalidArgumentException(
55
                'The stream must be readable, writable, or both.'
56
            );
57
        }
58
59
        $result = fopen('guzzle://stream', $mode, false, self::createStreamContext($stream));
60
61
        return $result;
62
    }
63
64
    /**
65
     * Creates a stream context that can be used to open a stream as a php stream resource.
66
     *
67
     * @param StreamInterface $stream
68
     *
69
     * @return resource
70
     */
71
    public static function createStreamContext(StreamInterface $stream)
72
    {
73
        return \stream_context_create([
74
            'guzzle' => ['stream' => $stream]
75
        ]);
76
    }
77
78
    /**
79
     * Registers the stream wrapper if needed
80
     *
81
     * @return void
82
     */
83
    public static function register()
84
    {
85
        if (!in_array('guzzle', stream_get_wrappers(), true)) {
86
            \stream_wrapper_register('guzzle', __CLASS__);
87
        }
88
    }
89
90
    /**
91
     * @param string $path
92
     * @param string $mode
93
     * @param array $options
94
     * @param string $opened_path
95
     *
96
     * @return bool
97
     */
98
    public function stream_open($path, $mode, $options, &$opened_path)
99
    {
100
        $options = stream_context_get_options($this->context);
101
102
        if (!isset($options['guzzle']['stream'])) {
103
            return false;
104
        }
105
106
        $this->mode = $mode;
107
        $this->stream = $options['guzzle']['stream'];
108
109
        return true;
110
    }
111
112
    /**
113
     * @param int $count
114
     *
115
     * @return string
116
     */
117
    public function stream_read($count)
118
    {
119
        return $this->stream->read($count);
120
    }
121
122
    /**
123
     * @param string $data
124
     *
125
     * @return int
126
     */
127
    public function stream_write($data)
128
    {
129
        return (int) $this->stream->write($data);
130
    }
131
132
    /**
133
     * @return int
134
     */
135
    public function stream_tell()
136
    {
137
        return $this->stream->tell();
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function stream_eof()
144
    {
145
        return $this->stream->eof();
146
    }
147
148
    /**
149
     * @param int $offset
150
     * @param int $whence
151
     *
152
     * @return bool
153
     */
154
    public function stream_seek($offset, $whence)
155
    {
156
        $this->stream->seek($offset, $whence);
157
158
        return true;
159
    }
160
161
    /**
162
     * @param int $cast_as
163
     *
164
     * @return resource|null
165
     */
166
    public function stream_cast($cast_as)
167
    {
168
        $stream = clone($this->stream);
169
170
        return $stream->detach();
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function stream_stat()
177
    {
178
        static $modeMap = [
179
            'r'  => 33060,
180
            'rb' => 33060,
181
            'r+' => 33206,
182
            'w'  => 33188,
183
            'wb' => 33188
184
        ];
185
186
        return [
187
            'dev'     => 0,
188
            'ino'     => 0,
189
            'mode'    => $modeMap[$this->mode],
190
            'nlink'   => 0,
191
            'uid'     => 0,
192
            'gid'     => 0,
193
            'rdev'    => 0,
194
            'size'    => $this->stream->getSize() ?: 0,
195
            'atime'   => 0,
196
            'mtime'   => 0,
197
            'ctime'   => 0,
198
            'blksize' => 0,
199
            'blocks'  => 0
200
        ];
201
    }
202
203
    /**
204
     * @param string $path
205
     * @param int $flags
206
     *
207
     * @return int[]
208
     */
209
    public function url_stat($path, $flags)
210
    {
211
        return [
212
            'dev'     => 0,
213
            'ino'     => 0,
214
            'mode'    => 0,
215
            'nlink'   => 0,
216
            'uid'     => 0,
217
            'gid'     => 0,
218
            'rdev'    => 0,
219
            'size'    => 0,
220
            'atime'   => 0,
221
            'mtime'   => 0,
222
            'ctime'   => 0,
223
            'blksize' => 0,
224
            'blocks'  => 0
225
        ];
226
    }
227
}
228