HttpResponder::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 8
b 0
f 0
nc 4
nop 2
dl 0
loc 15
ccs 5
cts 5
cp 1
crap 3
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 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