AbstractLocation::resolveRecursively()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8.048

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 20
cts 22
cp 0.9091
rs 8.4444
c 0
b 0
f 0
cc 8
nc 8
nop 2
crap 8.048
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 Psr\Http\Message\RequestInterface;
8
9
abstract class AbstractLocation implements RequestLocationInterface
10
{
11
    /** @var string */
12
    protected $locationName;
13
14
    /**
15
     * Set the name of the location
16
     *
17
     * @param $locationName
18
     */
19 10
    public function __construct($locationName)
20
    {
21 10
        $this->locationName = $locationName;
22 10
    }
23
24
    /**
25
     * @param CommandInterface $command
26
     * @param RequestInterface $request
27
     * @param Parameter $param
28
     * @return RequestInterface
29
     */
30
    public function visit(
31
        CommandInterface $command,
32
        RequestInterface $request,
33
        Parameter $param
34
    ) {
35
        return $request;
36
    }
37
38
    /**
39
     * @param CommandInterface $command
40
     * @param RequestInterface $request
41
     * @param Operation $operation
42
     * @return RequestInterface
43
     */
44
    public function after(
45
        CommandInterface $command,
46
        RequestInterface $request,
47
        Operation $operation
48
    ) {
49
        return $request;
50
    }
51
52
    /**
53
     * Prepare (filter and set desired name for request item) the value for
54
     * request.
55
     *
56
     * @param mixed     $value
57
     * @param Parameter $param
58
     *
59
     * @return array|mixed
60
     */
61 8
    protected function prepareValue($value, Parameter $param)
62
    {
63 8
        return is_array($value)
64 8
            ? $this->resolveRecursively($value, $param)
65 8
            : $param->filter($value);
66
    }
67
68
    /**
69
     * Recursively prepare and filter nested values.
70
     *
71
     * @param array     $value Value to map
72
     * @param Parameter $param Parameter related to the current key.
73
     *
74
     * @return array Returns the mapped array
75
     */
76 3
    protected function resolveRecursively(array $value, Parameter $param)
77
    {
78 3
        foreach ($value as $name => &$v) {
79 3
            switch ($param->getType()) {
80 3
                case 'object':
81 1
                    if ($subParam = $param->getProperty($name)) {
82 1
                        $key = $subParam->getWireName();
83 1
                        $value[$key] = $this->prepareValue($v, $subParam);
84 1
                        if ($name != $key) {
85
                            unset($value[$name]);
86
                        }
87 1
                    } elseif ($param->getAdditionalProperties() instanceof Parameter) {
88 1
                        $v = $this->prepareValue($v, $param->getAdditionalProperties());
89 1
                    }
90 1
                    break;
91 3
                case 'array':
92 1
                    if ($items = $param->getItems()) {
93 1
                        $v = $this->prepareValue($v, $items);
94 1
                    }
95 1
                    break;
96 3
            }
97 3
        }
98
99 3
        return $param->filter($value);
100
    }
101
}
102