Test Failed
Push — develop ( 8ee119...48c84c )
by Florian
02:36
created

StreamWrapper::stream_seek()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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));
1 ignored issue
show
Bug Best Practice introduced by
The expression return fopen('guzzle://s...StreamContext($stream)) could also return false which is incompatible with the documented return type resource. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
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)
3 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    public function stream_open(/** @scrutinizer ignore-unused */ $path, $mode, $options, &$opened_path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    public function stream_open($path, $mode, /** @scrutinizer ignore-unused */ $options, &$opened_path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $opened_path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

82
    public function stream_open($path, $mode, $options, /** @scrutinizer ignore-unused */ &$opened_path)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
1 ignored issue
show
Unused Code introduced by
The parameter $cast_as is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

123
    public function stream_cast(/** @scrutinizer ignore-unused */ $cast_as)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
2 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

157
    public function url_stat(/** @scrutinizer ignore-unused */ $path, $flags)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $flags is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

157
    public function url_stat($path, /** @scrutinizer ignore-unused */ $flags)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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