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\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
        echo "\n";
24
25
        printf(self::TEMPLATE, 'Request', "{$message->getMethod()} {$message->getUri()}");
26
27
        foreach ($headers as $header) {
28
            printf(self::TEMPLATE, "Request {$header}", $message->getHeaderLine($header));
29
        }
30
31
        $body = (string) $message->getBody();
32
33
        if (!empty($body)) {
34
            echo "\n{$body}";
35
        }
36
37
        echo "\n";
38
    }
39
40
    public function supports(MessageInterface $message): bool
41
    {
42
        return $message instanceof RequestInterface;
43
    }
44
}
45