Completed
Push — master ( 9dba24...2b8fe2 )
by Aurimas
02:28
created

ResponseSender::sendHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 12
1
<?php
2
3
namespace Thruster\Component\HttpResponse;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class ResponseSender
9
 *
10
 * @package Thruster\Component\HttpResponse
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class ResponseSender
14
{
15
    /**
16
     * @var ResponseInterface
17
     */
18
    private $response;
19
20
    private function __construct(ResponseInterface $response)
21
    {
22
        $this->response = $response;
23
    }
24
25
    public static function send(ResponseInterface $response)
26
    {
27
        $sender = new static($response);
28
29
        $sender->sendStatus();
30
        $sender->sendHeaders();
31
        $sender->sendBody();
32
    }
33
34
    private function sendStatus()
35
    {
36
        header(
37
            'HTTP/' .
38
            $this->response->getProtocolVersion() . ' ' .
39
            $this->response->getStatusCode() . ' ' .
40
            $this->response->getReasonPhrase()
41
        );
42
    }
43
44
    private function sendHeaders()
45
    {
46
        foreach ($this->response->getHeaders() as $name => $values) {
47
            foreach ($values as $value) {
48
                header($name . ': ' . $value);
49
            }
50
        }
51
    }
52
53
    private function sendBody()
54
    {
55
        echo $this->response->getBody()->getContents();
56
    }
57
}
58