diff options
author | beardyman <nornholdj@gmail.com> | 2015-09-15 20:43:30 -0400 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2015-09-15 20:43:30 -0400 |
commit | c0b9e8b6cb7b1d616941b657958126b551b22eef (patch) | |
tree | 98829e5b048cf9328f6211719f7e87886472dbe4 /lib/SparkPost/SparkPost.php | |
parent | aeea514f81c652257b64e606c771cc51249db49c (diff) | |
download | php-sparkpost-c0b9e8b6cb7b1d616941b657958126b551b22eef.zip php-sparkpost-c0b9e8b6cb7b1d616941b657958126b551b22eef.tar.gz php-sparkpost-c0b9e8b6cb7b1d616941b657958126b551b22eef.tar.bz2 |
Refactored to use http adapter instead of guzzle directly
Diffstat (limited to 'lib/SparkPost/SparkPost.php')
-rw-r--r-- | lib/SparkPost/SparkPost.php | 84 |
1 files changed, 74 insertions, 10 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 755df46..3b54ada 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -1,30 +1,34 @@ <?php namespace SparkPost; +use Ivory\HttpAdapter; +use Ivory\HttpAdapter\Configuration; class SparkPost { - + private static $config; + private static $httpAdapter; + private static $defaults = array( 'host'=>'api.sparkpost.com', 'protocol'=>'https', 'port'=>443, 'strictSSL'=>true, 'key'=>'', - 'version'=>'v1' + 'version'=>'v1' ); - + /** * Enforce that this object can't be instansiated */ private function __construct(){} - + /** * Allows the user to pass in values to override the defaults and set their API key * @param Array $configMap - Hashmap that contains config values for the SDK to connect to SparkPost * @throws \Exception */ - public static function setConfig(array $configMap) { - //check for API key because its required + public static function setConfig($httpAdapter, array $configMap) { + //check for API key because its required if (isset($configMap['key'])){ $key = trim($configMap['key']); if(empty($key)){ @@ -33,28 +37,88 @@ class SparkPost { } else { throw new \Exception('You must provide an API key'); } + // TODO: need to figure out how to enforce this + // if (!$httpAdapter instanceOf HttpAdapterInterface) { + // throw new \Exception('First Argument must be a valid Ivory\HttpAdapter'); + // } + self::$config = self::$defaults; + + self::$httpAdapter = $httpAdapter; + foreach ($configMap as $configOption => $configValue) { if(key_exists($configOption, self::$config)) { self::$config[$configOption] = $configValue; } } + + self::$httpAdapter->setConfiguration(self::getHttpConfig(self::$config)); + } + + + /** + * @desc Merges passed in headers with default headers for http requests + * @return Array - headers to be set on http requests + */ + public static function getHttpHeaders(Array $headers = null) { + $defaultOptions = [ + 'Authorization' => self::$config['key'], + 'Content-Type' => 'application/json', + ]; + + // Merge passed in headers with defaults + if (!is_null($headers)) { + foreach ($headers as $header => $value) { + $defaultOptions[$header] = $value; + } + } + return $defaultOptions; + } + + + /** + * @desc Helper function for getting the configuration for http requests + * @return \Ivory\HttpAdapter\Configuration + */ + // TODO: Need to figure out how to set strictSSL + private static function getHttpConfig($config) { + // get composer.json to extract version number + $composerFile = file_get_contents(dirname(__FILE__) . "/../../composer.json"); + $composer = json_decode($composerFile, true); + + // create Configuration for http adapter + $httpConfig = new Configuration(); + $baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version']; + $httpConfig->setBaseUri($baseUrl); + $httpConfig->setUserAgent('php-sparkpost/' . $composer['version']); + return $httpConfig; } - + /** * Retrieves the configuration that was previously setup by the user * @throws \Exception */ public static function getConfig() { - if (self::$config === null) { + if (self::$config === null) { throw new \Exception('No configuration has been provided'); } return self::$config; } - + public static function unsetConfig() { self::$config = NULL; } + + /** + * Retrieves the Http Adapter that was previously setup by the user + * @throws \Exception + */ + public static function getHttpAdapter() { + if (self::$config === null) { + throw new \Exception('No Http Adapter has been provided'); + } + return self::$httpAdapter; + } } -?>
\ No newline at end of file +?> |