Completed
Pull Request — master (#116)
by
unknown
02:33
created

CurlCommandFormatter::formatRequest()   C

Complexity

Conditions 11
Paths 153

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.1486

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 25
cts 28
cp 0.8929
rs 6.875
c 0
b 0
f 0
cc 11
nc 153
nop 1
crap 11.1486

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
40 4
            $argMaxLength = '\\' === DIRECTORY_SEPARATOR ? 8192 : 2097152;
41
42 4
            if ($body->getSize() > $argMaxLength) {
43 1
                $data = '[too long stream omitted]';
44 3
            } elseif ($body->isSeekable()) {
45 2
                $data = $body->__toString();
46 2
                $body->rewind();
47 2
                if (preg_match('/[\x00-\x1F\x7F]/', $data)) {
48 2
                    $data = '[binary stream omitted]';
49
                }
50
            } else {
51 1
                $data = '[non-seekable stream omitted]';
52
            }
53 4
            $escapedData = @escapeshellarg($data);
54 4
            if (empty($escapedData)) {
55
                $escapedData = 'We couldn\'t not escape the data properly';
56
            }
57
58 4
            $command .= sprintf(' --data %s', $escapedData);
59
        }
60
61 6
        return $command;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function formatResponse(ResponseInterface $response)
68
    {
69 1
        return '';
70
    }
71
72
    /**
73
     * @param RequestInterface $request
74
     *
75
     * @return string
76
     */
77 6
    private function getHeadersAsCommandOptions(RequestInterface $request)
78
    {
79 6
        $command = '';
80 6
        foreach ($request->getHeaders() as $name => $values) {
81 2
            if ('host' === strtolower($name) && $values[0] === $request->getUri()->getHost()) {
82
                continue;
83
            }
84
85 2
            if ('user-agent' === strtolower($name)) {
86 1
                $command .= sprintf(' -A %s', escapeshellarg($values[0]));
87
88 1
                continue;
89
            }
90
91 1
            $command .= sprintf(' -H %s', escapeshellarg($name.': '.$request->getHeaderLine($name)));
92
        }
93
94 6
        return $command;
95
    }
96
}
97