diff --git a/src/Client/ApiClientOptions.php b/src/Client/ApiClientOptions.php index 34d16834be5e03f70048e8b6ee650693527585dc..f35582fcbd0afb0e658f37c1268cbaf643e64172 100644 --- a/src/Client/ApiClientOptions.php +++ b/src/Client/ApiClientOptions.php @@ -34,4 +34,9 @@ interface ApiClientOptions extends HttpClientOptions, SerializerOptions */ public function getApiClientClass(); + /** + * @return int + */ + public function getTimeout(); + } \ No newline at end of file diff --git a/src/Client/ClientFactory.php b/src/Client/ClientFactory.php index 0cc058e53f912982e8d6713a51cf4e5eec98e7ab..e79a75f5063e07a14135b05298de5723d9d1084b 100644 --- a/src/Client/ClientFactory.php +++ b/src/Client/ClientFactory.php @@ -24,7 +24,8 @@ class ClientFactory $serializerFactory->createSerializer($options), $options->getLogger(), $options->getApiUrl(), - $options->getDefaultRequestHeaders() + $options->getDefaultRequestHeaders(), + $options->getTimeout() ); if ($options->isCachedClient()) { diff --git a/src/Client/ClientImplementation.php b/src/Client/ClientImplementation.php index f948becc99b953e324c1e5e2c1268333c3e06096..3777c5a1fdfe6dd0a9d7a51073944fa5159c9003 100644 --- a/src/Client/ClientImplementation.php +++ b/src/Client/ClientImplementation.php @@ -16,8 +16,6 @@ class ClientImplementation implements Client, LoggerAwareInterface { const CLIENT_VERSION = '1.6.5'; - const DEFAULT_TIMEOUT = 10; - const LIBRARY_LOGIN_CONTEXT = 'wp-api-client'; /** @var HttpClient */ @@ -35,6 +33,9 @@ class ClientImplementation implements Client, LoggerAwareInterface /** @var array */ private $defaultRequestHeaders; + /** @var int */ + private $timeout; + /** * Client constructor. * @param HttpClient $client @@ -42,19 +43,22 @@ class ClientImplementation implements Client, LoggerAwareInterface * @param LoggerInterface $logger * @param string $apiUri * @param array $defaultRequestHeaders + * @param int $timeout */ public function __construct( HttpClient $client, Serializer $serializer, LoggerInterface $logger, $apiUri, - array $defaultRequestHeaders + array $defaultRequestHeaders, + $timeout = 10 ) { - $this->client = $client; - $this->serializer = $serializer; - $this->logger = $logger; - $this->apiUrl = $apiUri; + $this->client = $client; + $this->serializer = $serializer; + $this->logger = $logger; + $this->apiUrl = $apiUri; $this->defaultRequestHeaders = $defaultRequestHeaders; + $this->timeout = $timeout; } /** @@ -73,7 +77,8 @@ class ClientImplementation implements Client, LoggerAwareInterface $fullUrl = $this->prepareFullUrl($request), $method = $request->getMethod(), $body = $this->prepareRequestBody($request), - $headers = $this->prepareRequestHeaders($request), self::DEFAULT_TIMEOUT + $headers = $this->prepareRequestHeaders($request), + $this->timeout ); $this->logger->debug( diff --git a/tests/unit/Client/TestClientFactory.php b/tests/unit/Client/TestClientFactory.php index 553524d74164ba77435805ca91dbc51f0cbd4dcc..6c57654e1c2e92bdab5ec76f7d2b3db64a67515d 100644 --- a/tests/unit/Client/TestClientFactory.php +++ b/tests/unit/Client/TestClientFactory.php @@ -43,6 +43,10 @@ class TestClientFactory extends \PHPUnit\Framework\TestCase ->withAnyArgs() ->andReturn($isCachedClient); + $options->shouldReceive('getTimeout') + ->withAnyArgs() + ->andReturn(30); + return $options; }