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

VarDumperAdapter::introspect()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 2
dl 0
loc 25
rs 8.5806
c 0
b 0
f 0
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
        // mandatory, clearing the line
21
        // todo : check how to clear without this echo...
22
        echo "\n";
23
24
        $introspect = [
25
            'Response Status' => "{$message->getStatusCode()} {$message->getReasonPhrase()}",
26
        ];
27
28
        foreach ($headers as $header) {
29
            $introspect["Response {$header}"] = $message->getHeaderLine($header);
30
        }
31
32
        $body = (string) $message->getBody();
33
34
        if (!empty($body)) {
35
            $introspect['Response Body'] = $body;
36
        }
37
38
        VarDumper::dump($introspect);
39
    }
40
41
    public function supports(MessageInterface $message): bool
42
    {
43
        return class_exists(VarDumper::class)
44
            && $message instanceof ResponseInterface
45
        ;
46
    }
47
}
48