AbstractMessageFormatter::formatHeaders()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Glooby\Debug\Formatter\Guzzle;
4
5
use Glooby\Debug\Formatter\FormatterInterface;
6
use Glooby\Debug\Formatter\JsonStringFormatter;
7
use Glooby\Debug\Formatter\StringFactoryFormatter;
8
use Glooby\Debug\Formatter\XmlStringFormatter;
9
use GuzzleHttp\Message\RequestInterface;
10
use GuzzleHttp\Message\ResponseInterface;
11
12
/**
13
 * @author Emil Kilhage
14
 */
15
abstract class AbstractMessageFormatter implements  FormatterInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "FormatterInterface"; 2 found
Loading history...
16
{
17
    /**
18
     * @param RequestInterface|ResponseInterface $message
19
     * @return string
20
     */
21
    protected function formatBody($message)
22
    {
23
        $header = $message->getHeader('Content-Type');
24
25
        if (JsonStringFormatter::isJsonHeader($header)) {
26
            $formatter = new JsonStringFormatter();
27
            return $formatter->format($message->getBody());
28
        } elseif (XmlStringFormatter::isXmlHeader($header)) {
29
            $formatter = new XmlStringFormatter();
30
            return $formatter->format($message->getBody());
31
        }
32
33
        $factory = new StringFactoryFormatter();
34
        return $factory->format($message->getBody());
35
    }
36
37
    /**
38
     * @param RequestInterface|ResponseInterface $message
39
     * @return string
40
     */
41
    protected function formatHeaders($message)
42
    {
43
        $headers = [];
44
45
        foreach ($message->getHeaders() as $header => $value) {
46
            $headers[] = sprintf('%s: %s', $header, implode("\n  : ", $value));
47
        }
48
49
        return implode("\n", $headers);
50
    }
51
}
52