Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 19 |
CRAP | |
0.00% |
0 / 1 |
| Response | |
0.00% |
0 / 37 |
|
0.00% |
0 / 19 |
552 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| cast | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getStatusCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReasonPhrase | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setStatus | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getProtocolVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaders | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHeaderLine | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBody | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setProtocolVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setBody | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRawHeaderLines | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCookie | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getCookies | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest; |
| 4 | |
| 5 | use Psr\Http\Message\StreamInterface; |
| 6 | use Wikimedia\Http\HttpStatus; |
| 7 | |
| 8 | class Response implements ResponseInterface { |
| 9 | |
| 10 | private int $statusCode = 200; |
| 11 | private string $reasonPhrase = 'OK'; |
| 12 | private string $protocolVersion = '1.1'; |
| 13 | private StreamInterface $body; |
| 14 | private HeaderContainer $headerContainer; |
| 15 | private array $cookies = []; |
| 16 | |
| 17 | /** |
| 18 | * @param string|StreamInterface $body |
| 19 | * |
| 20 | * @internal Use ResponseFactory |
| 21 | */ |
| 22 | public function __construct( $body = '' ) { |
| 23 | if ( is_string( $body ) ) { |
| 24 | $body = new StringStream( $body ); |
| 25 | } |
| 26 | |
| 27 | $this->body = $body; |
| 28 | $this->headerContainer = new HeaderContainer; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @internal for backwards compatibility code |
| 33 | */ |
| 34 | public static function cast( ResponseInterface $iResponse ): Response { |
| 35 | if ( $iResponse instanceof Response ) { |
| 36 | return $iResponse; |
| 37 | } |
| 38 | |
| 39 | $resp = new Response( |
| 40 | $iResponse->getBody() |
| 41 | ); |
| 42 | |
| 43 | foreach ( $iResponse->getHeaders() as $name => $values ) { |
| 44 | $resp->setHeader( $name, $values ); |
| 45 | } |
| 46 | |
| 47 | return $resp; |
| 48 | } |
| 49 | |
| 50 | /** @inheritDoc */ |
| 51 | public function getStatusCode(): int { |
| 52 | return $this->statusCode; |
| 53 | } |
| 54 | |
| 55 | /** @inheritDoc */ |
| 56 | public function getReasonPhrase(): string { |
| 57 | return $this->reasonPhrase; |
| 58 | } |
| 59 | |
| 60 | /** @inheritDoc */ |
| 61 | public function setStatus( $code, $reasonPhrase = '' ) { |
| 62 | $this->statusCode = $code; |
| 63 | if ( $reasonPhrase === '' ) { |
| 64 | $reasonPhrase = HttpStatus::getMessage( $code ) ?? ''; |
| 65 | } |
| 66 | $this->reasonPhrase = $reasonPhrase; |
| 67 | } |
| 68 | |
| 69 | /** @inheritDoc */ |
| 70 | public function getProtocolVersion(): string { |
| 71 | return $this->protocolVersion; |
| 72 | } |
| 73 | |
| 74 | /** @inheritDoc */ |
| 75 | public function getHeaders(): array { |
| 76 | return $this->headerContainer->getHeaders(); |
| 77 | } |
| 78 | |
| 79 | /** @inheritDoc */ |
| 80 | public function hasHeader( string $name ): bool { |
| 81 | return $this->headerContainer->hasHeader( $name ); |
| 82 | } |
| 83 | |
| 84 | /** @inheritDoc */ |
| 85 | public function getHeader( string $name ): array { |
| 86 | return $this->headerContainer->getHeader( $name ); |
| 87 | } |
| 88 | |
| 89 | /** @inheritDoc */ |
| 90 | public function getHeaderLine( string $name ): string { |
| 91 | return $this->headerContainer->getHeaderLine( $name ); |
| 92 | } |
| 93 | |
| 94 | /** @inheritDoc */ |
| 95 | public function getBody(): StreamInterface { |
| 96 | return $this->body; |
| 97 | } |
| 98 | |
| 99 | /** @inheritDoc */ |
| 100 | public function setProtocolVersion( $version ) { |
| 101 | $this->protocolVersion = $version; |
| 102 | } |
| 103 | |
| 104 | /** @inheritDoc */ |
| 105 | public function setHeader( $name, $value ) { |
| 106 | $this->headerContainer->setHeader( $name, $value ); |
| 107 | } |
| 108 | |
| 109 | /** @inheritDoc */ |
| 110 | public function addHeader( $name, $value ) { |
| 111 | $this->headerContainer->addHeader( $name, $value ); |
| 112 | } |
| 113 | |
| 114 | /** @inheritDoc */ |
| 115 | public function removeHeader( $name ) { |
| 116 | $this->headerContainer->removeHeader( $name ); |
| 117 | } |
| 118 | |
| 119 | /** @inheritDoc */ |
| 120 | public function setBody( StreamInterface|string $body ) { |
| 121 | $this->body = $body; |
| 122 | } |
| 123 | |
| 124 | /** @inheritDoc */ |
| 125 | public function getRawHeaderLines() { |
| 126 | return $this->headerContainer->getRawHeaderLines(); |
| 127 | } |
| 128 | |
| 129 | /** @inheritDoc */ |
| 130 | public function setCookie( $name, $value, $expire = 0, $options = [] ) { |
| 131 | $this->cookies[] = [ |
| 132 | 'name' => $name, |
| 133 | 'value' => $value, |
| 134 | 'expire' => $expire, |
| 135 | 'options' => $options |
| 136 | ]; |
| 137 | } |
| 138 | |
| 139 | /** @inheritDoc */ |
| 140 | public function getCookies() { |
| 141 | return $this->cookies; |
| 142 | } |
| 143 | } |