From efb082f4ff742838b08d4a4f8ad2a4a4ba21e42f Mon Sep 17 00:00:00 2001
From: Grzegorz Rola <grola@seostudio.pl>
Date: Tue, 28 May 2019 22:01:01 +0200
Subject: [PATCH] Added timeout to options.

---
 src/Client/ApiClientOptions.php         |  5 +++++
 src/Client/ClientFactory.php            |  3 ++-
 src/Client/ClientImplementation.php     | 21 +++++++++++++--------
 tests/unit/Client/TestClientFactory.php |  4 ++++
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/Client/ApiClientOptions.php b/src/Client/ApiClientOptions.php
index 34d1683..f35582f 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 0cc058e..e79a75f 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 f948bec..3777c5a 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 553524d..6c57654 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;
     }
 
-- 
GitLab