Skip to content

Commit 42ecdc3

Browse files
committed
Introduce DefaultsAwareInterface
1 parent 7690d65 commit 42ecdc3

6 files changed

Lines changed: 79 additions & 15 deletions

src/DefaultsAwareInterface.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* tomkyle/matomo-api-client (https://github.com/tomkyle/matomo-api-client)
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace tomkyle\MatomoApiClient;
11+
12+
use Psr\Log;
13+
14+
interface DefaultsAwareInterface extends Log\LoggerAwareInterface
15+
{
16+
17+
/**
18+
* Gets default API parameters.
19+
*
20+
* @return array<string,string>
21+
*/
22+
public function getDefaults(): array;
23+
24+
/**
25+
* Sets default API parameters.
26+
*
27+
* @param array<string,string> $defaults
28+
*/
29+
public function setDefaults(array $defaults): self;
30+
31+
/**
32+
* Merges new API parameters into the existing defaults.
33+
*
34+
* @param array<string,string> $defaults
35+
*/
36+
public function mergeDefaults(array $defaults): self;
37+
}

src/MatomoApiClient.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* Provides a client for interacting with the Matomo API.
2323
*/
24-
class MatomoApiClient implements MatomoApiClientInterface
24+
class MatomoApiClient implements MatomoApiClientInterface, DefaultsAwareInterface
2525
{
2626
use Log\LoggerAwareTrait;
2727

@@ -162,19 +162,15 @@ public function setApi(UriInterface $uri): self
162162
}
163163

164164
/**
165-
* Gets default API parameters.
166-
*
167-
* @return array<string,string>
165+
* @inheritDoc
168166
*/
169167
public function getDefaults(): array
170168
{
171169
return $this->defaults;
172170
}
173171

174172
/**
175-
* Sets default API parameters.
176-
*
177-
* @param array<string,string> $defaults
173+
* @inheritDoc
178174
*/
179175
public function setDefaults(array $defaults): self
180176
{
@@ -183,9 +179,7 @@ public function setDefaults(array $defaults): self
183179
}
184180

185181
/**
186-
* Merges new API parameters into the existing defaults.
187-
*
188-
* @param array<string,string> $defaults
182+
* @inheritDoc
189183
*/
190184
public function mergeDefaults(array $defaults): self
191185
{

src/MatomoApiClientTrait.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,37 @@ public function getMatomoClient(): MatomoApiClientInterface
4242
return $this->matomo_client;
4343
}
4444

45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function getDefaults(): array
50+
{
51+
if ($this->matomo_client instanceOf DefaultsAwareInterface) {
52+
return $this->matomo_client->getDefaults();
53+
}
54+
return [];
55+
}
56+
57+
/**
58+
* @inheritDoc
59+
*/
60+
public function setDefaults(array $defaults): self
61+
{
62+
if ($this->matomo_client instanceOf DefaultsAwareInterface) {
63+
$this->matomo_client->setDefaults($defaults);
64+
}
65+
return $this;
66+
}
67+
68+
/**
69+
* @inheritDoc
70+
*/
71+
public function mergeDefaults(array $defaults): self
72+
{
73+
if ($this->matomo_client instanceOf DefaultsAwareInterface) {
74+
$this->matomo_client->mergeDefaults($defaults);
75+
}
76+
return $this;
77+
}
4578
}

src/ProcessingMatomoApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Psr\Log;
1313

14-
class ProcessingMatomoApiClient implements MatomoApiClientInterface
14+
class ProcessingMatomoApiClient implements MatomoApiClientInterface, DefaultsAwareInterface
1515
{
1616
use MatomoApiClientTrait;
1717
use Log\LoggerAwareTrait;

src/Psr6CacheMatomoApiClient.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Psr\Cache\CacheItemPoolInterface;
1313
use Psr\Log;
1414

15-
class Psr6CacheMatomoApiClient implements MatomoApiClientInterface
15+
class Psr6CacheMatomoApiClient implements MatomoApiClientInterface, DefaultsAwareInterface
1616
{
1717
use MatomoApiClientTrait;
1818
use Log\LoggerAwareTrait;
@@ -23,7 +23,7 @@ class Psr6CacheMatomoApiClient implements MatomoApiClientInterface
2323
* @var CacheItemPoolInterface
2424
*/
2525
protected $cache;
26-
26+
2727
/**
2828
* Constructs a new cache proxy instance.
2929
*
@@ -58,7 +58,7 @@ public function request(array $params, ?string $method = null): array
5858
$this->logger->log(Log\LogLevel::INFO, "Matomo API response found in cache", ['cacheKey' => $cacheKey]);
5959
$result = $cacheItem->get();
6060
if (is_array($result)) {
61-
return $result;
61+
return $result;
6262
}
6363

6464
throw new \UnexpectedValueException("Expected cached Matomo API response to be an array.");

src/RetryingMatomoApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use Psr\Log;
1313

14-
class RetryingMatomoApiClient implements MatomoApiClientInterface
14+
class RetryingMatomoApiClient implements MatomoApiClientInterface, DefaultsAwareInterface
1515
{
1616
use MatomoApiClientTrait;
1717
use Log\LoggerAwareTrait;

0 commit comments

Comments
 (0)