Passed
Push — master ( f31cc0...4ee9d0 )
by Tobias
03:39 queued 01:35
created

ResponseBuilder::setBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Message;
6
7
use Buzz\Exception\InvalidArgumentException;
8
use Http\Message\ResponseFactory as HTTPlugResponseFactory;
9
use Interop\Http\Factory\ResponseFactoryInterface as InteropResponseFactory;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class ResponseBuilder
16
{
17
    /**
18
     * @var ResponseInterface
19
     */
20
    private $response;
21
22
    /**
23
     * @var null|resource
24
     */
25
    private $stream = null;
0 ignored issues
show
introduced by
The private property $stream is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var null|string
29
     */
30
    private $body = null;
0 ignored issues
show
introduced by
The private property $body is not used, and could be removed.
Loading history...
31
32
    private $protocolVersion;
0 ignored issues
show
introduced by
The private property $protocolVersion is not used, and could be removed.
Loading history...
33
    private $statusCode;
0 ignored issues
show
introduced by
The private property $statusCode is not used, and could be removed.
Loading history...
34
    private $reasonPhrase;
0 ignored issues
show
introduced by
The private property $reasonPhrase is not used, and could be removed.
Loading history...
35
    private $headers = [];
0 ignored issues
show
introduced by
The private property $headers is not used, and could be removed.
Loading history...
36
37
    /**
38
     * @param HTTPlugResponseFactory|InteropResponseFactory $responseFactory
39
     */
40
    public function __construct($responseFactory)
41
    {
42
        if (!$responseFactory instanceof HTTPlugResponseFactory && !$responseFactory instanceof InteropResponseFactory) {
0 ignored issues
show
introduced by
$responseFactory is always a sub-type of Interop\Http\Factory\ResponseFactoryInterface.
Loading history...
43
            throw new InvalidArgumentException('First parameter to ResponseBuilder must be a response factory');
44
        }
45
46
        $this->response = $responseFactory->createResponse();
47
    }
48
49
    public function setStatus(string $input): void
50
    {
51
        $parts = explode(' ', $input, 3);
52
        if (count($parts) < 2 || 0 !== strpos(strtolower($parts[0]), 'http/')) {
53
            throw new InvalidArgumentException(sprintf('"%s" is not a valid HTTP status line', $input));
54
        }
55
56
        $this->response = $this->response->withStatus((int) $parts[1], isset($parts[2]) ? $parts[2] : '');
57
        $this->response = $this->response->withProtocolVersion((string) substr($parts[0], 5));
58
    }
59
60
    /**
61
     * Add a single HTTP header line.
62
     *
63
     * @param string $input
64
     */
65
    public function addHeader(string $input): void
66
    {
67
        list($key, $value) = explode(':', $input, 2);
68
        $this->response = $this->response->withAddedHeader(trim($key), trim($value));
69
    }
70
71
    /**
72
     * Add HTTP headers. The input array is all the header lines from the HTTP message. Optionally including the
73
     * status line.
74
     *
75
     * @param array $headers
76
     */
77
    public function parseHttpHeaders(array $headers): void
78
    {
79
        $statusLine = array_shift($headers);
80
        try {
81
            $this->setStatus($statusLine);
82
        } catch (InvalidArgumentException $e) {
83
            array_unshift($headers, $statusLine);
84
        }
85
86
        foreach ($headers as $header) {
87
            $this->addHeader($header);
88
        }
89
    }
90
91
    /**
92
     * Add some content to the body. This function writes the $input to a stream.
93
     *
94
     * @param string $input
95
     *
96
     * @return int returns the number of bytes written
97
     */
98
    public function writeBody(string $input): int
99
    {
100
        return $this->response->getBody()->write($input);
101
    }
102
103
    public function getResponse(): ResponseInterface
104
    {
105
        $this->response->getBody()->rewind();
106
107
        return $this->response;
108
    }
109
}
110