Skip to content

Commit 5ca71ac

Browse files
author
Tim Helfensdörfer
committed
When using the image graph call use the default php format
closes #46
1 parent efec742 commit 5ca71ac

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Matomo.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,14 @@ public function reset(): Matomo
490490
/**
491491
* Make API request
492492
*
493-
* @param string $method
494-
* @param array $params
495-
* @param array $optional
493+
* @param string $method HTTP method
494+
* @param array $params Query parameters
495+
* @param array $optional Optional arguments for this api call
496+
* @param int|null $format Override the response format
496497
* @return bool|object
497498
* @throws InvalidRequestException
498499
*/
499-
private function _request(string $method, array $params = [], array $optional = [])
500+
private function _request(string $method, array $params = [], array $optional = [], $format = null)
500501
{
501502
$url = $this->_parseUrl($method, $params + $optional);
502503
if ($url === false) {
@@ -520,7 +521,7 @@ private function _request(string $method, array $params = [], array $optional =
520521

521522
if (!empty($buffer)) {
522523
try {
523-
return $this->_finishResponse($this->_parseResponse($buffer), $method, $params + $optional);
524+
return $this->_finishResponse($this->_parseResponse($buffer, $format), $method, $params + $optional);
524525
} catch (InvalidResponseException $e) {
525526
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
526527
}
@@ -639,11 +640,17 @@ private function _isValidResponse($response)
639640
* Parse request result
640641
*
641642
* @param Response $response
643+
* @param int|null $overrideFormat Override the default format
642644
* @return mixed
643645
*/
644-
private function _parseResponse(Response $response)
646+
private function _parseResponse(Response $response, $overrideFormat = null)
645647
{
646-
switch ($this->_format) {
648+
$format = $this->_format;
649+
if ($overrideFormat !== null) {
650+
$format = $overrideFormat;
651+
}
652+
653+
switch ($format) {
647654
case self::FORMAT_JSON:
648655
return json_decode($response, $this->_isJsonDecodeAssoc);
649656
default:
@@ -2483,7 +2490,7 @@ public function getImageGraph(
24832490
'aliasedGraph' => $aliasedGraph,
24842491
'idGoal ' => $idGoal,
24852492
'colors' => $colors,
2486-
], $optional);
2493+
], $optional, self::FORMAT_PHP);
24872494
}
24882495

24892496
/**

tests/MatomoTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,18 @@ public function testEmptySiteId()
247247

248248
$this->assertIsObject($matomo->getTimezonesList());
249249
}
250+
251+
/**
252+
* Test if matamo can be used without the site ID parameter.
253+
*/
254+
public function testGetImageGraph()
255+
{
256+
/**
257+
* @var $response \Httpful\Response
258+
*/
259+
$response = $this->_matomo->getImageGraph('UserCountry', 'getCountry');
260+
$this->assertIsObject($response);
261+
$this->assertEquals(200, $response->getStatusCode());
262+
$this->assertStringContainsString('PNG', $response->getRawBody());
263+
}
250264
}

0 commit comments

Comments
 (0)