Response::sendCookies()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 18
rs 9.4888
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
namespace Hhxsv5\LaravelS\Swoole;
5
6
use Swoole\Http\Response as SwooleResponse;
7
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\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...
8
9
abstract class Response implements ResponseInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Response
Loading history...
10
{
11
    protected $chunkLimit = 2097152; // 2 * 1024 * 1024
12
13
    protected $swooleResponse;
14
15
    protected $laravelResponse;
16
17
    public function __construct(SwooleResponse $swooleResponse, SymfonyResponse $laravelResponse)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
18
    {
19
        $this->swooleResponse = $swooleResponse;
20
        $this->laravelResponse = $laravelResponse;
21
    }
22
23
    public function setChunkLimit($chunkLimit)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setChunkLimit()
Loading history...
24
    {
25
        $this->chunkLimit = $chunkLimit;
26
    }
27
28
    public function sendStatusCode()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendStatusCode()
Loading history...
29
    {
30
        $this->swooleResponse->status($this->laravelResponse->getStatusCode());
31
    }
32
33
    private function getHeaders()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getHeaders()
Loading history...
Coding Style introduced by
Private method name "Response::getHeaders" must be prefixed with an underscore
Loading history...
34
    {
35
        if (method_exists($this->laravelResponse->headers, 'allPreserveCaseWithoutCookies')) {
36
            return $this->laravelResponse->headers->allPreserveCaseWithoutCookies();
37
        }
38
39
        return $this->laravelResponse->headers->allPreserveCase();
40
    }
41
42
    public function sendHeaders()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendHeaders()
Loading history...
43
    {
44
        $headers = $this->getHeaders();
45
        $trailers = isset($headers['trailer']) ? $headers['trailer'] : [];
46
47
        foreach ($headers as $name => $values) {
48
            if (in_array($name, $trailers, true)) {
49
                continue;
50
            }
51
            if (version_compare(SWOOLE_VERSION, '4.6.0', '>=')) {
52
                $this->swooleResponse->header($name, $values);
53
            } else {
54
                foreach ($values as $value) {
55
                    $this->swooleResponse->header($name, $value);
56
                }
57
            }
58
        }
59
    }
60
61
    public function sendTrailers()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendTrailers()
Loading history...
62
    {
63
        $headers = $this->getHeaders();
64
        $trailers = isset($headers['trailer']) ? $headers['trailer'] : [];
65
66
        foreach ($headers as $name => $values) {
67
            if (!in_array($name, $trailers, true)) {
68
                continue;
69
            }
70
71
            foreach ($values as $value) {
72
                $this->swooleResponse->trailer($name, $value);
73
            }
74
        }
75
    }
76
77
    public function sendCookies()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendCookies()
Loading history...
78
    {
79
        $hasIsRaw = null;
80
        /**@var \Symfony\Component\HttpFoundation\Cookie[] $cookies */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
81
        $cookies = $this->laravelResponse->headers->getCookies();
82
        foreach ($cookies as $cookie) {
83
            if ($hasIsRaw === null) {
84
                $hasIsRaw = method_exists($cookie, 'isRaw');
85
            }
86
            $setCookie = $hasIsRaw && $cookie->isRaw() ? 'rawcookie' : 'cookie';
87
            $this->swooleResponse->$setCookie(
88
                $cookie->getName(),
89
                (string)$cookie->getValue(),
90
                $cookie->getExpiresTime(),
91
                $cookie->getPath(),
92
                (string)$cookie->getDomain(),
93
                $cookie->isSecure(),
94
                $cookie->isHttpOnly()
95
            );
96
        }
97
    }
98
99
    public function send($gzip = false)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function send()
Loading history...
100
    {
101
        $this->sendStatusCode();
102
        $this->sendHeaders();
103
        $this->sendCookies();
104
        $this->sendTrailers();
105
        if ($gzip) {
106
            $this->gzip();
107
        }
108
        $this->sendContent();
109
    }
110
}
111