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\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
    /** @var iterable<string> */
18
    private $headers;
19
20
    /** @param iterable<string> $headers */
21
    public function __construct(iterable $headers)
22
    {
23
        $this->headers = $headers;
24
    }
25
26
    public function introspect(MessageInterface $message): void
27
    {
28
        if (!$this->supports($message)) {
29
            throw new UnsupportedMessage($message, RequestInterface::class);
30
        }
31
32
        assert($message instanceof RequestInterface);
33
34
        echo "\n";
35
36
        printf(self::TEMPLATE, 'Request', "{$message->getMethod()} {$message->getUri()}");
37
38
        foreach ($this->headers as $header) {
39
            printf(self::TEMPLATE, "Request {$header}", $message->getHeaderLine($header));
40
        }
41
42
        $body = (string) $message->getBody();
43
44
        if (!empty($body)) {
45
            echo "\n{$body}";
46
        }
47
48
        echo "\n";
49
    }
50
51
    public function supports(MessageInterface $message): bool
52
    {
53
        return $message instanceof RequestInterface;
54
    }
55
}
56