HttpResponder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 9
c 10
b 0
f 0
dl 0
loc 34
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
    ) {
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