Passed
Pull Request — master (#46)
by Baptiste
02:32
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\Request;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\RequestInterface;
6
7
use Behapi\Debug\Introspection;
8
use Behapi\Debug\Introspection\Adapter;
9
use Behapi\Debug\Introspection\UnsupportedMessage;
10
11
final class EchoerAdapter implements Adapter
12
{
13
    // 1 - key
14
    // 2 - value
15
    private const TEMPLATE = "\033[36m| \033[1m%s : \033[0;36m%s\033[0m\n";
16
17
    public function introspect(MessageInterface $message, array $headers): void
18
    {
19
        if (!$this->supports($message)) {
20
            throw new UnsupportedMessage($message, RequestInterface::class);
21
        }
22
23
        assert($message instanceof RequestInterface);
24
25
        echo "\n";
26
27
        printf(self::TEMPLATE, 'Request', "{$message->getMethod()} {$message->getUri()}");
28
29
        foreach ($headers as $header) {
30
            printf(self::TEMPLATE, "Request {$header}", $message->getHeaderLine($header));
31
        }
32
33
        $body = (string) $message->getBody();
34
35
        if (!empty($body)) {
36
            echo "\n{$body}";
37
        }
38
39
        echo "\n";
40
    }
41
42
    public function supports(MessageInterface $message): bool
43
    {
44
        return $message instanceof RequestInterface;
45
    }
46
}
47