HttpResponder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 9
c 10
b 0
f 0
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 3
A getOutput() 0 5 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
    public function __construct(
13
        private HeaderInterface $header,
14
        private ConditionalResponseInterface $condResponse,
15 9
    ) {
16
    }
17 9
18 1
    /**
19
     * {@inheritDoc}
20 1
     */
21
    public function __invoke(ResourceObject $ro, array $server): void
22
    {
23
        /** @var array{HTTP_IF_NONE_MATCH?: string} $server */
24 8
        $isModifed = $this->condResponse->isModified($ro, $server);
25 8
        $output = $isModifed ? $this->getOutput($ro, $server) : $this->condResponse->getOutput($ro->headers);
26
27
        foreach ($output->headers as $label => $value) {
28
            // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
29 8
            header("{$label}: {$value}", false);
30 8
        }
31
32
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
33
        http_response_code($output->code);
34 8
35
        echo $output->view;
36
    }
37 8
38 8
    /** @param array<string, string> $server */
39
    private function getOutput(ResourceObject $ro, array $server): Output
40 9
    {
41
        $ro->toString(); // render and set headers
42 9
43 9
        return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?: $ro->toString());
44 9
    }
45
}
46