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

VarDumperAdapter::__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 Symfony\Component\VarDumper\VarDumper;
8
9
use Behapi\Debug\Introspection\Adapter;
10
use Behapi\Debug\Introspection\UnsupportedMessage;
11
12
final class VarDumperAdapter implements Adapter
13
{
14
    /** @var iterable<string> */
15
    private $headers;
16
17
    /** @param iterable<string> $headers */
18
    public function __construct(iterable $headers)
19
    {
20
        $this->headers = $headers;
21
    }
22
23
    public function introspect(MessageInterface $message): void
24
    {
25
        if (!$this->supports($message)) {
26
            throw new UnsupportedMessage($message, RequestInterface::class);
27
        }
28
29
        assert($message instanceof RequestInterface);
30
31
        // mandatory, clearing the line
32
        // todo : check how to clear without this echo...
33
        echo "\n";
34
35
        $introspect = [
36
            'Request' => "{$message->getMethod()} {$message->getUri()}",
37
        ];
38
39
        foreach ($this->headers as $header) {
40
            $introspect["Request {$header}"] = $message->getHeaderLine($header);
41
        }
42
43
        $body = (string) $message->getBody();
44
45
        if (!empty($body)) {
46
            $introspect['Request Body'] = $body;
47
        }
48
49
        VarDumper::dump($introspect);
50
    }
51
52
    public function supports(MessageInterface $message): bool
53
    {
54
        return class_exists(VarDumper::class)
55
            && $message instanceof RequestInterface
56
        ;
57
    }
58
}
59