Skip to content

Commit

Permalink
Tweaked handling of options (defaults, overrides)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Wolf committed Nov 3, 2015
1 parent f59a433 commit b05df98
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/Denner/Client/DennerClient.php
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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());
Expand Down

0 comments on commit b05df98

Please sign in to comment.