HttpResponder::getOutput()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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
    ) {
16
    }
17
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function __invoke(ResourceObject $ro, array $server): void
22
    {
23
        /** @var array{HTTP_IF_NONE_MATCH?: string} $server */
24
        $isModifed = $this->condResponse->isModified($ro, $server);
25
        $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
            header("{$label}: {$value}", false);
30
        }
31
32
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
33
        http_response_code($output->code);
34
35
        echo $output->view;
36
    }
37
38
    /** @param array<string, string> $server */
39
    private function getOutput(ResourceObject $ro, array $server): Output
40
    {
41
        $ro->toString(); // render and set headers
42
43
        return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?: $ro->toString());
44
    }
45
}
46