diff --git a/src/Denner/Client/DennerClient.php b/src/Denner/Client/DennerClient.php index 48e1134..f9f8e41 100644 --- a/src/Denner/Client/DennerClient.php +++ b/src/Denner/Client/DennerClient.php @@ -19,12 +19,20 @@ abstract class DennerClient extends ServiceClient public static function factory($options = array()) { +// $requiredOptions = array(); +// +// foreach ($requiredOptions as $optionName) { +// if (!isset($options[$optionName]) || $options[$optionName] === '') { +// throw new Exception\InvalidArgumentException( +// sprintf('Missing required configuration option "%s"', $optionName) +// ); +// } +// } + + // These are applied if not otherwise specified $defaultOptions = array( 'base_url' => self::getDefaultServiceUrl(), 'defaults' => array( - // We're using our own error handler - // (this disabled the use of the internal HttpError subscriber) - 'exceptions' => false, // Float describing the number of seconds to wait while trying to connect to a server. // 0 was the default (wait indefinitely). 'connect_timeout' => 10, @@ -34,23 +42,6 @@ public static function factory($options = array()) ), ); - // If default options present, do a union with our $defaultOptions['defaults'] - if (array_key_exists('defaults', $options)) { - $options['defaults'] += $defaultOptions['defaults']; - } - -// $requiredOptions = array(); -// -// foreach ($requiredOptions as $optionName) { -// if (!isset($options[$optionName]) || $options[$optionName] === '') { -// throw new Exception\InvalidArgumentException( -// sprintf('Missing required configuration option "%s"', $optionName) -// ); -// } -// } - - $config = Collection::fromConfig($options, $defaultOptions); - $headers = array( 'Accept' => 'application/json', 'User-Agent' => 'denner-client/' . self::CLIENT_VERSION, @@ -64,11 +55,20 @@ public static function factory($options = array()) $headers['App-Key'] = $options['app_key']; } - $httpClient = new HttpClient($config->toArray()); - $httpClient->setDefaultOption( - 'headers', - array_merge($httpClient->getDefaultOption('headers') ?: array(), $headers) + // These are always applied + $overrideOptions = array( + 'defaults' => array( + // We're using our own error handler + // (this disables the use of the internal HttpError subscriber) + 'exceptions' => false, + 'headers' => $headers, + ), ); + + // Apply options + $config = array_replace_recursive($defaultOptions, $options, $overrideOptions); + + $httpClient = new HttpClient($config); $httpClient->getEmitter()->attach(new Subscriber\ErrorHandler()); $serviceDescriptionFile = __DIR__ . sprintf('/ServiceDescription/%s.php', self::getServiceDescriptionName());