diff --git a/src/Braze.php b/src/Braze.php index 8782e19..ac8abdb 100644 --- a/src/Braze.php +++ b/src/Braze.php @@ -149,6 +149,16 @@ public function setValidation(bool $active, bool $strict = false): void $this->strictValidation = $active && $strict; } + public function setConnectionTimeout(?float $connectionTimeout): void + { + $this->client->setConnectionTimeout($connectionTimeout); + } + + public function setOverallTimeout(?float $overallTimeout): void + { + $this->client->setOverallTimeout($overallTimeout); + } + public function getDryRun(): bool { return $this->dryRun; @@ -163,4 +173,14 @@ public function getStrictValidation(): bool { return $this->strictValidation; } + + public function getConnectionTimeout(): ?float + { + return $this->client->getConnectionTimeout(); + } + + public function getOverallTimeout(): ?float + { + return $this->client->getOverallTimeout(); + } } diff --git a/src/Client.php b/src/Client.php index d122985..24a2c36 100644 --- a/src/Client.php +++ b/src/Client.php @@ -107,4 +107,24 @@ public function flush(): int return $resolvedResponseCount; } + + public function setConnectionTimeout(?float $connectionTimeout): void + { + $this->adapter->setConnectionTimeout($connectionTimeout); + } + + public function setOverallTimeout(?float $overallTimeout): void + { + $this->adapter->setOverallTimeout($overallTimeout); + } + + public function getConnectionTimeout(): ?float + { + return $this->adapter->getConnectionTimeout(); + } + + public function getOverallTimeout(): ?float + { + return $this->adapter->getOverallTimeout(); + } } diff --git a/src/ClientAdapter/ClientAdapterInterface.php b/src/ClientAdapter/ClientAdapterInterface.php index fbdf693..3019185 100644 --- a/src/ClientAdapter/ClientAdapterInterface.php +++ b/src/ClientAdapter/ClientAdapterInterface.php @@ -33,4 +33,12 @@ public function makeRequest(string $method, string $path, ?string $body = null): * @throws ClientException */ public function resolveResponse(mixed $httpResponse): ClientResolvedResponse; + + public function setConnectionTimeout(?float $connectionTimeout): void; + + public function setOverallTimeout(?float $overallTimeout): void; + + public function getConnectionTimeout(): ?float; + + public function getOverallTimeout(): ?float; } diff --git a/src/ClientAdapter/Psr18Adapter.php b/src/ClientAdapter/Psr18Adapter.php index ccc71a7..87b7112 100644 --- a/src/ClientAdapter/Psr18Adapter.php +++ b/src/ClientAdapter/Psr18Adapter.php @@ -29,6 +29,9 @@ class Psr18Adapter implements ClientAdapterInterface private ?UriInterface $baseURI = null; + private ?float $connectionTimeout = null; + private ?float $overallTimeout = null; + public function __construct(ClientInterface $client, UriFactoryInterface $uriFactory, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory) { $this->client = $client; @@ -92,4 +95,24 @@ public function resolveResponse($httpResponse): ClientResolvedResponse { return new ClientResolvedResponse($httpResponse->getStatusCode(), $httpResponse->getBody()); } + + public function setConnectionTimeout(?float $connectionTimeout): void + { + throw new \RuntimeException('Psr18Adapter does not support timeouts'); + } + + public function setOverallTimeout(?float $overallTimeout): void + { + throw new \RuntimeException('Psr18Adapter does not support timeouts'); + } + + public function getConnectionTimeout(): ?float + { + throw new \RuntimeException('Psr18Adapter does not support timeouts'); + } + + public function getOverallTimeout(): ?float + { + throw new \RuntimeException('Psr18Adapter does not support timeouts'); + } } diff --git a/src/ClientAdapter/SymfonyAdapter.php b/src/ClientAdapter/SymfonyAdapter.php index e66e2a6..644443b 100644 --- a/src/ClientAdapter/SymfonyAdapter.php +++ b/src/ClientAdapter/SymfonyAdapter.php @@ -21,6 +21,9 @@ class SymfonyAdapter implements ClientAdapterInterface private ?string $baseURI = null; + private ?float $connectionTimeout = null; + private ?float $overallTimeout = null; + public function __construct(HttpClientInterface $client) { $this->client = $client; @@ -42,6 +45,8 @@ public function makeRequest(string $method, string $path, ?string $body = null): 'base_uri' => $this->baseURI, 'headers' => $this->headers, 'body' => $body, + 'timeout' => $this->connectionTimeout, + 'max_duration' => $this->overallTimeout ?? 0, ]; try { @@ -66,4 +71,24 @@ public function resolveResponse($httpResponse): ClientResolvedResponse throw new ClientException($clientException->getMessage(), 0, $clientException); } } + + public function setConnectionTimeout(?float $connectionTimeout): void + { + $this->connectionTimeout = $connectionTimeout; + } + + public function setOverallTimeout(?float $overallTimeout): void + { + $this->overallTimeout = $overallTimeout; + } + + public function getConnectionTimeout(): ?float + { + return $this->connectionTimeout; + } + + public function getOverallTimeout(): ?float + { + return $this->overallTimeout; + } } diff --git a/tests/BrazeTest.php b/tests/BrazeTest.php index 092766f..9fdfd4e 100644 --- a/tests/BrazeTest.php +++ b/tests/BrazeTest.php @@ -48,4 +48,22 @@ public function testFlush(): void $resolvedResponseCount = $this->instance->flush(); $this->assertSame(1, $resolvedResponseCount); } + + public function testSetConnectionTimeout(): void + { + $timeout = 1.2; + + $this->instance->setConnectionTimeout($timeout); + + $this->assertSame($timeout, $this->instance->getConnectionTimeout()); + } + + public function testSetOverallTimeout(): void + { + $timeout = 2.0; + + $this->instance->setOverallTimeout($timeout); + + $this->assertSame($timeout, $this->instance->getOverallTimeout()); + } } diff --git a/tests/ClientAdapter/Psr18AdapterTest.php b/tests/ClientAdapter/Psr18AdapterTest.php index 7c30c5e..9b510a1 100644 --- a/tests/ClientAdapter/Psr18AdapterTest.php +++ b/tests/ClientAdapter/Psr18AdapterTest.php @@ -147,4 +147,35 @@ public function testResolveResponse(): void self::assertSame(200, $resolvedResponse->getStatusCode()); } + + public function testSetConnectionTimeout(): void + { + self::expectException(\RuntimeException::class); + + $this->psr18Adapter->setConnectionTimeout(1.0); + } + + + public function testGetConnectionTimeout(): void + { + self::expectException(\RuntimeException::class); + + $this->psr18Adapter->getConnectionTimeout(); + } + + + public function testSetOverallTimeout(): void + { + self::expectException(\RuntimeException::class); + + $this->psr18Adapter->setOverallTimeout(1.0); + } + + + public function testGetOverallTimeout(): void + { + self::expectException(\RuntimeException::class); + + $this->psr18Adapter->getOverallTimeout(); + } }