ViewTrait::getEmptyBody()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lit\Voltage\Traits;
6
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * Implements ViewInterface and provide a helper method getEmptyBody.
11
 */
12
trait ViewTrait
13
{
14
    /**
15
     * @var ResponseInterface
16
     */
17
    protected $response;
18
19
    /**
20
     * @ses \Lit\Voltage\Interfaces\ViewInterface::setResponse
21
     * @param ResponseInterface $response Set the response prototype.
22
     * @return void
23
     */
24
    public function setResponse(ResponseInterface $response): void
25
    {
26
        $this->response = $response;
27
    }
28
29
    /**
30
     * Ensure body is empty and writable and return that.
31
     *
32
     * @return \Psr\Http\Message\StreamInterface
33
     * @throws \RuntimeException When the body is not writable or not empty somehow.
34
     */
35
    protected function getEmptyBody()
36
    {
37
        $body = $this->response->getBody();
38
        if (!$body->isWritable()) {
39
            throw new \RuntimeException('response body is not writeble');
40
        }
41
        if ($body->getSize() !== 0) {
42
            throw new \RuntimeException('response body is not empty');
43
        }
44
45
        return $body;
46
    }
47
}
48