ResponseMerger::setCookies()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 0
cp 0
rs 9.8666
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Pachico\SlimSwoole\Bridge;
4
5
use Slim\App;
6
use Psr\Http\Message\ResponseInterface;
7
use swoole_http_response;
8
use Dflydev\FigCookies\SetCookies;
9
10
class ResponseMerger implements ResponseMergerInterface
11
{
12
    /**
13
     * @var App
14
     */
15
    private $app;
16
17
    /**
18
     * @param App $app
19 18
     */
20
    public function __construct(App $app)
21 18
    {
22 18
        $this->app = $app;
23
    }
24
25
    /**
26
     * @param Response $response
27
     * @param swoole_http_response $swooleResponse
28
     *
29
     * @return swoole_http_response
30 18
     */
31
    public function mergeToSwoole(
32
        ResponseInterface $response,
33
        swoole_http_response $swooleResponse
34 18
    ): swoole_http_response {
35
        $container = $this->app->getContainer();
36 18
37 18
        $settings = $container->get('settings');
38 3
        if (isset($settings['addContentLengthHeader']) && $settings['addContentLengthHeader'] == true) {
39 3
            $size = $response->getBody()->getSize();
40 3
            if ($size !== null) {
41
                $swooleResponse->header('Content-Length', (string) $size);
42
            }
43
        }
44 18
45 3
        if (!empty($response->getHeaders())) {
46 3
            $this->setCookies($swooleResponse, $response);
47
48
            $response = $response->withoutHeader('Set-Cookie');
49
50 18
            foreach ($response->getHeaders() as $key => $headerArray) {
51
                $swooleResponse->header($key, implode('; ', $headerArray));
52 18
            }
53 9
        }
54 3
55
        $swooleResponse->status($response->getStatusCode(), $response->getReasonPhrase());
0 ignored issues
show
Unused Code introduced by
The call to Swoole\Http\Response::status() has too many arguments starting with $response->getReasonPhrase(). ( Ignorable by Annotation )

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

55
        $swooleResponse->/** @scrutinizer ignore-call */ 
56
                         status($response->getStatusCode(), $response->getReasonPhrase());

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...
56
57 9
        if ($response->getBody()->getSize() > 0) {
58
            if ($response->getBody()->isSeekable()) {
59
                $response->getBody()->rewind();
60 18
            }
61
62
            $swooleResponse->write($response->getBody()->getContents());
63
        }
64
65
        return $swooleResponse;
66
    }
67
    
68
    private function setCookies($swooleResponse, $response)
69
    {
70
        if (!$response->hasHeader('Set-Cookie')) {
71
            return;
72
        }
73
74
        $setCookies = SetCookies::fromSetCookieStrings($response->getHeader('Set-Cookie'));
75
        foreach ($setCookies->getAll() as $setCookie) {
76
            $swooleResponse->cookie(
77
                $setCookie->getName(),
78
                $setCookie->getValue(),
79
                $setCookie->getExpires(),
80
                $setCookie->getPath(),
81
                $setCookie->getDomain(),
82
                $setCookie->getSecure(),
83
                $setCookie->getHttpOnly()
84
            );
85
        }
86
    }
87
}
88