Skip to content

Commit 5bdcb75

Browse files
committed
feat: add psr 18 support
1 parent 76f1e2e commit 5bdcb75

File tree

5 files changed

+24
-106
lines changed

5 files changed

+24
-106
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
config.php
22
composer.lock
33
.phpunit.result.cache
4+
.phpunit.cache
45
test-results
56

67
.idea/

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22

3+
### 2.1.0 (2022/06/15)
4+
5+
* Implemented [PSR-18](https://www.php-fig.org/psr/psr-18/) support
6+
37
### 2.0.0 (2022/06/14)
48

59
* Switched from httpful to guzzlehttp

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ So you can execute multiple requests without specifying the parameters again.
3232

3333
### siteId
3434

35-
The ID of your website, single number, list separated through comma "1,4,7", or "all"
35+
The ID of your website, single number, list separated through comma `"1,4,7"`, or `"all"`.
3636

3737
### period
3838

@@ -50,9 +50,7 @@ If you set the period to `Matomo::PERIOD_RANGE` you can specify the range via
5050
$matomo->setRange('2012-01-14', Matomo::DATE_YESTERDAY); //All data from the first date until yesterday
5151
$matomo->setRange('2012-01-14'); //All data from the first date until now
5252

53-
__When you use the period range you do not need to specify a date!__
54-
55-
If you set it to something other than `Matomo::PERIOD_RANGE` you can specify the date via
53+
If you set it to something other than `Matomo::PERIOD_RANGE` you can specify the date via:
5654

5755
$matomo->setPeriod(x);
5856
$matomo->setDate('2012-03-03');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": ">=8.2",
77
"ext-json": "*",
88
"ext-curl": "*",
9-
"guzzlehttp/guzzle": "^7.7"
9+
"php-http/guzzle7-adapter": "^1.0"
1010
},
1111
"require-dev": {
1212
"roave/security-advisories": "dev-latest",

src/Matomo.php

Lines changed: 16 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace VisualAppeal;
44

5-
use GuzzleHttp\Client;
6-
use GuzzleHttp\Exception\GuzzleException;
5+
use GuzzleHttp\Psr7\Request;
6+
use Http\Adapter\Guzzle7\Client as GuzzleAdapter;
77
use InvalidArgumentException;
88
use JsonException;
9+
use Psr\Http\Client\ClientExceptionInterface;
10+
use Psr\Http\Client\ClientInterface;
911

1012
/**
1113
* Repository: https://github.com/VisualAppeal/Matomo-PHP-API
@@ -87,24 +89,9 @@ class Matomo
8789
private bool $_isJsonDecodeAssoc = false;
8890

8991
/**
90-
* @var bool If the certificate of the matomo installation should be verified.
92+
* @var ClientInterface|null HTTP client interface
9193
*/
92-
private bool $_verifySsl = false;
93-
94-
/**
95-
* @var int How many redirects curl should execute until aborting.
96-
*/
97-
private int $_maxRedirects = 5;
98-
99-
/**
100-
* @var int Timeout in seconds.
101-
*/
102-
private int $_timeout = 5;
103-
104-
/**
105-
* @var Client|null Guzzle client
106-
*/
107-
private ?Client $_client = null;
94+
private ?ClientInterface $_client = null;
10895

10996
/**
11097
* Create a new instance.
@@ -117,7 +104,7 @@ class Matomo
117104
* @param string $date
118105
* @param string $rangeStart
119106
* @param string|null $rangeEnd
120-
* @param Client|null $client
107+
* @param ClientInterface|null $client
121108
*/
122109
public function __construct(
123110
string $site,
@@ -128,7 +115,7 @@ public function __construct(
128115
string $date = self::DATE_YESTERDAY,
129116
string $rangeStart = '',
130117
string $rangeEnd = null,
131-
Client $client = null,
118+
ClientInterface $client = null,
132119
) {
133120
$this->_site = $site;
134121
$this->_token = $token;
@@ -147,7 +134,7 @@ public function __construct(
147134
if ($client !== null) {
148135
$this->setClient($client);
149136
} else {
150-
$this->setClient(new Client());
137+
$this->setClient(new GuzzleAdapter);
151138
}
152139
}
153140

@@ -430,85 +417,17 @@ public function setIsJsonDecodeAssoc(bool $isJsonDecodeAssoc): Matomo
430417
}
431418

432419
/**
433-
* If the certificate of the matomo installation should be verified.
434-
*
435-
* @return bool
436-
*/
437-
public function getVerifySsl(): bool
438-
{
439-
return $this->_verifySsl;
440-
}
441-
442-
/**
443-
* Set if the certificate of the matomo installation should be verified.
444-
*
445-
* @param bool $verifySsl
446-
*
447-
* @return Matomo
448-
*/
449-
public function setVerifySsl(bool $verifySsl): Matomo
450-
{
451-
$this->_verifySsl = $verifySsl;
452-
453-
return $this;
454-
}
455-
456-
/**
457-
* How many redirects curl should execute until aborting.
458-
*
459-
* @return int
460-
*/
461-
public function getMaxRedirects(): int
462-
{
463-
return $this->_maxRedirects;
464-
}
465-
466-
/**
467-
* Set how many redirects curl should execute until aborting.
468-
*
469-
* @param int $maxRedirects
470-
*
471-
* @return Matomo
472-
*/
473-
public function setMaxRedirects(int $maxRedirects): Matomo
474-
{
475-
$this->_maxRedirects = $maxRedirects;
476-
477-
return $this;
478-
}
479-
480-
/**
481-
* @return int
482-
*/
483-
public function getTimeout(): int
484-
{
485-
return $this->_timeout;
486-
}
487-
488-
/**
489-
* @param int $timeout
490-
*
491-
* @return Matomo
492-
*/
493-
public function setTimeout(int $timeout): Matomo
494-
{
495-
$this->_timeout = $timeout;
496-
497-
return $this;
498-
}
499-
500-
/**
501-
* @return Client|null
420+
* @return ClientInterface|null
502421
*/
503-
public function getClient(): ?Client
422+
public function getClient(): ?ClientInterface
504423
{
505424
return $this->_client;
506425
}
507426

508427
/**
509-
* @param Client|null $client
428+
* @param ClientInterface|null $client
510429
*/
511-
public function setClient(?Client $client): void
430+
public function setClient(?ClientInterface $client): void
512431
{
513432
$this->_client = $client;
514433
}
@@ -556,13 +475,9 @@ private function _request(
556475
$format = $overrideFormat ?? $this->_format;
557476

558477
try {
559-
$response = $this->_client->get($url, [
560-
'verify' => $this->_verifySsl,
561-
'allow_redirects' => [
562-
'max' => $this->_maxRedirects,
563-
],
564-
]);
565-
} catch (GuzzleException $e) {
478+
$request = new Request('GET', $url);
479+
$response = $this->_client->sendRequest($request);
480+
} catch (ClientExceptionInterface $e) {
566481
// Network error, e.g. timeout or connection refused
567482
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
568483
}

0 commit comments

Comments
 (0)