Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

SapiEmitter::emitBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 *
9
 * @see       https://github.com/zendframework/zend-httphandlerrunner for the canonical source repository
10
 * @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
11
 * @license   https://github.com/zendframework/zend-httphandlerrunner/blob/master/LICENSE.md New BSD License
12
 */
13
14
declare(strict_types=1);
15
16
namespace Spiral\Http\Emitter;
17
18
use Psr\Http\Message\ResponseInterface;
19
use Spiral\Http\EmitterInterface;
20
use Spiral\Http\Exception\EmitterException;
21
22
/**
23
 * Source code has been extracted from Zend/Diactoros.
24
 *
25
 * @codeCoverageIgnore
26
 */
27
final class SapiEmitter implements EmitterInterface
28
{
29
    /**
30
     * Emits a response for a PHP SAPI environment.
31
     *
32
     * Emits the status line and headers via the header() function, and the
33
     * body content via the output buffer.
34
     *
35
     * @param ResponseInterface $response
36
     * @return bool
37
     */
38
    public function emit(ResponseInterface $response): bool
39
    {
40
        $this->assertNoPreviousOutput();
41
42
        $this->emitHeaders($response);
43
        $this->emitStatusLine($response);
44
        $this->emitBody($response);
45
46
        return true;
47
    }
48
49
    /**
50
     * Emit the message body.
51
     *
52
     * @param ResponseInterface $response
53
     */
54
    private function emitBody(ResponseInterface $response): void
55
    {
56
        echo $response->getBody();
57
    }
58
59
    /**
60
     * Checks to see if content has previously been sent.
61
     *
62
     * If either headers have been sent or the output buffer contains content,
63
     * raises an exception.
64
     *
65
     * @throws EmitterException if headers have already been sent.
66
     * @throws EmitterException if output is present in the output buffer.
67
     */
68
    private function assertNoPreviousOutput(): void
69
    {
70
        if (headers_sent()) {
71
            throw new EmitterException('Unable to emit response, headers already send.');
72
        }
73
74
        if (ob_get_level() > 0 && ob_get_length() > 0) {
75
            throw new EmitterException('Unable to emit response, found non closed buffered output.');
76
        }
77
    }
78
79
    /**
80
     * Emit the status line.
81
     *
82
     * Emits the status line using the protocol version and status code from
83
     * the response; if a reason phrase is available, it, too, is emitted.
84
     *
85
     * It is important to mention that this method should be called after
86
     * `emitHeaders()` in order to prevent PHP from changing the status code of
87
     * the emitted response.
88
     *
89
     * @param ResponseInterface $response
90
     */
91
    private function emitStatusLine(ResponseInterface $response): void
92
    {
93
        $reasonPhrase = $response->getReasonPhrase();
94
        $statusCode = $response->getStatusCode();
95
96
        header(sprintf(
97
            'HTTP/%s %d%s',
98
            $response->getProtocolVersion(),
99
            $statusCode,
100
            ($reasonPhrase ? ' ' . $reasonPhrase : '')
101
        ), true, $statusCode);
102
    }
103
104
    /**
105
     * Emit response headers.
106
     *
107
     * Loops through each header, emitting each; if the header value
108
     * is an array with multiple values, ensures that each is sent
109
     * in such a way as to create aggregate headers (instead of replace
110
     * the previous).
111
     *
112
     * @param ResponseInterface $response
113
     */
114
    private function emitHeaders(ResponseInterface $response): void
115
    {
116
        $statusCode = $response->getStatusCode();
117
118
        foreach ($response->getHeaders() as $header => $values) {
119
            $name = $this->filterHeader($header);
120
            $first = $name === 'Set-Cookie' ? false : true;
121
            foreach ($values as $value) {
122
                header(sprintf(
123
                    '%s: %s',
124
                    $name,
125
                    $value
126
                ), $first, $statusCode);
127
                $first = false;
128
            }
129
        }
130
    }
131
132
    /**
133
     * Filter a header name to wordcase
134
     *
135
     * @param string $header
136
     * @return string
137
     */
138
    private function filterHeader(string $header): string
139
    {
140
        return ucwords($header, '-');
141
    }
142
}
143