22
33namespace 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 ;
88use InvalidArgumentException ;
99use 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