Completed
Push — master ( 395fc6...529de4 )
by Baptiste
9s
created

EchoerAdapter::introspect()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 2
1
<?php declare(strict_types=1);
2
namespace Behapi\Debug\Introspection\Response;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\ResponseInterface;
6
7
use Behapi\Debug\Introspection\Adapter;
8
use Behapi\Debug\Introspection\UnsupportedMessage;
9
10
final class EchoerAdapter implements Adapter
11
{
12
    // 1 - key
13
    // 2 - value
14
    private const TEMPLATE = "\033[36m| \033[1m%s : \033[0;36m%s\033[0m\n";
15
16
    public function introspect(MessageInterface $message, array $headers): void
17
    {
18
        if (!$this->supports($message)) {
19
            throw new UnsupportedMessage($message, ResponseInterface::class);
20
        }
21
22
        assert($message instanceof ResponseInterface);
23
24
        echo "\n";
25
26
        printf(self::TEMPLATE, 'Response status', "{$message->getStatusCode()} {$message->getReasonPhrase()}");
27
28
        foreach ($headers as $header) {
29
            printf(self::TEMPLATE, "Response {$header}", $message->getHeaderLine($header));
30
        }
31
32
        $body = (string) $message->getBody();
33
34
        if (!empty($body)) {
35
            echo "\n{$body}";
36
        }
37
38
        echo "\n";
39
    }
40
41
    public function supports(MessageInterface $message): bool
42
    {
43
        return $message instanceof ResponseInterface;
44
    }
45
}
46