Completed
Pull Request — develop (#605)
by
unknown
14:48
created

PreProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 45
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 34 7
1
<?php
2
/**
3
 * Schema Class for output data.
4
 */
5
namespace Graviton\ProxyApiBundle\Processor;
6
7
use Graviton\ProxyApiBundle\Helper\HttpHelper;
8
use Graviton\ProxyApiBundle\Model\ProxyModel;
9
use Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Before Proxy Request process and prepare data for orequest
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class PreProcessor implements PreProcessorInterface
19
{
20
    /**
21
     * Filter or change values of incoming request to be processed
22
     *
23
     * @param Request    $originalRequest Incoming current request
24
     * @param HttpHelper $httpHelper      Building the new request
25
     * @param ProxyModel $proxyModel      Configuration model
26
     * @return HttpHelper
27
     */
28
    public function process(
29
        Request $originalRequest,
30
        HttpHelper $httpHelper,
31
        ProxyModel $proxyModel
32
    ) {
33
        $httpHelper->setMethod($originalRequest->getMethod());
34
35
        $queryParams = (array) $originalRequest->query->all();
36
37
        // Query Params, it will remove unwanted params and map to new.
38
        if ($optParams = $proxyModel->getQueryParams()) {
39
            $newParamsString = http_build_query($optParams);
40
            foreach ($queryParams as $key => $value) {
41
                // Remove attempt to inject with %26 = & in value.
42
                $value = strpos($value, '&') !== false ? strstr($value, '&', true) : $value;
43
                $newParamsString = str_replace('%7B'.$key.'%7D', trim($value), $newParamsString);
44
            }
45
            parse_str($newParamsString, $queryParams);
46
        }
47
48
        // Map additional params, append additional params
49
        if ($optParams = $proxyModel->getQueryAdditionals()) {
50
            foreach ($optParams as $key => $value) {
51
                $queryParams[$key] = $value;
52
            }
53
        }
54
55
        // Add to HTTP Query Params
56
        foreach ($queryParams as $name => $value) {
0 ignored issues
show
Bug introduced by
The expression $queryParams of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
            $httpHelper->addQueryParams($name, $value);
58
        }
59
60
        return $httpHelper;
61
    }
62
}
63