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

EchoerAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A introspect() 0 21 4
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