MediaWiki  master
MwHttpRequestToResponseInterfaceAdapter.php
Go to the documentation of this file.
1 <?php
2 
3 declare( strict_types = 1 );
4 namespace MediaWiki\Http;
5 
6 use LogicException;
7 use MWHttpRequest;
8 use Psr\Http\Message\ResponseInterface;
9 use Psr\Http\Message\StreamInterface;
10 use function GuzzleHttp\Psr7\stream_for;
11 
18 class MwHttpRequestToResponseInterfaceAdapter implements ResponseInterface {
19 
23  private $mwHttpRequest;
24 
30  $this->validateHasResponse( $mwHttpRequest );
31  $this->mwHttpRequest = $mwHttpRequest;
32  }
33 
34  public function getProtocolVersion(): void {
35  // This is not accessible via MWHttpRequest, but it is set in its protected `respVersion` property.
36  // If this is ever needed, it can get exposed in MWHttpRequest.
37  throw new LogicException( __METHOD__ . ' is not implemented' );
38  }
39 
40  public function withProtocolVersion( $version ): void {
41  $this->throwExceptionForBuilderMethod( __METHOD__ );
42  }
43 
44  public function getHeaders(): array {
45  return $this->mwHttpRequest->getResponseHeaders();
46  }
47 
48  public function hasHeader( $name ): bool {
49  return isset( $this->mwHttpRequest->getResponseHeaders()[$name] );
50  }
51 
52  public function getHeader( $name ): array {
53  return $this->hasHeader( $name ) ? $this->mwHttpRequest->getResponseHeaders()[$name] : [];
54  }
55 
56  public function getHeaderLine( $name ): string {
57  return $this->hasHeader( $name )
58  ? implode( ',', $this->mwHttpRequest->getResponseHeaders()[$name] )
59  : '';
60  }
61 
62  public function withHeader( $name, $value ): void {
63  $this->throwExceptionForBuilderMethod( __METHOD__ );
64  }
65 
66  public function withAddedHeader( $name, $value ): void {
67  $this->throwExceptionForBuilderMethod( __METHOD__ );
68  }
69 
70  public function withoutHeader( $name ): void {
71  $this->throwExceptionForBuilderMethod( __METHOD__ );
72  }
73 
74  public function getBody(): StreamInterface {
75  return stream_for( $this->mwHttpRequest->getContent() );
76  }
77 
78  public function withBody( StreamInterface $body ): void {
79  $this->throwExceptionForBuilderMethod( __METHOD__ );
80  }
81 
82  public function getStatusCode(): int {
83  return $this->mwHttpRequest->getStatus();
84  }
85 
86  public function withStatus( $code, $reasonPhrase = '' ): void {
87  $this->throwExceptionForBuilderMethod( __METHOD__ );
88  }
89 
90  public function getReasonPhrase(): string {
91  return ''; // not exposed through MWHttpRequest, unlikely to ever be useful
92  }
93 
94  private function throwExceptionForBuilderMethod( string $method ): void {
95  throw new LogicException( "Builder method $method is not supported." );
96  }
97 
98  private function validateHasResponse( MWHttpRequest $mwHttpRequest ): void {
104  if ( empty( $mwHttpRequest->getResponseHeaders() ) ) {
105  throw new LogicException( 'Trying to get response information from a request that was not yet executed' );
106  }
107  }
108 }
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withProtocolVersion
withProtocolVersion( $version)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:40
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getHeaders
getHeaders()
Definition: MwHttpRequestToResponseInterfaceAdapter.php:44
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withStatus
withStatus( $code, $reasonPhrase='')
Definition: MwHttpRequestToResponseInterfaceAdapter.php:86
MediaWiki\Http
Definition: HttpRequestFactory.php:20
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withAddedHeader
withAddedHeader( $name, $value)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:66
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\$mwHttpRequest
MWHttpRequest $mwHttpRequest
Definition: MwHttpRequestToResponseInterfaceAdapter.php:23
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\hasHeader
hasHeader( $name)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:48
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getBody
getBody()
Definition: MwHttpRequestToResponseInterfaceAdapter.php:74
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\throwExceptionForBuilderMethod
throwExceptionForBuilderMethod(string $method)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:94
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter
Definition: MwHttpRequestToResponseInterfaceAdapter.php:18
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getProtocolVersion
getProtocolVersion()
Definition: MwHttpRequestToResponseInterfaceAdapter.php:34
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getStatusCode
getStatusCode()
Definition: MwHttpRequestToResponseInterfaceAdapter.php:82
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getHeaderLine
getHeaderLine( $name)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:56
MWHttpRequest
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Definition: MWHttpRequest.php:33
MWHttpRequest\getResponseHeaders
getResponseHeaders()
Returns an associative array of response headers after the request has been executed.
Definition: MWHttpRequest.php:498
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getReasonPhrase
getReasonPhrase()
Definition: MwHttpRequestToResponseInterfaceAdapter.php:90
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\__construct
__construct(MWHttpRequest $mwHttpRequest)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:29
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withHeader
withHeader( $name, $value)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:62
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withoutHeader
withoutHeader( $name)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:70
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\getHeader
getHeader( $name)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:52
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\withBody
withBody(StreamInterface $body)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:78
MediaWiki\Http\MwHttpRequestToResponseInterfaceAdapter\validateHasResponse
validateHasResponse(MWHttpRequest $mwHttpRequest)
Definition: MwHttpRequestToResponseInterfaceAdapter.php:98