QueryLocation::after()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 9.2088
c 0
b 0
f 0
cc 5
nc 2
nop 3
crap 5
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\Command\Guzzle\QuerySerializer\QuerySerializerInterface;
8
use GuzzleHttp\Command\Guzzle\QuerySerializer\Rfc3986Serializer;
9
use GuzzleHttp\Psr7;
10
use Psr\Http\Message\RequestInterface;
11
12
/**
13
 * Adds query string values to requests
14
 */
15
class QueryLocation extends AbstractLocation
16
{
17
    /**
18
     * @var QuerySerializerInterface
19
     */
20
    private $querySerializer;
21
22
    /**
23
     * Set the name of the location
24
     *
25
     * @param string                        $locationName
26
     * @param QuerySerializerInterface|null $querySerializer
27
     */
28 3
    public function __construct($locationName = 'query', QuerySerializerInterface $querySerializer = null)
29
    {
30 3
        parent::__construct($locationName);
31
32 3
        $this->querySerializer = $querySerializer ?: new Rfc3986Serializer();
33 3
    }
34
35
    /**
36
     * @param CommandInterface $command
37
     * @param RequestInterface $request
38
     * @param Parameter        $param
39
     *
40
     * @return RequestInterface
41
     */
42 2
    public function visit(
43
        CommandInterface $command,
44
        RequestInterface $request,
45
        Parameter $param
46
    ) {
47 2
        $uri = $request->getUri();
48 2
        $query = Psr7\parse_query($uri->getQuery());
49
50 2
        $query[$param->getWireName()] = $this->prepareValue(
51 2
            $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...
52
            $param
53 2
        );
54
55 2
        $uri = $uri->withQuery($this->querySerializer->aggregate($query));
56
57 2
        return $request->withUri($uri);
58
    }
59
60
    /**
61
     * @param CommandInterface $command
62
     * @param RequestInterface $request
63
     * @param Operation        $operation
64
     *
65
     * @return RequestInterface
66
     */
67 1
    public function after(
68
        CommandInterface $command,
69
        RequestInterface $request,
70
        Operation $operation
71
    ) {
72 1
        $additional = $operation->getAdditionalParameters();
73 1
        if ($additional && $additional->getLocation() == $this->locationName) {
74 1
            foreach ($command->toArray() as $key => $value) {
75 1
                if (!$operation->hasParam($key)) {
76 1
                    $uri = $request->getUri();
77 1
                    $query = Psr7\parse_query($uri->getQuery());
78
79 1
                    $query[$key] = $this->prepareValue(
80 1
                        $value,
81
                        $additional
82 1
                    );
83
84 1
                    $uri = $uri->withQuery($this->querySerializer->aggregate($query));
85 1
                    $request = $request->withUri($uri);
86 1
                }
87 1
            }
88 1
        }
89
90 1
        return $request;
91
    }
92
}
93