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

VarDumperAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 4 2
B introspect() 0 25 4
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