Completed
Push — master ( 899138...8002fa )
by David
01:53
created

CurlCommandFormatter::getHeadersAsCommandOptions()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.3222
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.025
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 6
    public function formatRequest(RequestInterface $request)
20
    {
21 6
        $command = sprintf('curl %s', escapeshellarg((string) $request->getUri()->withFragment('')));
22 6
        if ('1.0' === $request->getProtocolVersion()) {
23
            $command .= ' --http1.0';
24 6
        } elseif ('2.0' === $request->getProtocolVersion()) {
25 1
            $command .= ' --http2';
26
        }
27
28 6
        $method = strtoupper($request->getMethod());
29 6
        if ('HEAD' === $method) {
30
            $command .= ' --head';
31 6
        } elseif ('GET' !== $method) {
32 4
            $command .= ' --request '.$method;
33
        }
34
35 6
        $command .= $this->getHeadersAsCommandOptions($request);
36
37 6
        $body = $request->getBody();
38 6
        if ($body->getSize() > 0) {
39
            // escapeshellarg argument max length on Windows, but longer body in curl command would be impractical anyways
40 4
            if ($body->getSize() > 8192) {
41 1
                $data = '[too long stream omitted]';
42 3
            } elseif ($body->isSeekable()) {
43 2
                $data = $body->__toString();
44 2
                $body->rewind();
45 2
                if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
46 2
                    $data = '[binary stream omitted]';
47
                }
48
            } else {
49 1
                $data = '[non-seekable stream omitted]';
50
            }
51 4
            $escapedData = @escapeshellarg($data);
52 4
            if (empty($escapedData)) {
53
                $escapedData = 'We couldn\'t not escape the data properly';
54
            }
55
56 4
            $command .= sprintf(' --data %s', $escapedData);
57
        }
58
59 6
        return $command;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function formatResponse(ResponseInterface $response)
66
    {
67 1
        return '';
68
    }
69
70
    /**
71
     * @param RequestInterface $request
72
     *
73
     * @return string
74
     */
75 6
    private function getHeadersAsCommandOptions(RequestInterface $request)
76
    {
77 6
        $command = '';
78 6
        foreach ($request->getHeaders() as $name => $values) {
79 2
            if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
80
                continue;
81
            }
82
83 2
            if ('user-agent' === strtolower($name)) {
84 1
                $command .= sprintf(' -A %s', escapeshellarg($values[0]));
85
86 1
                continue;
87
            }
88
89 1
            $command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name)));
90
        }
91
92 6
        return $command;
93
    }
94
}
95