22
33namespace VisualAppeal ;
44
5- use GuzzleHttp \Client ;
6- use GuzzleHttp \ Exception \ GuzzleException ;
5+ use GuzzleHttp \Psr7 \ Request ;
6+ use Http \ Adapter \ Guzzle7 \ Client as GuzzleAdapter ;
77use InvalidArgumentException ;
88use 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