Completed
Push — master ( 60e022...9de47a )
by Stefano
03:52
created

MultiPartLocation::after()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 3
crap 2
1
<?php
2
namespace GuzzleHttp\Command\Guzzle\RequestLocation;
3
4
use GuzzleHttp\Command\CommandInterface;
5
use GuzzleHttp\Command\Guzzle\Operation;
6
use GuzzleHttp\Command\Guzzle\Parameter;
7
use GuzzleHttp\Psr7;
8
use Psr\Http\Message\RequestInterface;
9
10
/**
11
 * Adds POST files to a request
12
 */
13
class MultiPartLocation extends AbstractLocation
14
{
15
    /** @var string $contentType */
16
    protected $contentType = 'multipart/form-data; boundary=';
17
18
    /** @var array $formParamsData */
19
    protected $multipartData = [];
20
21
    /**
22
     * Set the name of the location
23
     *
24
     * @param string $locationName
25
     */
26 1
    public function __construct($locationName = 'multipart')
27
    {
28 1
        parent::__construct($locationName);
29 1
    }
30
31
    /**
32
     * @param CommandInterface $command
33
     * @param RequestInterface $request
34
     * @param Parameter $param
35
     * @return RequestInterface
36
     */
37 1
    public function visit(
38
        CommandInterface $command,
39
        RequestInterface $request,
40
        Parameter $param
41
    ) {
42 1
        $this->multipartData[] = [
43 1
            'name' => $param->getWireName(),
44 1
            'contents' => $this->prepareValue($command[$param->getName()], $param)
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
45 1
        ];
46
47 1
        return $request;
48
    }
49
50
51
    /**
52
     * @param CommandInterface $command
53
     * @param RequestInterface $request
54
     * @param Operation $operation
55
     * @return RequestInterface
56
     */
57 1
    public function after(
58
        CommandInterface $command,
59
        RequestInterface $request,
60
        Operation $operation
61
    ) {
62 1
        $data = $this->multipartData;
63 1
        $this->multipartData = [];
64 1
        $modify = [];
65
66 1
        $body = new Psr7\MultipartStream($data);
67 1
        $modify['body'] = Psr7\stream_for($body);
68 1
        $request = Psr7\modify_request($request, $modify);
69 1
        if ($request->getBody() instanceof Psr7\MultipartStream) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Psr7\MultipartStream does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
70
            // Use a multipart/form-data POST if a Content-Type is not set.
71 1
            $request->withHeader('Content-Type', $this->contentType . $request->getBody()->getBoundary());
72 1
        }
73
74 1
        return $request;
75
    }
76
}
77