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

src/RequestLocation/QueryLocation.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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