Skip to content

Commit bccaf53

Browse files
committed
feat: added support for php8.2 by exchanging the http library with guzzlehttp
1 parent 2e680ba commit bccaf53

File tree

4 files changed

+174
-154
lines changed

4 files changed

+174
-154
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-20.04
1313
strategy:
1414
matrix:
15-
php-versions: ['8.0', '8.1']
15+
php-versions: ['8.0', '8.1', '8.2']
1616

1717
name: PHPUnit ${{ matrix.php-versions }}
1818

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"description": "API wrapper for matomo",
44
"license": "MIT",
55
"require": {
6-
"php": ">=8.0",
6+
"php": ">=8.2",
77
"ext-json": "*",
88
"ext-curl": "*",
9-
"voku/httpful": "^2.4"
9+
"guzzlehttp/guzzle": "^7.7"
1010
},
1111
"require-dev": {
1212
"roave/security-advisories": "dev-latest",
13-
"phpunit/phpunit": "^9.0"
13+
"phpunit/phpunit": "^10.1"
1414
},
1515
"scripts": {
1616
"test": "phpunit"

src/Matomo.php

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace VisualAppeal;
44

5-
use Httpful\Exception\NetworkErrorException;
6-
use Httpful\Request;
7-
use Httpful\Response;
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
use GuzzleHttp\Psr7\Response;
88
use InvalidArgumentException;
99
use JsonException;
1010

@@ -104,6 +104,11 @@ class Matomo
104104
*/
105105
private int $_timeout = 5;
106106

107+
/**
108+
* @var Client|null Guzzle client
109+
*/
110+
private ?Client $_client = null;
111+
107112
/**
108113
* Create a new instance.
109114
*
@@ -115,6 +120,7 @@ class Matomo
115120
* @param string $date
116121
* @param string $rangeStart
117122
* @param string|null $rangeEnd
123+
* @param Client|null $client
118124
*/
119125
public function __construct(
120126
string $site,
@@ -124,7 +130,8 @@ public function __construct(
124130
string $period = self::PERIOD_DAY,
125131
string $date = self::DATE_YESTERDAY,
126132
string $rangeStart = '',
127-
string $rangeEnd = null
133+
string $rangeEnd = null,
134+
Client $client = null,
128135
) {
129136
$this->_site = $site;
130137
$this->_token = $token;
@@ -139,6 +146,12 @@ public function __construct(
139146
} else {
140147
$this->setDate($date);
141148
}
149+
150+
if ($client !== null) {
151+
$this->setClient($client);
152+
} else {
153+
$this->setClient(new Client());
154+
}
142155
}
143156

144157
/**
@@ -359,8 +372,8 @@ public function setRange(string $rangeStart = null, string $rangeEnd = null): Ma
359372
$this->_rangeEnd = $rangeEnd;
360373

361374
if (is_null($rangeEnd)) {
362-
if (str_contains($rangeStart, 'last')
363-
|| str_contains($rangeStart, 'previous')
375+
if (str_contains($rangeStart ?? '', 'last')
376+
|| str_contains($rangeStart ?? '', 'previous')
364377
) {
365378
$this->setDate($rangeStart);
366379
} else {
@@ -487,6 +500,22 @@ public function setTimeout(int $timeout): Matomo
487500
return $this;
488501
}
489502

503+
/**
504+
* @return Client|null
505+
*/
506+
public function getClient(): ?Client
507+
{
508+
return $this->_client;
509+
}
510+
511+
/**
512+
* @param Client|null $client
513+
*/
514+
public function setClient(?Client $client): void
515+
{
516+
$this->_client = $client;
517+
}
518+
490519
/**
491520
* Reset all default variables.
492521
*/
@@ -527,28 +556,21 @@ private function _request(
527556
throw new InvalidRequestException('Could not parse URL!');
528557
}
529558

530-
// Build the request
531-
$req = Request::get($url)
532-
->followRedirects($this->_maxRedirects)
533-
->withTimeout($this->_timeout);
534-
535-
if ($this->_verifySsl) {
536-
$req->enableStrictSSL();
537-
} else {
538-
$req->disableStrictSSL();
539-
}
540-
541-
// Send the request
542559
try {
543-
$response = $req->send();
544-
} catch (NetworkErrorException $e) {
560+
$response = $this->_client->get($url, [
561+
'verify' => $this->_verifySsl,
562+
'allow_redirects' => [
563+
'max' => $this->_maxRedirects,
564+
],
565+
]);
566+
} catch (GuzzleException $e) {
545567
// Network error, e.g. timeout or connection refused
546568
throw new InvalidRequestException($e->getMessage(), $e->getCode(), $e);
547569
}
548570

549571
// Validate if the response was successful
550572
if ($response->getStatusCode() !== 200) {
551-
throw new InvalidRequestException($response->getRawBody(), $response->getStatusCode());
573+
throw new InvalidRequestException($response->getBody(), $response->getStatusCode());
552574
}
553575

554576
// Sometimes the response was unsuccessful, but the status code was 200
@@ -589,7 +611,7 @@ private function _parseUrl(string $method, array $params = []): string
589611
if (is_array($value)) {
590612
$params[$key] = urlencode(implode(',', $value));
591613
} else {
592-
$params[$key] = urlencode($value);
614+
$params[$key] = urlencode($value ?? '');
593615
}
594616
}
595617

@@ -664,7 +686,7 @@ private function _parseResponse(Response $response, string $overrideFormat = nul
664686
$format = $overrideFormat ?? $this->_format;
665687

666688
return match ($format) {
667-
self::FORMAT_JSON => json_decode($response, $this->_isJsonDecodeAssoc, 512,
689+
self::FORMAT_JSON => json_decode($response->getBody(), $this->_isJsonDecodeAssoc, 512,
668690
JSON_THROW_ON_ERROR),
669691
default => $response,
670692
};

0 commit comments

Comments
 (0)