Completed
Push — master ( 90a9ca...a0f42a )
by David
09:01
created

Formatter::formatException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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 MessageFormatter;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * This class is a decorator for any Http\Message\Formatter with the the ability to format exceptions.
14
 *
15
 * @author Fabien Bourigault <[email protected]>
16
 *
17
 * @internal
18
 */
19
class Formatter implements MessageFormatter
20
{
21
    /**
22
     * @var MessageFormatter
23
     */
24
    private $formatter;
25
26
    /**
27
     * @param MessageFormatter $formatter
28
     */
29
    public function __construct(MessageFormatter $formatter)
30
    {
31
        $this->formatter = $formatter;
32
    }
33
34
    /**
35
     * Formats an exception.
36
     *
37
     * @param Exception $exception
38
     *
39
     * @return string
40
     */
41
    public function formatException(Exception $exception)
42
    {
43
        if ($exception instanceof HttpException) {
44
            return $this->formatter->formatResponse($exception->getResponse());
45
        }
46
47
        if ($exception instanceof TransferException) {
48
            return sprintf('Transfer error: %s', $exception->getMessage());
49
        }
50
51
        return sprintf('Unexpected exception of type "%s": %s', get_class($exception), $exception->getMessage());
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function formatRequest(RequestInterface $request)
58
    {
59
        return $this->formatter->formatRequest($request);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function formatResponse(ResponseInterface $response)
66
    {
67
        return $this->formatter->formatResponse($response);
68
    }
69
}
70