Completed
Pull Request — master (#100)
by Maxime
02:42
created

Response::setHttpResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is a part of a nekland library
4
 *
5
 * (c) Nekland <[email protected]>
6
 *
7
 * For the full license, take a look to the LICENSE file
8
 * on the root directory of this project
9
 */
10
11
namespace Nekland\Woketo\Http;
12
13
14
use React\Socket\ConnectionInterface;
15
16
class Response extends AbstractHttpMessage
17
{
18
    const SWITCHING_PROTOCOLS = '101 Switching Protocols';
19
    const BAD_REQUEST = '400 Bad Request';
20
21
    /**
22
     * @var string For example "404 Not Found"
23
     */
24
    private $httpResponse;
25
    
26
    privat
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
27
28
    public function __construct()
29
    {
30
        $this->setHttpVersion('HTTP/1.1');
31
    }
32
33
    /**
34
     * @param string $httpResponse
35
     * @return Response
36
     */
37
    public function setHttpResponse($httpResponse)
38
    {
39
        $this->httpResponse = $httpResponse;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @param ConnectionInterface $stream
46
     */
47
    public function send(ConnectionInterface $stream)
48
    {
49
        $stringResponse = $this->getHttpVersion() . ' ' . $this->httpResponse . "\r\n";
50
51
        foreach ($this->getHeaders() as $name => $content) {
52
            $stringResponse .= $name . ': '. $content . "\r\n";
53
        }
54
55
        // No content to concatenate
56
        $stringResponse .= "\r\n";
57
58
        $stream->write($stringResponse);
59
    }
60
61
    public static function createSwitchProtocolResponse()
62
    {
63
        $response = new Response();
64
65
        $response->setHttpResponse(Response::SWITCHING_PROTOCOLS);
66
        $response->addHeader('Upgrade', 'websocket');
67
        $response->addHeader('Connection', 'Upgrade');
68
69
        return $response;
70
    }
71
72
    /**
73
     * @param string $data
74
     * @return Response
75
     */
76
    public static function create(string $data) : Response
77
    {
78
        
79
    }
80
}
81