AbstractResponse   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 28
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A prepareHeaders() 0 4 1
A prepareBody() 0 4 1
formatData() 0 1 ?
A createBodyFromString() 0 8 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