Completed
Pull Request — master (#50)
by Tobias
04:31 queued 02:06
created

CurlCommandFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 0
cbo 2
dl 0
loc 36
ccs 17
cts 18
cp 0.9444
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B formatRequest() 0 22 5
A formatResponse() 0 4 1
1
<?php
2
3
namespace Http\Message\Formatter;
4
5
use Http\Message\Formatter;
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
9
/**
10
 * A formatter that prints a cURL command for HTTP requests.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class CurlCommandFormatter implements Formatter
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 2
    public function formatRequest(RequestInterface $request)
20
    {
21 2
        $command = sprintf('curl \'%s\'', $request->getUri());
22 2
        if ($request->getProtocolVersion() === '1.0') {
23
            $command .= ' --http1.0';
24 2
        } elseif ($request->getProtocolVersion() === '2.0') {
25 1
            $command .= ' --http2';
26 1
        }
27
28 2
        $command .= ' --request '.$request->getMethod();
29
30 2
        foreach ($request->getHeaders() as $name => $values) {
31 1
            $command .= sprintf(' -H \'%s: %s\'', $name, $request->getHeaderLine($name));
32 2
        }
33
34 2
        $body = $request->getBody()->__toString();
35 2
        if (!empty($body)) {
36 1
            $command .= sprintf(' --data \'%s\'', $body);
37 1
        }
38
39 2
        return $command;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function formatResponse(ResponseInterface $response)
46
    {
47 1
        return '';
48
    }
49
}
50