PrepareUrl::getPayloadParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TheIconic\Tracking\GoogleAnalytics\Network;
4
5
use TheIconic\Tracking\GoogleAnalytics\Parameters\General\CacheBuster;
6
use TheIconic\Tracking\GoogleAnalytics\Parameters\SingleParameter;
7
use TheIconic\Tracking\GoogleAnalytics\Parameters\CompoundParameterCollection;
8
9
/**
10
 * Class PrepareUrl
11
 *
12
 * Builds the URL.
13
 *
14
 * @package TheIconic\Tracking\GoogleAnalytics
15
 */
16
class PrepareUrl
17
{
18
    /**
19
     * @var array
20
     */
21
    private $payloadParameters;
22
23
    /**
24
     * @var string
25
     */
26
    private $cacheBuster = '';
27
28
    /**
29
     * Build URL which is sent to Google Analytics
30
     *
31
     * @internal
32
     * @param string $url
33
     * @param SingleParameter[] $singleParameters
34
     * @param CompoundParameterCollection[] $compoundParameters
35
     * @return string
36
     */
37
    public function build($url, array $singleParameters, array $compoundParameters, $onlyQuery = false)
38
    {
39
        $singlesPost = $this->getSingleParametersPayload($singleParameters);
40
41
        $compoundsPost = $this->getCompoundParametersPayload($compoundParameters);
42
43
        $this->payloadParameters = array_merge($singlesPost, $compoundsPost);
44
45
        if (!empty($this->cacheBuster)) {
46
            $this->payloadParameters['z'] = $this->cacheBuster;
47
        }
48
        $query = http_build_query($this->payloadParameters, null, ini_get('arg_separator.output'), PHP_QUERY_RFC3986);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $numeric_prefix of http_build_query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $query = http_build_query($this->payloadParameters, /** @scrutinizer ignore-type */ null, ini_get('arg_separator.output'), PHP_QUERY_RFC3986);
Loading history...
49
        return $onlyQuery ? $query : ($url . '?' . $query);
50
    }
51
52
    /**
53
     * @internal
54
     * @return array
55
     */
56
    public function getPayloadParameters()
57
    {
58
        return $this->payloadParameters;
59
    }
60
61
    /**
62
     * Prepares all the Single Parameters to be sent to GA.
63
     *
64
     * @param SingleParameter[] $singleParameters
65
     * @return array
66
     */
67
    private function getSingleParametersPayload(array $singleParameters)
68
    {
69
        $postData = [];
70
        $cacheBuster = new CacheBuster();
71
72
        foreach ($singleParameters as $parameterObj) {
73
            if ($parameterObj->getName() === $cacheBuster->getName()) {
74
                $this->cacheBuster = $parameterObj->getValue();
75
                continue;
76
            }
77
78
            $postData[$parameterObj->getName()] = $parameterObj->getValue();
79
        }
80
81
        return $postData;
82
    }
83
84
    /**
85
     * Prepares compound parameters inside collections to be sent to GA.
86
     *
87
     * @param CompoundParameterCollection[] $compoundParameters
88
     * @return array
89
     */
90
    private function getCompoundParametersPayload(array $compoundParameters)
91
    {
92
        $postData = [];
93
94
        foreach ($compoundParameters as $compoundCollection) {
95
            $parameterArray = $compoundCollection->getParametersArray();
96
            $postData = array_merge($postData, $parameterArray);
97
        }
98
99
        return $postData;
100
    }
101
}
102