Completed
Pull Request — master (#128)
by Fabien
09:03
created

Formatter::formatException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
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 6
    public function __construct(MessageFormatter $formatter)
30
    {
31 6
        $this->formatter = $formatter;
32 6
    }
33
34
    /**
35
     * Formats an exception.
36
     *
37
     * @param Exception $exception
38
     *
39
     * @return string
40
     */
41 3
    public function formatException(Exception $exception)
42
    {
43 3
        if ($exception instanceof HttpException) {
44 1
            return $this->formatter->formatResponse($exception->getResponse());
45
        }
46
47 2
        if ($exception instanceof TransferException) {
48 1
            return sprintf('Transfer error: %s', $exception->getMessage());
49
        }
50
51 1
        return sprintf('Unexpected exception of type "%s": %s', get_class($exception), $exception->getMessage());
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function formatRequest(RequestInterface $request)
58
    {
59 1
        return $this->formatter->formatRequest($request);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function formatResponse(ResponseInterface $response)
66
    {
67 1
        return $this->formatter->formatResponse($response);
68
    }
69
}
70