Passed
Push — master ( d13d8d...dbf2b1 )
by Karel
11:09 queued 11s
created

Query::getParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chuckbe\Chuckcms\Chuck\Matomo;
4
5
/**
6
 * Default implementation of a query for the Matomo reporting API.
7
 */
8
class Query implements QueryInterface
9
{
10
    /**
11
     * An HTTP client that encapsulates a GuzzleHttp client.
12
     *
13
     * @var \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient
14
     */
15
    protected $httpClient;
16
17
    /**
18
     * Associative array of query parameters, keyed by parameter name.
19
     *
20
     * @var array
21
     */
22
    protected $parameters;
23
24
    /**
25
     * Constructs a new Query object.
26
     *
27
     * @param string $url
28
     *   The URL of the Matomo server.
29
     * @param \Chuckbe\Chuckcms\Chuck\Matomo\HttpClient $httpClient
30
     *   The HTTP client wrapper.
31
     */
32
    public function __construct($url, HttpClient $httpClient)
33
    {
34
        $this->httpClient = $httpClient;
35
        $this->httpClient->setUrl($url);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function setParameters(array $parameters)
42
    {
43
        foreach ($parameters as $name => $value) {
44
            $this->setParameter($name, $value);
45
        }
46
47
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function setParameter($name, $value)
54
    {
55
        $this->parameters[$name] = $value;
56
57
        return $this;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getParameters()
64
    {
65
        return $this->parameters;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getParameter($name)
72
    {
73
        if (!array_key_exists($name, $this->parameters)) {
74
            throw new \InvalidArgumentException("Parameter '$name' is not set.");
75
        }
76
        return $this->parameters[$name];
77
    }
78
79
    /**
80
     * Prepares the query for execution.
81
     */
82
    protected function prepareExecute()
83
    {
84
        // Set the format to JSON.
85
        $this->setParameter('format', 'json');
86
87
        // Use the reporting API.
88
        $this->setParameter('module', 'API');
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function execute()
95
    {
96
        $this->prepareExecute();
97
        $response = $this->httpClient->setRequestParameters($this->parameters)->sendRequest();
98
        return new QueryResult($response);
99
    }
100
}