Completed
Push — master ( 3371e5...425c01 )
by Baptiste
11s
created

EchoerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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
    /** @var iterable<string> */
17
    private $headers;
18
19
    /** @param iterable<string> $headers */
20
    public function __construct(iterable $headers)
21
    {
22
        $this->headers = $headers;
23
    }
24
25
    public function introspect(MessageInterface $message): void
26
    {
27
        if (!$this->supports($message)) {
28
            throw new UnsupportedMessage($message, ResponseInterface::class);
29
        }
30
31
        assert($message instanceof ResponseInterface);
32
33
        echo "\n";
34
35
        printf(self::TEMPLATE, 'Response status', "{$message->getStatusCode()} {$message->getReasonPhrase()}");
36
37
        foreach ($this->headers as $header) {
38
            printf(self::TEMPLATE, "Response {$header}", $message->getHeaderLine($header));
39
        }
40
41
        $body = (string) $message->getBody();
42
43
        if (!empty($body)) {
44
            echo "\n{$body}";
45
        }
46
47
        echo "\n";
48
    }
49
50
    public function supports(MessageInterface $message): bool
51
    {
52
        return $message instanceof ResponseInterface;
53
    }
54
}
55