BaseHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A writeHeader() 0 13 4
A writeData() 0 4 1
A processResponse() 0 15 1
1
<?php
2
3
namespace ByJG\RestServer\HandleOutput;
4
5
use ByJG\RestServer\HttpResponse;
6
7
abstract class BaseHandler implements HandleOutputInterface
8
{
9
    protected $buildNull = true;
10
    protected $onlyString = false;
11
    protected $header = [];
12
13
    public function writeHeader($headerList = null)
14
    {
15
        if ($headerList === null) {
16
            $headerList = $this->header;
17
        }
18
        foreach ($headerList as $header) {
19
            if (is_array($header)) {
20
                header($header[0], $header[1]);
21
                continue;
22
            }
23
            header($header);
24
        }
25
    }
26
27
    public function writeData($data)
28
    {
29
        echo $data;
30
    }
31
32
    public function processResponse(HttpResponse $response)
33
    {
34
        $instanceHeaders = $response->getHeaders();
35
        $this->writeHeader($instanceHeaders);
36
37
        http_response_code($response->getResponseCode());
38
39
        $serialized = $response
40
            ->getResponseBag()
41
            ->process($this->buildNull, $this->onlyString);
42
43
        $this->writeData(
44
            $this->getFormatter()->process($serialized)
45
        );
46
    }
47
}
48