CliResponder::getOutput()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 2
c 1
b 1
f 0
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
cc 2
nc 1
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Transfer;
6
7
use BEAR\Resource\Code;
8
use BEAR\Resource\ResourceObject;
9
use BEAR\Sunday\Extension\Transfer\TransferInterface;
10
use BEAR\Sunday\Provide\Transfer\ConditionalResponseInterface;
11
use BEAR\Sunday\Provide\Transfer\HeaderInterface;
12
use BEAR\Sunday\Provide\Transfer\Output;
13
use Override;
14
15
use const PHP_EOL;
16 1
17
final class CliResponder implements TransferInterface
18 1
{
19 1
    public function __construct(
20 1
        private HeaderInterface $header,
21
        private ConditionalResponseInterface $condResponse,
22
    ) {
23 1
    }
24 1
25
    /**
26 1
     * {@inheritDoc}
27 1
     */
28
    #[Override]
29
    public function __invoke(ResourceObject $ro, array $server): void
30 1
    {
31
        /** @var array{HTTP_IF_NONE_MATCH?: string} $server */
32 1
        $isModified = $this->condResponse->isModified($ro, $server);
33 1
        $output = $isModified ? $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

33
        $output = $isModified ? $this->getOutput($ro, $server) : $this->condResponse->getOutput(/** @scrutinizer ignore-type */ $ro->headers);
Loading history...
34 1
35
        $statusText = (new Code())->statusText[$ro->code] ?? '';
36
        $ob = $output->code . ' ' . $statusText . PHP_EOL;
37
38
        // header
39
        foreach ($output->headers as $label => $value) {
40
            $ob .= "{$label}: {$value}" . PHP_EOL;
41
        }
42
43
        // empty line
44
        $ob .= PHP_EOL;
45
46
        // body
47
        $ob .= (string) $output->view;
48
49
        echo $ob;
50
    }
51
52
    /** @param array<string, string> $server */
53
    private function getOutput(ResourceObject $ro, array $server): Output
54
    {
55
        $ro->toString(); // set headers as well
56
57
        return new Output($ro->code, ($this->header)($ro, $server), (string) $ro->view ?: $ro->toString());
58
    }
59
}
60