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

Response   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 143
rs 10
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isSend() 0 3 1
A send() 0 4 1
A header() 0 4 1
A write() 0 3 1
A version() 0 3 1
A sendFile() 0 4 1
A __construct() 0 4 1
A cookie() 0 11 1
A status() 0 5 1
A redirect() 0 3 1
1
<?php
2
3
4
namespace suda\swoole;
5
6
use suda\framework\http\Cookie;
7
use suda\framework\http\Stream;
8
9
/**
10
 * Class Response
11
 * @package suda\swoole
12
 */
13
class Response implements \suda\framework\http\Response
14
{
15
    /**
16
     * @var bool
17
     */
18
    protected $send = false;
19
20
    /**
21
     * @var \Swoole\Http\Response
22
     */
23
    protected $response;
24
25
    /**
26
     * @var int
27
     */
28
    protected $status = 200;
29
30
    /**
31
     * Response constructor.
32
     * @param \Swoole\Http\Response $response
33
     */
34
    public function __construct(\Swoole\Http\Response $response)
35
    {
36
        $this->response = $response;
37
        $this->send = false;
38
    }
39
40
41
    /**
42
     * 设置状态码
43
     *
44
     * @param integer $statusCode
45
     * @return \suda\framework\http\Response
46
     */
47
    public function status(int $statusCode)
48
    {
49
        $this->response->status($statusCode);
50
        $this->status  = $statusCode;
51
        return $this;
52
    }
53
54
    /**
55
     * 判断是否发送
56
     *
57
     * @return boolean
58
     */
59
    public function isSend(): bool
60
    {
61
        return $this->send;
62
    }
63
64
    /**
65
     * 设置头部信息
66
     *
67
     * @param string $name
68
     * @param string $value
69
     * @param bool $replace
70
     * @param bool $ucfirst
71
     * @return \suda\framework\http\Response
72
     */
73
    public function header(string $name, string $value, bool $replace = false, bool $ucfirst = true)
74
    {
75
        $this->response->header($name, $value, $ucfirst);
0 ignored issues
show
Unused Code introduced by
The call to Swoole\Http\Response::header() has too many arguments starting with $ucfirst. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
        $this->response->/** @scrutinizer ignore-call */ 
76
                         header($name, $value, $ucfirst);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
76
        return $this;
77
    }
78
79
    /**
80
     * 设置Cookie信息
81
     *
82
     * @param Cookie $cookie
83
     * @return \suda\framework\http\Response
84
     */
85
    public function cookie(Cookie $cookie)
86
    {
87
        $this->response->cookie(
88
            $cookie->getName(),
89
            $cookie->getValue(),
90
            $cookie->getExpire(),
91
            $cookie->getPath(),
92
            $cookie->getDomain(),
93
            $cookie->isSecure()
94
        );
95
        return $this;
96
    }
97
98
    /**
99
     * 写数据
100
     *
101
     * @param Stream|string $data
102
     * @return void
103
     */
104
    public function write($data)
105
    {
106
        $this->response->write($data);
107
    }
108
109
    /**
110
     * 发送数据
111
     *
112
     * @param Stream|string $data
113
     * @return void
114
     */
115
    public function send($data)
116
    {
117
        $this->send = true;
118
        $this->response->end($data);
119
    }
120
121
    /**
122
     * 发送文件内容
123
     *
124
     * @param string $filename
125
     * @param integer $offset
126
     * @param integer $length
127
     * @return void
128
     */
129
    public function sendFile(string $filename, int $offset = 0, int $length = null)
130
    {
131
        $this->send = true;
132
        $this->response->sendfile($filename, $offset, $length);
0 ignored issues
show
Unused Code introduced by
The call to Swoole\Http\Response::sendfile() has too many arguments starting with $offset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
        $this->response->/** @scrutinizer ignore-call */ 
133
                         sendfile($filename, $offset, $length);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
133
    }
134
135
    /**
136
     * 跳转
137
     *
138
     * @param string $url
139
     * @param integer $httpCode
140
     * @return void
141
     */
142
    public function redirect(string $url, int $httpCode = 302)
143
    {
144
        $this->response->redirect($url, $httpCode);
0 ignored issues
show
Bug introduced by
The method redirect() does not exist on Swoole\Http\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        $this->response->/** @scrutinizer ignore-call */ 
145
                         redirect($url, $httpCode);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
    }
146
147
    /**
148
     * 设置响应版本
149
     *
150
     * @param string $version
151
     * @return \suda\framework\http\Response
152
     */
153
    public function version(string $version)
154
    {
155
        return $this;
156
    }
157
}