Passed
Pull Request — master (#46)
by Baptiste
02:41
created

EchoerAdapter::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        echo "\n";
23
24
        printf(self::TEMPLATE, 'Response status', "{$message->getStatusCode()} {$message->getReasonPhrase()}");
25
26
        foreach ($headers as $header) {
27
            printf(self::TEMPLATE, "Response {$header}", $message->getHeaderLine($header));
28
        }
29
30
        $body = (string) $message->getBody();
31
32
        if (!empty($body)) {
33
            echo "\n{$body}";
34
        }
35
36
        echo "\n";
37
    }
38
39
    public function supports(MessageInterface $message): bool
40
    {
41
        return $message instanceof ResponseInterface;
42
    }
43
}
44