diff --git a/src/Core/Client.php b/src/Core/Client.php index 1818c36f2d..a6a9931b41 100644 --- a/src/Core/Client.php +++ b/src/Core/Client.php @@ -203,7 +203,7 @@ public function getHttpClient($options = []) { if (is_null($this->httpClient)) { $clientOptions = $this->config->getClientOptions(); - if (count($clientOptions)) { + if (count($clientOptions) > 0) { $options = array_merge($clientOptions, $options); } $client = parent::getHttpClient($options); diff --git a/src/Core/Client/Adapter/ConfigAware.php b/src/Core/Client/Adapter/ConfigAware.php new file mode 100644 index 0000000000..fbe0af0828 --- /dev/null +++ b/src/Core/Client/Adapter/ConfigAware.php @@ -0,0 +1,15 @@ + + */ + +namespace Commercetools\Core\Client\Adapter; + +interface ConfigAware +{ + /** + * @param string $option + * @return mixed + */ + public function getConfig($option); +} diff --git a/src/Core/Client/Adapter/Guzzle5Adapter.php b/src/Core/Client/Adapter/Guzzle5Adapter.php index bff166f4fa..9c81c44efc 100644 --- a/src/Core/Client/Adapter/Guzzle5Adapter.php +++ b/src/Core/Client/Adapter/Guzzle5Adapter.php @@ -21,7 +21,7 @@ use Commercetools\Core\Error\ApiException; use Psr\Log\LogLevel; -class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware +class Guzzle5Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware { const DEFAULT_CONCURRENCY = 25; @@ -283,4 +283,12 @@ public static function getAdapterInfo() { return 'GuzzleHttp/' . Client::VERSION; } + + /** + * @inheritdoc + */ + public function getConfig($option) + { + return $this->client->getDefaultOption($option); + } } diff --git a/src/Core/Client/Adapter/Guzzle6Adapter.php b/src/Core/Client/Adapter/Guzzle6Adapter.php index 6934e4e6f8..c5a5e8d2f9 100644 --- a/src/Core/Client/Adapter/Guzzle6Adapter.php +++ b/src/Core/Client/Adapter/Guzzle6Adapter.php @@ -19,7 +19,7 @@ use Commercetools\Core\Error\ApiException; use Psr\Log\LogLevel; -class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware +class Guzzle6Adapter implements AdapterOptionInterface, CorrelationIdAware, TokenProviderAware, ConfigAware { const DEFAULT_CONCURRENCY = 25; /** @@ -221,4 +221,12 @@ public static function getAdapterInfo() { return 'GuzzleHttp/' . Client::VERSION; } + + /** + * @inheritdoc + */ + public function getConfig($option) + { + return $this->client->getConfig($option); + } } diff --git a/src/Core/Client/OAuth/Manager.php b/src/Core/Client/OAuth/Manager.php index 052b9d3d63..60c71215a4 100644 --- a/src/Core/Client/OAuth/Manager.php +++ b/src/Core/Client/OAuth/Manager.php @@ -251,6 +251,10 @@ protected function getBearerToken(array $data) public function getHttpClient($options = []) { if (is_null($this->httpClient)) { + $clientOptions = $this->config->getOAuthClientOptions(); + if (count($clientOptions) > 0) { + $options = array_merge($clientOptions, $options); + } $client = parent::getHttpClient($options); if ($this->getConfig()->getCorrelationIdProvider() instanceof CorrelationIdProvider && $client instanceof CorrelationIdAware diff --git a/src/Core/Config.php b/src/Core/Config.php index ea883a0d83..594575f71c 100644 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -174,6 +174,8 @@ class Config implements ContextAwareInterface protected $clientOptions = []; + protected $oauthClientOptions = []; + public function __construct() { $this->enableCorrelationId = Uuid::active(); @@ -682,6 +684,24 @@ public function setClientOptions(array $clientOptions) return $this; } + /** + * @return array + */ + public function getOAuthClientOptions() + { + return $this->oauthClientOptions; + } + + /** + * @param array $clientOptions + * @return Config + */ + public function setOAuthClientOptions(array $clientOptions) + { + $this->oauthClientOptions = $clientOptions; + return $this; + } + /** * @return string */ diff --git a/tests/unit/Client/OAuth/ManagerTest.php b/tests/unit/Client/OAuth/ManagerTest.php index 7b21edbc45..e56d720fa5 100644 --- a/tests/unit/Client/OAuth/ManagerTest.php +++ b/tests/unit/Client/OAuth/ManagerTest.php @@ -9,6 +9,7 @@ use Cache\Adapter\PHPArray\ArrayCachePool; use Cache\Adapter\Void\VoidCachePool; use Commercetools\Core\Cache\CacheAdapterFactory; +use Commercetools\Core\Client\Adapter\ConfigAware; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; @@ -284,4 +285,19 @@ public function testOAuthUrl() $this->assertSame($this->getConfig()->getOauthUrl(), $output); } + + public function testSetClientOptions() + { + $config = Config::of()->setClientId('')->setClientSecret('')->setProject(''); + $manager = new Manager($config); + + $this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient()); + $this->assertTrue($manager->getHttpClient()->getConfig('verify')); + + $config = Config::of()->setClientId('')->setClientSecret('')->setProject('')->setOAuthClientOptions(['verify' => false]); + $manager = new Manager($config); + + $this->assertInstanceOf(ConfigAware::class, $manager->getHttpClient()); + $this->assertFalse($manager->getHttpClient()->getConfig('verify')); + } } diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index fba03213db..9b25914523 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -6,7 +6,9 @@ namespace Commercetools\Core; +use Commercetools\Core\Client\Adapter\AdapterFactory; use Commercetools\Core\Client\Adapter\AdapterOptionInterface; +use Commercetools\Core\Client\Adapter\ConfigAware; use Commercetools\Core\Client\OAuth\Manager; use Commercetools\Core\Error\BadGatewayException; use Commercetools\Core\Error\ConcurrentModificationException; @@ -805,4 +807,19 @@ public function testHtmlBody() $this->assertSame('Length Required', $response->getMessage()); } + + public function testSetClientOptions() + { + $client = Client::ofConfig( + Config::of()->setClientId('')->setClientSecret('')->setProject('') + ); + $this->assertInstanceOf(ConfigAware::class, $client->getHttpClient()); + $this->assertTrue($client->getHttpClient()->getConfig('verify')); + + $client = Client::ofConfig( + Config::of()->setClientId('')->setClientSecret('')->setProject('')->setClientOptions(['verify' => false]) + ); + $this->assertInstanceOf(ConfigAware::class, $client->getHttpClient()); + $this->assertFalse($client->getHttpClient()->getConfig('verify')); + } }