Completed
Push — 1.x ( f091f0...81f5a7 )
by Akihito
20s
created

HttpResponder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 41
rs 10
ccs 15
cts 15
cp 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 3
A getOutput() 0 3 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Sunday\Provide\Transfer;
6
7
use BEAR\Resource\ResourceObject;
8
use BEAR\Sunday\Extension\Transfer\TransferInterface;
9
10
class HttpResponder implements TransferInterface
11
{
12
    /**
13
     * @var HeaderInterface
14
     */
15 9
    private $header;
16
17 9
    /**
18 1
     * @var ConditionalResponseInterface
19
     */
20 1
    private $condResponse;
21
22
    public function __construct(HeaderInterface $header, ConditionalResponseInterface $condResponse)
23
    {
24 8
        $this->header = $header;
25 8
        $this->condResponse = $condResponse;
26
    }
27
28
    /**
29 8
     * {@inheritdoc}
30 8
     */
31
    public function __invoke(ResourceObject $ro, array $server)
32
    {
33
        $isModifed = $this->condResponse->isModified($ro, $server);
34 8
        $output = $isModifed ? $this->getOutput($ro, $server) : $this->condResponse->getOutput($ro->headers);
35
36
        // header
37 8
        foreach ($output->headers as $label => $value) {
38 8
            header("{$label}: {$value}", false);
39
        }
40 9
41
        // code
42 9
        http_response_code($output->code);
43 9
44 9
        // body
45
        echo $output->view;
46
    }
47
48
    private function getOutput(ResourceObject $ro, array $server) : Output
49
    {
50
        return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?: $ro->toString());
51 1
    }
52
}
53