Passed
Push — master ( b45126...8af3f1 )
by 世昌
03:18
created

ResponseWrapper::header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 5
rs 10
c 2
b 0
f 0
1
<?php
2
3
4
namespace suda\framework\response;
5
6
use suda\framework\http\Cookie;
7
use suda\framework\http\Stream;
8
use suda\framework\http\Response;
9
10
class ResponseWrapper implements Response
11
{
12
    /**
13
     * @var Response
14
     */
15
    protected $response;
16
17
    /**
18
     * @varint
19
     */
20
    protected $status = 200;
21
22
    /**
23
     * 创建响应
24
     * @param Response $response
25
     */
26
    public function __construct(Response $response)
27
    {
28
        $this->status = 200;
29
        $this->response = $response;
30
    }
31
32
    /**
33
     * 设置状态码
34
     *
35
     * @param integer $statusCode
36
     * @return Response
37
     */
38
    public function status(int $statusCode)
39
    {
40
        $this->sendWarningIfy(__METHOD__);
41
        $this->status = $statusCode;
42
        $this->response->status($statusCode);
43
        return $this;
44
    }
45
46
47
    /**
48
     * 获取状态码
49
     *
50
     * @return integer
51
     */
52
    public function getStatus():int
53
    {
54
        return $this->status;
55
    }
56
57
58
    /**
59
     * 设置响应版本
60
     *
61
     * @param string $version
62
     * @return $this
63
     */
64
    public function version(string $version)
65
    {
66
        $this->sendWarningIfy(__METHOD__);
67
        $this->response->version($version);
68
        return $this;
69
    }
70
71
    /**
72
     * 判断是否发送
73
     *
74
     * @return boolean
75
     */
76
    public function isSend(): bool
77
    {
78
        return $this->response->isSend();
79
    }
80
81
    /**
82
     * 设置头部信息
83
     *
84
     * @param string $name
85
     * @param string $value
86
     * @param bool $replace
87
     * @param bool $ucfirst
88
     * @return $this
89
     */
90
    public function header(string $name, string $value, bool $replace = false, bool $ucfirst = true)
91
    {
92
        $this->sendWarningIfy(__METHOD__);
93
        $this->response->header($name, $value, $replace, $ucfirst);
94
        return $this;
95
    }
96
97
    /**
98
     * 设置Cookie信息
99
     *
100
     * @param Cookie $cookie
101
     * @return $this
102
     */
103
    public function cookie(Cookie $cookie)
104
    {
105
        $this->sendWarningIfy(__METHOD__);
106
        $this->response->cookie($cookie);
107
        return $this;
108
    }
109
110
    /**
111
     * 写数据
112
     *
113
     * @param Stream|string $data
114
     * @return void
115
     */
116
    public function write($data)
117
    {
118
        $this->sendWarningIfy(__METHOD__);
119
        $this->response->write($data);
120
    }
121
122
    /**
123
     * 发送数据
124
     *
125
     * @param Stream|string $data
126
     * @return void
127
     */
128
    public function send($data)
129
    {
130
        $this->sendWarningIfy(__METHOD__);
131
        $this->response->send($data);
132
    }
133
134
    /**
135
     * 跳转
136
     *
137
     * @param string $url
138
     * @param integer $httpCode
139
     * @return void
140
     */
141
    public function redirect(string $url, int $httpCode = 302)
142
    {
143
        $this->sendWarningIfy(__METHOD__);
144
        $this->response->redirect($url, $httpCode);
145
    }
146
147
    /**
148
     * 发送文件内容
149
     *
150
     * @param string $filename
151
     * @param integer $offset
152
     * @param integer $length
153
     * @return void
154
     */
155
    public function sendFile(string $filename, int $offset = 0, int $length = null)
156
    {
157
        $this->sendWarningIfy(__METHOD__);
158
        $this->response->sendFile($filename, $offset, $length);
159
    }
160
161
    /**
162
     * @param string $name
163
     */
164
    private function sendWarningIfy(string $name)
165
    {
166
        if ($this->isSend()) {
167
            trigger_error($name .': response has been send', E_USER_WARNING);
168
        }
169
    }
170
}
171