Completed
Push — master ( 3371e5...425c01 )
by Baptiste
11s
created

EchoerAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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