Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
Response
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 19
552
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 cast
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 getStatusCode
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
 setStatus
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getProtocolVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaders
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaderLine
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setProtocolVersion
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 removeHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setBody
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRawHeaderLines
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCookie
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getCookies
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Rest;
4
5use HttpStatus;
6use Psr\Http\Message\StreamInterface;
7
8class 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    public function getStatusCode() {
51        return $this->statusCode;
52    }
53
54    public function getReasonPhrase() {
55        return $this->reasonPhrase;
56    }
57
58    public function setStatus( $code, $reasonPhrase = '' ) {
59        $this->statusCode = $code;
60        if ( $reasonPhrase === '' ) {
61            $reasonPhrase = HttpStatus::getMessage( $code ) ?? '';
62        }
63        $this->reasonPhrase = $reasonPhrase;
64    }
65
66    public function getProtocolVersion() {
67        return $this->protocolVersion;
68    }
69
70    public function getHeaders() {
71        return $this->headerContainer->getHeaders();
72    }
73
74    public function hasHeader( $name ) {
75        return $this->headerContainer->hasHeader( $name );
76    }
77
78    public function getHeader( $name ) {
79        return $this->headerContainer->getHeader( $name );
80    }
81
82    public function getHeaderLine( $name ) {
83        return $this->headerContainer->getHeaderLine( $name );
84    }
85
86    public function getBody() {
87        return $this->body;
88    }
89
90    public function setProtocolVersion( $version ) {
91        $this->protocolVersion = $version;
92    }
93
94    public function setHeader( $name, $value ) {
95        $this->headerContainer->setHeader( $name, $value );
96    }
97
98    public function addHeader( $name, $value ) {
99        $this->headerContainer->addHeader( $name, $value );
100    }
101
102    public function removeHeader( $name ) {
103        $this->headerContainer->removeHeader( $name );
104    }
105
106    public function setBody( StreamInterface $body ) {
107        $this->body = $body;
108    }
109
110    public function getRawHeaderLines() {
111        return $this->headerContainer->getRawHeaderLines();
112    }
113
114    public function setCookie( $name, $value, $expire = 0, $options = [] ) {
115        $this->cookies[] = [
116            'name' => $name,
117            'value' => $value,
118            'expire' => $expire,
119            'options' => $options
120        ];
121    }
122
123    public function getCookies() {
124        return $this->cookies;
125    }
126}