EchoerAdapter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 3 1
A 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
    /** @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