Completed
Pull Request — master (#94)
by
unknown
07:00
created

CurlCommandFormatter::formatResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1.0156
1
<?php
2
3
namespace Http\Message\Formatter;
4
5
use Http\Message\Formatter;
6
use PhpSpec\Exception\Exception;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Http\Message\Exception\EmptyStringException;
10
11
/**
12
 * A formatter that prints a cURL command for HTTP requests.
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
class CurlCommandFormatter implements Formatter
16
{
17
    /**
18
     * {@inheritdoc}
19 3
     */
20
    public function formatRequest(RequestInterface $request)
21 3
    {
22 3
        $command = sprintf('curl %s', escapeshellarg((string)$request->getUri()->withFragment('')));
23
        if ('1.0' === $request->getProtocolVersion()) {
24 3
            $command .= ' --http1.0';
25 1
        } elseif ('2.0' === $request->getProtocolVersion()) {
26 1
            $command .= ' --http2';
27
        }
28 3
29 3
        $method = strtoupper($request->getMethod());
30
        if ('HEAD' === $method) {
31 3
            $command .= ' --head';
32 1
        } elseif ('GET' !== $method) {
33 1
            $command .= ' --request ' . $method;
34
        }
35 3
36
        $command .= $this->getHeadersAsCommandOptions($request);
37 3
38 3
        $body = $request->getBody();
39 1
        if ($body->getSize() > 0) {
40
            if ($body->isSeekable()) {
41
                $data = $body->__toString();
42 1
                $body->rewind();
43 1
                if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
44 1
                    $data = '[binary stream omitted]';
45
                }
46 3
            } else {
47
                $data = '[non-seekable stream omitted]';
48
            }
49
50
            $escapedData = @escapeshellarg($data);
51
            if (isset($php_errormsg)) {
52 1
                throw new \InvalidArgumentException($php_errormsg);
53
            }
54 1
            $command .= sprintf(' --data %s', $escapedData);
55
56
        }
57
58
        return $command;
59
    }
60
61
    /**
62 3
     * {@inheritdoc}
63
     */
64 3
    public function formatResponse(ResponseInterface $response)
65 3
    {
66 2
        return '';
67
    }
68
69
    /**
70 2
     * @param RequestInterface $request
71 1
     *
72
     * @return string
73 1
     */
74
    private function getHeadersAsCommandOptions(RequestInterface $request)
75
    {
76 1
        $command = '';
77 3
        foreach ($request->getHeaders() as $name => $values) {
78
            if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
79 3
                continue;
80
            }
81
82
            if ('user-agent' === strtolower($name)) {
83
                $command .= sprintf(' -A %s', escapeshellarg($values[0]));
84
85
                continue;
86
            }
87
88
            $command .= sprintf(' -H %s', escapeshellarg($name . ': ' . $request->getHeaderLine($name)));
89
        }
90
91
        return $command;
92
    }
93
}
94