| 1 |
<?php |
| 2 |
|
| 3 |
declare( strict_types = 1 ); |
| 4 |
namespace MediaWiki\Http; |
| 5 |
|
| 6 |
use GuzzleHttp\Psr7\Utils; |
| 7 |
use LogicException; |
| 8 |
use MWHttpRequest; |
| 9 |
use Psr\Http\Message\ResponseInterface; |
| 10 |
use Psr\Http\Message\StreamInterface; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class MwHttpRequestToResponseInterfaceAdapter implements ResponseInterface { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private $mwHttpRequest; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( MWHttpRequest $mwHttpRequest ) { |
| 30 |
$this->validateHasResponse( $mwHttpRequest ); |
| 31 |
$this->mwHttpRequest = $mwHttpRequest; |
| 32 |
} |
| 33 |
|
| 34 |
public function getProtocolVersion(): string { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
throw new LogicException( __METHOD__ . ' is not implemented' ); |
| 39 |
} |
| 40 |
|
| 41 |
public function withProtocolVersion( $version ): self { |
| 42 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 43 |
} |
| 44 |
|
| 45 |
public function getHeaders(): array { |
| 46 |
return $this->mwHttpRequest->getResponseHeaders(); |
| 47 |
} |
| 48 |
|
| 49 |
public function hasHeader( $name ): bool { |
| 50 |
return isset( $this->mwHttpRequest->getResponseHeaders()[$name] ); |
| 51 |
} |
| 52 |
|
| 53 |
public function getHeader( $name ): array { |
| 54 |
return $this->hasHeader( $name ) ? $this->mwHttpRequest->getResponseHeaders()[$name] : []; |
| 55 |
} |
| 56 |
|
| 57 |
public function getHeaderLine( $name ): string { |
| 58 |
return $this->hasHeader( $name ) |
| 59 |
? implode( ',', $this->mwHttpRequest->getResponseHeaders()[$name] ) |
| 60 |
: ''; |
| 61 |
} |
| 62 |
|
| 63 |
public function withHeader( $name, $value ): self { |
| 64 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 65 |
} |
| 66 |
|
| 67 |
public function withAddedHeader( $name, $value ): self { |
| 68 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 69 |
} |
| 70 |
|
| 71 |
public function withoutHeader( $name ): self { |
| 72 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 73 |
} |
| 74 |
|
| 75 |
public function getBody(): StreamInterface { |
| 76 |
return Utils::streamFor( $this->mwHttpRequest->getContent() ); |
| 77 |
} |
| 78 |
|
| 79 |
public function withBody( StreamInterface $body ): self { |
| 80 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 81 |
} |
| 82 |
|
| 83 |
public function getStatusCode(): int { |
| 84 |
return $this->mwHttpRequest->getStatus(); |
| 85 |
} |
| 86 |
|
| 87 |
public function withStatus( $code, $reasonPhrase = '' ): self { |
| 88 |
$this->throwExceptionForBuilderMethod( __METHOD__ ); |
| 89 |
} |
| 90 |
|
| 91 |
public function getReasonPhrase(): string { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private function throwExceptionForBuilderMethod( string $method ): void { |
| 100 |
throw new LogicException( "Builder method $method is not supported." ); |
| 101 |
} |
| 102 |
|
| 103 |
private function validateHasResponse( MWHttpRequest $mwHttpRequest ): void { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if ( !$mwHttpRequest->getResponseHeaders() ) { |
| 110 |
throw new LogicException( 'Trying to get response information from a request that was not yet executed' ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |