| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use WCPOS\Vendor\GuzzleHttp\Psr7\Exception\TimeoutException; |
| 7 |
use WCPOS\Vendor\Psr\Http\Message\StreamInterface; |
| 8 |
/** |
| 9 |
* @internal |
| 10 |
*/ |
| 11 |
final class StreamTimeout |
| 12 |
{ |
| 13 |
private function __construct() |
| 14 |
{ |
| 15 |
} |
| 16 |
public static function read(StreamInterface $stream, int $length, string $timeoutMessage) : string |
| 17 |
{ |
| 18 |
try { |
| 19 |
$buffer = $stream->read($length); |
| 20 |
} catch (TimeoutException $e) { |
| 21 |
throw $e; |
| 22 |
} catch (\RuntimeException $e) { |
| 23 |
self::throwIfReadTimedOut($stream, $timeoutMessage, $e); |
| 24 |
throw $e; |
| 25 |
} |
| 26 |
if ($buffer === '') { |
| 27 |
self::throwIfReadTimedOut($stream, $timeoutMessage); |
| 28 |
} |
| 29 |
return $buffer; |
| 30 |
} |
| 31 |
public static function throwIfReadTimedOut(StreamInterface $stream, string $message, ?\Throwable $previous = null) : void |
| 32 |
{ |
| 33 |
if (self::isReadTimedOut($stream)) { |
| 34 |
throw new TimeoutException($message, 0, $previous); |
| 35 |
} |
| 36 |
} |
| 37 |
public static function throwIfWriteTimedOut(StreamInterface $stream, ?\Throwable $previous = null) : void |
| 38 |
{ |
| 39 |
if (self::isWriteTimedOut($stream)) { |
| 40 |
throw new TimeoutException('Unable to write to stream: timed out', 0, $previous); |
| 41 |
} |
| 42 |
} |
| 43 |
public static function isReadTimedOut(StreamInterface $stream) : bool |
| 44 |
{ |
| 45 |
try { |
| 46 |
if ($stream->getMetadata('timed_out') !== \true) { |
| 47 |
return \false; |
| 48 |
} |
| 49 |
return !$stream->eof(); |
| 50 |
} catch (\Throwable $e) { |
| 51 |
return \false; |
| 52 |
} |
| 53 |
} |
| 54 |
public static function isWriteTimedOut(StreamInterface $stream) : bool |
| 55 |
{ |
| 56 |
try { |
| 57 |
return $stream->getMetadata('timed_out') === \true; |
| 58 |
} catch (\Throwable $e) { |
| 59 |
return \false; |
| 60 |
} |
| 61 |
} |
| 62 |
/** |
| 63 |
* @param resource $resource |
| 64 |
*/ |
| 65 |
public static function isResourceReadTimedOut($resource) : bool |
| 66 |
{ |
| 67 |
try { |
| 68 |
/** @var array<string, mixed> $metadata */ |
| 69 |
$metadata = \stream_get_meta_data($resource); |
| 70 |
return ($metadata['timed_out'] ?? \false) === \true && !\feof($resource); |
| 71 |
} catch (\Throwable $e) { |
| 72 |
return \false; |
| 73 |
} |
| 74 |
} |
| 75 |
/** |
| 76 |
* @param resource $resource |
| 77 |
*/ |
| 78 |
public static function isResourceWriteTimedOut($resource) : bool |
| 79 |
{ |
| 80 |
try { |
| 81 |
/** @var array<string, mixed> $metadata */ |
| 82 |
$metadata = \stream_get_meta_data($resource); |
| 83 |
return ($metadata['timed_out'] ?? \false) === \true; |
| 84 |
} catch (\Throwable $e) { |
| 85 |
return \false; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|