Completed
Pull Request — master (#128)
by Fabien
10:07
created

Formatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A formatException() 0 12 3
A formatRequest() 0 4 1
A formatResponse() 0 4 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 $exception->getMessage();
49
        }
50
51
        return sprintf('Unexpected exception of type "%s"', get_class($exception));
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