Completed
Pull Request — master (#94)
by
unknown
05:42 queued 01:18
created

CurlCommandFormatter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 90.7%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 0
cbo 3
dl 0
loc 79
ccs 39
cts 43
cp 0.907
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D formatRequest() 0 40 9
A formatResponse() 0 4 1
B getHeadersAsCommandOptions() 0 19 5
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
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class CurlCommandFormatter implements Formatter
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18 5
    public function formatRequest(RequestInterface $request)
19
    {
20 5
        $command = sprintf('curl %s', escapeshellarg((string)$request->getUri()->withFragment('')));
21 5
        if ('1.0' === $request->getProtocolVersion()) {
22
            $command .= ' --http1.0';
23 5
        } elseif ('2.0' === $request->getProtocolVersion()) {
24 1
            $command .= ' --http2';
25 1
        }
26
27 5
        $method = strtoupper($request->getMethod());
28 5
        if ('HEAD' === $method) {
29
            $command .= ' --head';
30 5
        } elseif ('GET' !== $method) {
31 3
            $command .= ' --request '.$method;
32 3
        }
33
34 5
        $command .= $this->getHeadersAsCommandOptions($request);
35
36 5
        $body = $request->getBody();
37 5
        if ($body->getSize() > 0) {
38 3
            if ($body->isSeekable()) {
39 2
                $data = $body->__toString();
40 2
                $body->rewind();
41 2
                if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
42 1
                    $data = '[binary stream omitted]';
43 1
                }
44 2
            } else {
45 1
                $data = '[non-seekable stream omitted]';
46
            }
47
48 3
            $escapedData = @escapeshellarg($data);
49 3
            if (isset($php_errormsg)) {
50
                throw new \InvalidArgumentException($php_errormsg);
51
            }
52 3
            $command .= sprintf(' --data %s', $escapedData);
53
54 3
        }
55
56 5
        return $command;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function formatResponse(ResponseInterface $response)
63
    {
64 1
        return '';
65
    }
66
67
    /**
68
     * @param RequestInterface $request
69
     *
70
     * @return string
71
     */
72 5
    private function getHeadersAsCommandOptions(RequestInterface $request)
73
    {
74 5
        $command = '';
75 5
        foreach ($request->getHeaders() as $name => $values) {
76 2
            if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
77
                continue;
78
            }
79
80 2
            if ('user-agent' === strtolower($name)) {
81 1
                $command .= sprintf(' -A %s', escapeshellarg($values[0]));
82
83 1
                continue;
84
            }
85
86 1
            $command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name)));
87 5
        }
88
89 5
        return $command;
90
    }
91
}
92