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

VarDumperAdapter::introspect()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 5
nop 2
1
<?php declare(strict_types=1);
2
namespace Behapi\Debug\Introspection\Response;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\ResponseInterface;
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, ResponseInterface::class);
18
        }
19
20
        assert($message instanceof ResponseInterface);
21
22
        // mandatory, clearing the line
23
        // todo : check how to clear without this echo...
24
        echo "\n";
25
26
        $introspect = [
27
            'Response Status' => "{$message->getStatusCode()} {$message->getReasonPhrase()}",
28
        ];
29
30
        foreach ($headers as $header) {
31
            $introspect["Response {$header}"] = $message->getHeaderLine($header);
32
        }
33
34
        $body = (string) $message->getBody();
35
36
        if (!empty($body)) {
37
            $introspect['Response 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 ResponseInterface
47
        ;
48
    }
49
}
50