Skip to content

Commit

Permalink
When using the image graph call use the default php format
Browse files Browse the repository at this point in the history
closes #46
  • Loading branch information
thelfensdrfer committed Aug 20, 2020
1 parent efec742 commit 5ca71ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,14 @@ public function reset(): Matomo
/**
* Make API request
*
* @param string $method
* @param array $params
* @param array $optional
* @param string $method HTTP method
* @param array $params Query parameters
* @param array $optional Optional arguments for this api call
* @param int|null $format Override the response format
* @return bool|object
* @throws InvalidRequestException
*/
private function _request(string $method, array $params = [], array $optional = [])
private function _request(string $method, array $params = [], array $optional = [], $format = null)
{
$url = $this->_parseUrl($method, $params + $optional);
if ($url === false) {
Expand All @@ -520,7 +521,7 @@ private function _request(string $method, array $params = [], array $optional =

if (!empty($buffer)) {
try {
return $this->_finishResponse($this->_parseResponse($buffer), $method, $params + $optional);
return $this->_finishResponse($this->_parseResponse($buffer, $format), $method, $params + $optional);
} catch (InvalidResponseException $e) {
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -639,11 +640,17 @@ private function _isValidResponse($response)
* Parse request result
*
* @param Response $response
* @param int|null $overrideFormat Override the default format
* @return mixed
*/
private function _parseResponse(Response $response)
private function _parseResponse(Response $response, $overrideFormat = null)
{
switch ($this->_format) {
$format = $this->_format;
if ($overrideFormat !== null) {
$format = $overrideFormat;
}

switch ($format) {
case self::FORMAT_JSON:
return json_decode($response, $this->_isJsonDecodeAssoc);
default:
Expand Down Expand Up @@ -2483,7 +2490,7 @@ public function getImageGraph(
'aliasedGraph' => $aliasedGraph,
'idGoal ' => $idGoal,
'colors' => $colors,
], $optional);
], $optional, self::FORMAT_PHP);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/MatomoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,18 @@ public function testEmptySiteId()

$this->assertIsObject($matomo->getTimezonesList());
}

/**
* Test if matamo can be used without the site ID parameter.
*/
public function testGetImageGraph()
{
/**
* @var $response \Httpful\Response
*/
$response = $this->_matomo->getImageGraph('UserCountry', 'getCountry');
$this->assertIsObject($response);
$this->assertEquals(200, $response->getStatusCode());
$this->assertStringContainsString('PNG', $response->getRawBody());
}
}

0 comments on commit 5ca71ac

Please sign in to comment.