Completed
Pull Request — master (#110)
by Charlotte
06:09
created

Response   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 54
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setHttpResponse() 0 6 1
A send() 0 13 2
A createSwitchProtocolResponse() 0 10 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
    public function __construct()
27
    {
28
        $this->setHttpVersion('HTTP/1.1');
29
    }
30
31
    /**
32
     * @param string $httpResponse
33
     * @return Response
34
     */
35
    public function setHttpResponse($httpResponse)
36
    {
37
        $this->httpResponse = $httpResponse;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param ConnectionInterface $stream
44
     */
45
    public function send(ConnectionInterface $stream)
46
    {
47
        $stringResponse = $this->getHttpVersion() . ' ' . $this->httpResponse . "\r\n";
48
49
        foreach ($this->getHeaders() as $name => $content) {
50
            $stringResponse .= $name . ': '. $content . "\r\n";
51
        }
52
        
53
        // No content to concatenate
54
        $stringResponse .= "\r\n";
55
56
        $stream->write($stringResponse);
57
    }
58
59
    public static function createSwitchProtocolResponse()
60
    {
61
        $response = new Response();
62
63
        $response->setHttpResponse(Response::SWITCHING_PROTOCOLS);
64
        $response->addHeader('Upgrade', 'websocket');
65
        $response->addHeader('Connection', 'Upgrade');
66
67
        return $response;
68
    }
69
}
70