Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
12 / 21
47.06% covered (danger)
47.06%
8 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
MwHttpRequestToResponseInterfaceAdapter
57.14% covered (warning)
57.14%
12 / 21
47.06% covered (danger)
47.06%
8 / 17
51.49
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getProtocolVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 withProtocolVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getHeaderLine
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 withAddedHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 withoutHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStatusCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 withStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getReasonPhrase
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 throwExceptionForBuilderMethod
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateHasResponse
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare( strict_types = 1 );
4namespace MediaWiki\Http;
5
6use GuzzleHttp\Psr7\Utils;
7use LogicException;
8use MWHttpRequest;
9use Psr\Http\Message\ResponseInterface;
10use Psr\Http\Message\StreamInterface;
11
12/**
13 * @since 1.36
14 * @unstable
15 *
16 * @license GPL-2.0-or-later
17 */
18class MwHttpRequestToResponseInterfaceAdapter implements ResponseInterface {
19
20    /**
21     * @var MWHttpRequest
22     */
23    private $mwHttpRequest;
24
25    /**
26     * @param MWHttpRequest $mwHttpRequest the MWHttpRequest must contain response information, i.e. must have been
27     *                                     `execute`d
28     */
29    public function __construct( MWHttpRequest $mwHttpRequest ) {
30        $this->validateHasResponse( $mwHttpRequest );
31        $this->mwHttpRequest = $mwHttpRequest;
32    }
33
34    public function getProtocolVersion(): string {
35        // @phan-suppress-previous-line PhanPluginNeverReturnMethod
36        // This is not accessible via MWHttpRequest, but it is set in its protected `respVersion` property.
37        // If this is ever needed, it can get exposed in MWHttpRequest.
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 ''; // not exposed through MWHttpRequest, unlikely to ever be useful
93    }
94
95    /**
96     * @param string $method
97     * @return never
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         * MWHttpRequest objects contain request information, but also contain response information after calling
106         * `execute`. The best way of determining whether a MWHttpRequest contains response information is to check
107         * whether its headers list is empty.
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}