HttpResponder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 3
A getOutput() 0 5 1
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
use Override;
10
11
class HttpResponder implements TransferInterface
12
{
13
    public function __construct(
14
        private HeaderInterface $header,
15 9
        private ConditionalResponseInterface $condResponse,
16
    ) {
17 9
    }
18 1
19
    /**
20 1
     * {@inheritDoc}
21
     */
22
    #[Override]
23
    public function __invoke(ResourceObject $ro, array $server): void
24 8
    {
25 8
        /** @var array{HTTP_IF_NONE_MATCH?: string} $server */
26
        $isModifed = $this->condResponse->isModified($ro, $server);
27
        $output = $isModifed ? $this->getOutput($ro, $server) : $this->condResponse->getOutput($ro->headers);
0 ignored issues
show
Bug introduced by
$ro->headers of type BEAR\Resource\Headers is incompatible with the type array expected by parameter $headers of BEAR\Sunday\Provide\Tran...eInterface::getOutput(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        $output = $isModifed ? $this->getOutput($ro, $server) : $this->condResponse->getOutput(/** @scrutinizer ignore-type */ $ro->headers);
Loading history...
28
29 8
        foreach ($output->headers as $label => $value) {
30 8
            // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
31
            header("{$label}: {$value}", false);
32
        }
33
34 8
        // phpcs:ignore SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFallbackGlobalName
35
        http_response_code($output->code);
36
37 8
        echo $output->view;
38 8
    }
39
40 9
    /** @param array<string, string> $server */
41
    private function getOutput(ResourceObject $ro, array $server): Output
42 9
    {
43 9
        $ro->toString(); // render and set headers
44 9
45
        return new Output($ro->code, ($this->header)($ro, $server), $ro->view ?? $ro->toString());
46
    }
47
}
48