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

EchoerAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
B introspect() 0 23 4
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