Completed
Branch master (9dff9d)
by Albert
05:30
created

Response   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 18

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setSwooleResponse() 0 5 1
A getSwooleResponse() 0 3 1
A sendHeaders() 0 32 6
A send() 0 4 1
A setIlluminateResponse() 0 10 2
A sendContent() 0 12 4
A getIlluminateResponse() 0 3 1
A make() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace SwooleTW\Http\Transformers;
4
5
use Illuminate\Http\Response as IlluminateResponse;
6
use Swoole\Http\Response as SwooleResponse;
7
use Symfony\Component\HttpFoundation\BinaryFileResponse;
8
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
9
use Symfony\Component\HttpFoundation\StreamedResponse;
10
11
class Response
12
{
13
    /**
14
     * @var \Swoole\Http\Response
15
     */
16
    protected $swooleResponse;
17
18
    /**
19
     * @var \Illuminate\Http\Response
20
     */
21
    protected $illuminateResponse;
22
23
    /**
24
     * Make a response.
25
     *
26
     * @param $illuminateResponse
27
     * @param \Swoole\Http\Response $swooleResponse
28
     * @return \SwooleTW\Http\Server\Response
0 ignored issues
show
Bug introduced by
The type SwooleTW\Http\Server\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     */
30
    public static function make($illuminateResponse, SwooleResponse $swooleResponse)
31
    {
32
        return new static($illuminateResponse, $swooleResponse);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static($illum...ponse, $swooleResponse) returns the type SwooleTW\Http\Transformers\Response which is incompatible with the documented return type SwooleTW\Http\Server\Response.
Loading history...
33
    }
34
35
    /**
36
     * Response constructor.
37
     *
38
     * @param mixed $illuminateResponse
39
     * @param \Swoole\Http\Response $swooleResponse
40
     */
41
    public function __construct($illuminateResponse, SwooleResponse $swooleResponse)
42
    {
43
        $this->setIlluminateResponse($illuminateResponse);
44
        $this->setSwooleResponse($swooleResponse);
45
    }
46
47
    /**
48
     * Sends HTTP headers and content.
49
     *
50
     * @throws \InvalidArgumentException
51
     */
52
    public function send()
53
    {
54
        $this->sendHeaders();
55
        $this->sendContent();
56
    }
57
58
    /**
59
     * Sends HTTP headers.
60
     *
61
     * @throws \InvalidArgumentException
62
     */
63
    protected function sendHeaders()
64
    {
65
        $illuminateResponse = $this->getIlluminateResponse();
66
67
        /* RFC2616 - 14.18 says all Responses need to have a Date */
68
        if (! $illuminateResponse->headers->has('Date')) {
69
            $illuminateResponse->setDate(\DateTime::createFromFormat('U', time()));
0 ignored issues
show
Bug introduced by
It seems like DateTime::createFromFormat('U', time()) can also be of type false; however, parameter $date of Symfony\Component\HttpFo...ion\Response::setDate() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

69
            $illuminateResponse->setDate(/** @scrutinizer ignore-type */ \DateTime::createFromFormat('U', time()));
Loading history...
70
        }
71
72
        // headers
73
        // allPreserveCaseWithoutCookies() doesn't exist before Laravel 5.3
74
        $headers = $illuminateResponse->headers->allPreserveCase();
75
        if (isset($headers['Set-Cookie'])) {
76
            unset($headers['Set-Cookie']);
77
        }
78
        foreach ($headers as $name => $values) {
79
            foreach ($values as $value) {
80
                $this->swooleResponse->header($name, $value);
81
            }
82
        }
83
84
        // status
85
        $this->swooleResponse->status($illuminateResponse->getStatusCode());
86
87
        // cookies
88
        foreach ($illuminateResponse->headers->getCookies() as $cookie) {
89
            // may need to consider rawcookie
90
            $this->swooleResponse->cookie(
91
                $cookie->getName(), $cookie->getValue(),
92
                $cookie->getExpiresTime(), $cookie->getPath(),
93
                $cookie->getDomain(), $cookie->isSecure(),
94
                $cookie->isHttpOnly()
95
            );
96
        }
97
    }
98
99
    /**
100
     * Sends HTTP content.
101
     */
102
    protected function sendContent()
103
    {
104
        $illuminateResponse = $this->getIlluminateResponse();
105
106
        if ($illuminateResponse instanceof StreamedResponse &&
0 ignored issues
show
introduced by
$illuminateResponse is never a sub-type of Symfony\Component\HttpFoundation\StreamedResponse.
Loading history...
107
            property_exists($illuminateResponse, 'output')
108
        ) {
109
            $this->swooleResponse->end($illuminateResponse->output);
110
        } elseif ($illuminateResponse instanceof BinaryFileResponse) {
0 ignored issues
show
introduced by
$illuminateResponse is never a sub-type of Symfony\Component\HttpFo...tion\BinaryFileResponse.
Loading history...
111
            $this->swooleResponse->sendfile($illuminateResponse->getFile()->getPathname());
112
        } else {
113
            $this->swooleResponse->end($illuminateResponse->getContent());
114
        }
115
    }
116
117
    /**
118
     * @param \Swoole\Http\Response $swooleResponse
119
     * @return \SwooleTW\Http\Server\Response
120
     */
121
    protected function setSwooleResponse(SwooleResponse $swooleResponse)
122
    {
123
        $this->swooleResponse = $swooleResponse;
124
125
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type SwooleTW\Http\Transformers\Response which is incompatible with the documented return type SwooleTW\Http\Server\Response.
Loading history...
126
    }
127
128
    /**
129
     * @return \Swoole\Http\Response
130
     */
131
    public function getSwooleResponse()
132
    {
133
        return $this->swooleResponse;
134
    }
135
136
    /**
137
     * @param mixed illuminateResponse
138
     * @return \SwooleTW\Http\Server\Response
139
     */
140
    protected function setIlluminateResponse($illuminateResponse)
141
    {
142
        if (! $illuminateResponse instanceof SymfonyResponse) {
143
            $content = (string) $illuminateResponse;
144
            $illuminateResponse = new IlluminateResponse($content);
145
        }
146
147
        $this->illuminateResponse = $illuminateResponse;
148
149
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type SwooleTW\Http\Transformers\Response which is incompatible with the documented return type SwooleTW\Http\Server\Response.
Loading history...
150
    }
151
152
    /**
153
     * @return \Illuminate\Http\Response
154
     */
155
    public function getIlluminateResponse()
156
    {
157
        return $this->illuminateResponse;
158
    }
159
}
160