ParametersBag   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 51
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 2
B buildParameters() 0 26 5
1
<?php
2
3
namespace ComradeReader\Collection\Parameters;
4
5
/**
6
 * @package ComradeReader\Collection\Parameters
7
 */
8
class ParametersBag implements ParametersBagInterface
9
{
10
    /**
11
     * @var array $parameters
12
     */
13
    private $parameters;
14
15
    public function set($parameters): ParametersBagInterface
16
    {
17
        if (!is_array($parameters)) {
18
            throw new \InvalidArgumentException('$parameters for ParamtersBag should be an array');
19
        }
20
21
        $this->parameters = $parameters;
22
        return $this;
23
    }
24
25
    /**
26
     * @param string $url
27
     * @param string $method
28
     * @param array $parameters
29
     *
30
     * @return array
31
     */
32
    public function buildParameters(string $url, string $method, array $parameters)
33
    {
34
        if (!is_array($this->parameters)) {
35
            throw new \InvalidArgumentException('$parameters should be of type array');
36
        }
37
38
        // merge query string from the url
39
        $extractedQueryFromUrl = parse_url($url, PHP_URL_QUERY);
40
41
        if (strlen($extractedQueryFromUrl) > 0) {
42
            parse_str($extractedQueryFromUrl, $queryArray);
43
44
            $parameters = array_merge($parameters, $queryArray);
45
        }
46
47
        if ('POST' === $method || 'PUT' === $method) {
48
            return [
49
                'query'       => $parameters,
50
                'form_params' => $this->parameters,
51
            ];
52
        }
53
54
        return [
55
            'query' => array_merge($parameters, $this->parameters),
56
        ];
57
    }
58
}
59