| Total Complexity | 18 |
| Total Lines | 150 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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
|
|||
| 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) |
||
| 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) |
||
| 121 | } |
||
| 122 | |||
| 123 | public function stream_cast($cast_as) |
||
|
1 ignored issue
–
show
|
|||
| 124 | { |
||
| 125 | $stream = clone($this->stream); |
||
| 126 | |||
| 127 | return $stream->detach(); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function stream_stat() |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function url_stat($path, $flags) |
||
|
2 ignored issues
–
show
|
|||
| 158 | { |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | } |
||
| 176 |
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.