Completed
Push — master ( c3cfe8...3b1f33 )
by Tobias
04:47
created

Formatter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 70
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A formatException() 0 12 3
A formatRequest() 0 4 1
A formatResponse() 0 4 1
A formatAsCurlCommand() 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 Http\Message\Formatter\CurlCommandFormatter;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * This class is a decorator for any Http\Message\Formatter with the the ability to format exceptions and requests as
15
 * cURL commands.
16
 *
17
 * @author Fabien Bourigault <[email protected]>
18
 *
19
 * @internal
20
 */
21
class Formatter implements MessageFormatter
22
{
23
    /**
24
     * @var MessageFormatter
25
     */
26
    private $formatter;
27
28
    /**
29
     * @var CurlCommandFormatter
30
     */
31
    private $curlFormatter;
32
33
    /**
34
     * @param MessageFormatter     $formatter
35
     * @param CurlCommandFormatter $curlFormatter
36
     */
37 7
    public function __construct(MessageFormatter $formatter, CurlCommandFormatter $curlFormatter)
38
    {
39 7
        $this->formatter = $formatter;
40 7
        $this->curlFormatter = $curlFormatter;
41 7
    }
42
43
    /**
44
     * Formats an exception.
45
     *
46
     * @param Exception $exception
47
     *
48
     * @return string
49
     */
50 3
    public function formatException(Exception $exception)
51
    {
52 3
        if ($exception instanceof HttpException) {
53 1
            return $this->formatter->formatResponse($exception->getResponse());
54
        }
55
56 2
        if ($exception instanceof TransferException) {
57 1
            return sprintf('Transfer error: %s', $exception->getMessage());
58
        }
59
60 1
        return sprintf('Unexpected exception of type "%s": %s', get_class($exception), $exception->getMessage());
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function formatRequest(RequestInterface $request)
67
    {
68 1
        return $this->formatter->formatRequest($request);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function formatResponse(ResponseInterface $response)
75
    {
76 1
        return $this->formatter->formatResponse($response);
77
    }
78
79
    /**
80
     * Format a RequestInterface as a cURL command.
81
     *
82
     * @param RequestInterface $request
83
     *
84
     * @return string
85
     */
86 1
    public function formatAsCurlCommand(RequestInterface $request)
87
    {
88 1
        return $this->curlFormatter->formatRequest($request);
89
    }
90
}
91