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

VarDumperAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B introspect() 0 27 4
A supports() 0 4 2
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
    public function introspect(MessageInterface $message, array $headers): void
15
    {
16
        if (!$this->supports($message)) {
17
            throw new UnsupportedMessage($message, RequestInterface::class);
18
        }
19
20
        assert($message instanceof RequestInterface);
21
22
        // mandatory, clearing the line
23
        // todo : check how to clear without this echo...
24
        echo "\n";
25
26
        $introspect = [
27
            'Request' => "{$message->getMethod()} {$message->getUri()}",
28
        ];
29
30
        foreach ($headers as $header) {
31
            $introspect["Request {$header}"] = $message->getHeaderLine($header);
32
        }
33
34
        $body = (string) $message->getBody();
35
36
        if (!empty($body)) {
37
            $introspect['Request Body'] = $body;
38
        }
39
40
        VarDumper::dump($introspect);
41
    }
42
43
    public function supports(MessageInterface $message): bool
44
    {
45
        return class_exists(VarDumper::class)
46
            && $message instanceof RequestInterface
47
        ;
48
    }
49
}
50