Completed
Push — master ( 3206cc...899138 )
by David
24:10 queued 23:01
created

ResponseBuilder::getResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Http\Message\Builder;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Fills response object with values.
9
 */
10
class ResponseBuilder
11
{
12
    /**
13
     * The response to be built.
14
     *
15
     * @var ResponseInterface
16
     */
17
    protected $response;
18
19
    /**
20
     * Create builder for the given response.
21
     *
22
     * @param ResponseInterface $response
23
     */
24 3
    public function __construct(ResponseInterface $response)
25
    {
26 3
        $this->response = $response;
27 3
    }
28
29
    /**
30
     * Return response.
31
     *
32
     * @return ResponseInterface
33
     */
34
    public function getResponse()
35
    {
36
        return $this->response;
37
    }
38
39
    /**
40
     * Add headers represented by an array of header lines.
41
     *
42
     * @param string[] $headers Response headers as array of header lines.
43
     *
44
     * @return $this
45
     *
46
     * @throws \UnexpectedValueException For invalid header values.
47
     * @throws \InvalidArgumentException For invalid status code arguments.
48
     */
49 2
    public function setHeadersFromArray(array $headers)
50
    {
51 2
        $status = array_shift($headers);
52 2
        $this->setStatus($status);
53
54 2
        foreach ($headers as $headerLine) {
55 2
            $headerLine = trim($headerLine);
56 2
            if ('' === $headerLine) {
57 1
                continue;
58
            }
59
60 2
            $this->addHeader($headerLine);
61
        }
62
63 2
        return $this;
64
    }
65
66
    /**
67
     * Add headers represented by a single string.
68
     *
69
     * @param string $headers Response headers as single string.
70
     *
71
     * @return $this
72
     *
73
     * @throws \InvalidArgumentException if $headers is not a string on object with __toString()
74
     * @throws \UnexpectedValueException For invalid header values.
75
     */
76 1
    public function setHeadersFromString($headers)
77
    {
78 1
        if (!(is_string($headers)
79 1
            || (is_object($headers) && method_exists($headers, '__toString')))
80
        ) {
81
            throw new \InvalidArgumentException(
82
                sprintf(
83
                    '%s expects parameter 1 to be a string, %s given',
84
                    __METHOD__,
85
                    is_object($headers) ? get_class($headers) : gettype($headers)
86
                )
87
            );
88
        }
89
90 1
        $this->setHeadersFromArray(explode("\r\n", $headers));
91
92 1
        return $this;
93
    }
94
95
    /**
96
     * Set response status from a status string.
97
     *
98
     * @param string $statusLine Response status as a string.
99
     *
100
     * @return $this
101
     *
102
     * @throws \InvalidArgumentException For invalid status line.
103
     */
104 2
    public function setStatus($statusLine)
105
    {
106 2
        $parts = explode(' ', $statusLine, 3);
107 2
        if (count($parts) < 2 || 0 !== strpos(strtolower($parts[0]), 'http/')) {
108
            throw new \InvalidArgumentException(
109
                sprintf('"%s" is not a valid HTTP status line', $statusLine)
110
            );
111
        }
112
113 2
        $reasonPhrase = count($parts) > 2 ? $parts[2] : '';
114 2
        $this->response = $this->response
115 2
            ->withStatus((int) $parts[1], $reasonPhrase)
116 2
            ->withProtocolVersion(substr($parts[0], 5));
117
118 2
        return $this;
119
    }
120
121
    /**
122
     * Add header represented by a string.
123
     *
124
     * @param string $headerLine Response header as a string.
125
     *
126
     * @return $this
127
     *
128
     * @throws \InvalidArgumentException For invalid header names or values.
129
     */
130 2
    public function addHeader($headerLine)
131
    {
132 2
        $parts = explode(':', $headerLine, 2);
133 2
        if (2 !== count($parts)) {
134
            throw new \InvalidArgumentException(
135
                sprintf('"%s" is not a valid HTTP header line', $headerLine)
136
            );
137
        }
138 2
        $name = trim($parts[0]);
139 2
        $value = trim($parts[1]);
140 2
        if ($this->response->hasHeader($name)) {
141
            $this->response = $this->response->withAddedHeader($name, $value);
142
        } else {
143 2
            $this->response = $this->response->withHeader($name, $value);
144
        }
145
146 2
        return $this;
147
    }
148
}
149