Completed
Pull Request — master (#128)
by Fabien
18:02 queued 08:06
created

Formatter::formatResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Http\HttplugBundle\Collector;
4
5
use Exception;
6
use Http\Client\Exception\HttpException;
7
use Http\Client\Exception\TransferException;
8
use Http\Message\Formatter as FormatterInterface;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * @author Fabien Bourigault <[email protected]>
14
 *
15
 * @internal
16
 */
17
class Formatter implements FormatterInterface
18
{
19
    /**
20
     * @var FormatterInterface
21
     */
22
    private $formatter;
23
24
    /**
25
     * @param FormatterInterface $formatter
26
     */
27
    public function __construct(FormatterInterface $formatter)
28
    {
29
        $this->formatter = $formatter;
30
    }
31
32
    /**
33
     * Formats an exception.
34
     *
35
     * @param Exception $exception
36
     *
37
     * @return string
38
     */
39
    public function formatException(Exception $exception)
40
    {
41
        if ($exception instanceof HttpException) {
42
            return $this->formatter->formatResponse($exception->getResponse());
43
        }
44
45
        if ($exception instanceof TransferException) {
46
            return $exception->getMessage();
47
        }
48
49
        return sprintf('Unexpected exception of type "%s"', get_class($exception));
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function formatRequest(RequestInterface $request)
56
    {
57
        return $this->formatter->formatRequest($request);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function formatResponse(ResponseInterface $response)
64
    {
65
        return $this->formatter->formatResponse($response);
66
    }
67
}
68