Passed
Push — master ( a4ef1c...1fb27f )
by Florian
12:17 queued 12s
created

StreamWrapper   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 65
c 1
b 0
f 0
dl 0
loc 150
ccs 0
cts 102
cp 0
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
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 14 4
A stream_write() 0 3 1
A url_stat() 0 16 1
A stream_cast() 0 5 1
A stream_tell() 0 3 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 Psr\Http\Message\StreamInterface;
19
20
/**
21
 * Converts Guzzle streams into PHP stream resources.
22
 */
23
class StreamWrapper
24
{
25
    /** @var resource */
26
    public $context;
27
28
    /** @var StreamInterface */
29
    private $stream;
30
31
    /** @var string r, r+, or w */
32
    private $mode;
33
34
    /**
35
     * Returns a resource representing the stream.
36
     *
37
     * @param StreamInterface $stream The stream to get a resource for
38
     *
39
     * @return resource
40
     * @throws \InvalidArgumentException if stream is not readable or writable
41
     */
42
    public static function getResource(StreamInterface $stream)
43
    {
44
        self::register();
45
46
        if ($stream->isReadable()) {
47
            $mode = $stream->isWritable() ? 'r+' : 'r';
48
        } elseif ($stream->isWritable()) {
49
            $mode = 'w';
50
        } else {
51
            throw new \InvalidArgumentException('The stream must be readable, '
52
                . 'writable, or both.');
53
        }
54
55
        return fopen('guzzle://stream', $mode, null, self::createStreamContext($stream));
56
    }
57
58
    /**
59
     * Creates a stream context that can be used to open a stream as a php stream resource.
60
     *
61
     * @param StreamInterface $stream
62
     *
63
     * @return resource
64
     */
65
    public static function createStreamContext(StreamInterface $stream)
66
    {
67
        return stream_context_create([
68
            'guzzle' => ['stream' => $stream]
69
        ]);
70
    }
71
72
    /**
73
     * Registers the stream wrapper if needed
74
     */
75
    public static function register()
76
    {
77
        if (!in_array('guzzle', stream_get_wrappers())) {
78
            stream_wrapper_register('guzzle', __CLASS__);
79
        }
80
    }
81
82
    public function stream_open($path, $mode, $options, &$opened_path)
83
    {
84
        $options = stream_context_get_options($this->context);
85
86
        if (!isset($options['guzzle']['stream'])) {
87
            return false;
88
        }
89
90
        $this->mode = $mode;
91
        $this->stream = $options['guzzle']['stream'];
92
93
        return true;
94
    }
95
96
    public function stream_read($count)
97
    {
98
        return $this->stream->read($count);
99
    }
100
101
    public function stream_write($data)
102
    {
103
        return (int) $this->stream->write($data);
104
    }
105
106
    public function stream_tell()
107
    {
108
        return $this->stream->tell();
109
    }
110
111
    public function stream_eof()
112
    {
113
        return $this->stream->eof();
114
    }
115
116
    public function stream_seek($offset, $whence)
117
    {
118
        $this->stream->seek($offset, $whence);
119
120
        return true;
121
    }
122
123
    public function stream_cast($cast_as)
124
    {
125
        $stream = clone($this->stream);
126
127
        return $stream->detach();
128
    }
129
130
    public function stream_stat()
131
    {
132
        static $modeMap = [
133
            'r'  => 33060,
134
            'rb' => 33060,
135
            'r+' => 33206,
136
            'w'  => 33188,
137
            'wb' => 33188
138
        ];
139
140
        return [
141
            'dev'     => 0,
142
            'ino'     => 0,
143
            'mode'    => $modeMap[$this->mode],
144
            'nlink'   => 0,
145
            'uid'     => 0,
146
            'gid'     => 0,
147
            'rdev'    => 0,
148
            'size'    => $this->stream->getSize() ?: 0,
149
            'atime'   => 0,
150
            'mtime'   => 0,
151
            'ctime'   => 0,
152
            'blksize' => 0,
153
            'blocks'  => 0
154
        ];
155
    }
156
157
    public function url_stat($path, $flags)
158
    {
159
        return [
160
            'dev'     => 0,
161
            'ino'     => 0,
162
            'mode'    => 0,
163
            'nlink'   => 0,
164
            'uid'     => 0,
165
            'gid'     => 0,
166
            'rdev'    => 0,
167
            'size'    => 0,
168
            'atime'   => 0,
169
            'mtime'   => 0,
170
            'ctime'   => 0,
171
            'blksize' => 0,
172
            'blocks'  => 0
173
        ];
174
    }
175
}
176