Passed
Branch pretty-dump (f26c5c)
by Baptiste
02:47
created

VarDumperJson::supports()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
namespace Behapi\Debug\Dumper\Response;
3
4
use Psr\Http\Message\MessageInterface;
5
use Psr\Http\Message\ResponseInterface;
6
7
use Symfony\Component\VarDumper\VarDumper as SfDumper;
8
9
use Behapi\Debug\Dumper;
10
use Behapi\Debug\Dumper\UnsupportedMessage;
11
12
final class VarDumperJson implements Dumper
13
{
14
    public function dump(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
        $dump = [
25
            'Response Status' => "{$message->getStatusCode()} {$message->getReasonPhrase()}",
2 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            'Response Status' => "{$message->/** @scrutinizer ignore-call */ getStatusCode()} {$message->getReasonPhrase()}",
Loading history...
Bug introduced by
The method getReasonPhrase() does not exist on Psr\Http\Message\MessageInterface. It seems like you code against a sub-type of Psr\Http\Message\MessageInterface such as Psr\Http\Message\ResponseInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            'Response Status' => "{$message->getStatusCode()} {$message->/** @scrutinizer ignore-call */ getReasonPhrase()}",
Loading history...
26
        ];
27
28
        foreach ($headers as $header) {
29
            $dump["Response {$header}"] = $message->getHeaderLine($header);
30
        }
31
32
        $body = (string) $message->getBody();
33
34
        if (!empty($body)) {
35
            $dump['Response Body'] = json_decode($body);
36
        }
37
38
        SfDumper::dump($dump);
39
    }
40
41
    public function supports(MessageInterface $message): bool
42
    {
43
        if (!class_exists(SfDumper::class)) {
44
            return false;
45
        }
46
47
        if (!$message instanceof ResponseInterface) {
48
            return false;
49
        }
50
51
        [$contentType,] = explode(';', $message->getHeaderLine('Content-Type'), 2);
52
53
        return 'application/json' === $contentType;
54
    }
55
}
56