Completed
Push — master ( ad34f0...67a90e )
by Stefano
32s
created

QueryLocation::visit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 3
crap 1
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 query string values to requests
12
 */
13
class QueryLocation extends AbstractLocation
14
{
15
    /**
16
     * Set the name of the location
17
     *
18
     * @param string $locationName
19
     */
20 5
    public function __construct($locationName = 'query')
21
    {
22 5
        parent::__construct($locationName);
23 5
    }
24
25
    /**
26
     * @param CommandInterface $command
27
     * @param RequestInterface $request
28
     * @param Parameter        $param
29
     *
30
     * @return RequestInterface
31
     */
32 4
    public function visit(
33
        CommandInterface $command,
34
        RequestInterface $request,
35
        Parameter $param
36
    ) {
37 4
        $uri = $request->getUri();
38 4
        $query = Psr7\parse_query($uri->getQuery());
39
40 4
        $query[$param->getWireName()] = $this->prepareValue(
41 4
            $command[$param->getName()],
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
42
            $param
43 4
        );
44
45 4
        $uri = $uri->withQuery(http_build_query($query, null, '&', PHP_QUERY_RFC3986));
46
47 4
        return $request->withUri($uri);
48
    }
49
50
    /**
51
     * @param CommandInterface $command
52
     * @param RequestInterface $request
53
     * @param Operation        $operation
54
     *
55
     * @return RequestInterface
56
     */
57 1
    public function after(
58
        CommandInterface $command,
59
        RequestInterface $request,
60
        Operation $operation
61
    ) {
62 1
        $additional = $operation->getAdditionalParameters();
63 1
        if ($additional && $additional->getLocation() == $this->locationName) {
64 1
            foreach ($command->toArray() as $key => $value) {
65 1
                if (!$operation->hasParam($key)) {
66 1
                    $uri = $request->getUri();
67 1
                    $query = Psr7\parse_query($uri->getQuery());
68
69 1
                    $query[$key] = $this->prepareValue(
70 1
                        $value,
71
                        $additional
72 1
                    );
73
74 1
                    $uri = $uri->withQuery(http_build_query($query, null, '&', PHP_QUERY_RFC3986));
75 1
                    $request = $request->withUri($uri);
76 1
                }
77 1
            }
78 1
        }
79
80 1
        return $request;
81
    }
82
}
83