AbstractResponse::createBodyFromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace hiapi\Core\Console;
4
5
use Laminas\Diactoros\Response;
6
use Laminas\Diactoros\Stream;
7
8
abstract class AbstractResponse extends Response
9
{
10
    public function __construct($data, int $status = 200, array $headers = [])
11
    {
12
        parent::__construct($this->prepareBody($data), $status, $this->prepareHeaders($headers));
13
    }
14
15
    protected function prepareHeaders(array $headers)
16
    {
17
        return $headers;
18
    }
19
20
    protected function prepareBody($data): Stream
21
    {
22
        return $this->createBodyFromString($this->formatData($data));
23
    }
24
25
    abstract protected function formatData($data): string;
26
27
    protected function createBodyFromString(string $string): Stream
28
    {
29
        $body = new Stream('php://temp', 'wb+');
30
        $body->write($string);
31
        $body->rewind();
32
33
        return $body;
34
    }
35
}
36