From 4b8a1370698581fece4d5ec188851031e41dcad0 Mon Sep 17 00:00:00 2001 From: beardyman Date: Sun, 4 Oct 2015 19:42:14 -0400 Subject: [PATCH 1/3] added check to see if an api key is passed in instead of a config object --- lib/SparkPost/SparkPost.php | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 755df46..a9035f9 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -2,7 +2,7 @@ namespace SparkPost; class SparkPost { - + private static $config; private static $defaults = array( 'host'=>'api.sparkpost.com', @@ -10,23 +10,29 @@ class SparkPost { '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 + * @param String | Array $config - if a string it is an api key + * If an array, is should contain config values for the SDK to connect to SparkPost * @throws \Exception */ - public static function setConfig(array $configMap) { - //check for API key because its required - if (isset($configMap['key'])){ - $key = trim($configMap['key']); + public static function setConfig($config) { + // if the config map is a string we should assume that its an api key + if (gettype($config) === 'string') { + $config = ['key'=>$config]; + } + + //check for API key because its required + if (isset($config['key'])){ + $key = trim($config['key']); if(empty($key)){ throw new \Exception('You must provide an API key'); } @@ -34,27 +40,27 @@ public static function setConfig(array $configMap) { throw new \Exception('You must provide an API key'); } self::$config = self::$defaults; - foreach ($configMap as $configOption => $configValue) { + foreach ($config as $configOption => $configValue) { if(key_exists($configOption, self::$config)) { self::$config[$configOption] = $configValue; } } } - + /** * 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; } } -?> \ No newline at end of file +?> From 0be23bb4dd085692df0942dcf0961fbd1857bd60 Mon Sep 17 00:00:00 2001 From: beardyman Date: Sat, 10 Oct 2015 11:02:39 -0400 Subject: [PATCH 2/3] Added ability to pass in api key as a config --- lib/SparkPost/SparkPost.php | 16 +++++++++------- test/unit/SparkPostTest.php | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index d4449b8..2a554dd 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -40,11 +40,6 @@ class SparkPost { * its just they API Key. */ public function __construct($httpAdapter, $settingsConfig) { - // if the config map is a string we should assume that its an api key - if (gettype($settingsConfig) === 'string') { - $settingsConfig = ['key'=>$settingsConfig]; - } - //config needs to be setup before adapter because of default adapter settings $this->setConfig($settingsConfig); $this->setHttpAdapter($httpAdapter); @@ -114,10 +109,17 @@ public function setHttpAdapter($httpAdapter) { /** * Allows the user to pass in values to override the defaults and set their API key - * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost + * @param String | Array $settingsConfig - Hashmap that contains config values + * for the SDK to connect to SparkPost. If its a string we assume that + * its just they API Key. * @throws \Exception */ - public function setConfig(Array $settingsConfig) { + public function setConfig($settingsConfig) { + // if the config map is a string we should assume that its an api key + if (gettype($settingsConfig) === 'string') { + $settingsConfig = ['key'=>$settingsConfig]; + } + // Validate API key because its required if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){ throw new \Exception('You must provide an API key'); diff --git a/test/unit/SparkPostTest.php b/test/unit/SparkPostTest.php index 101784a..b1b38fe 100644 --- a/test/unit/SparkPostTest.php +++ b/test/unit/SparkPostTest.php @@ -51,6 +51,12 @@ public function testSetBadHTTPAdapter() { $this->resource->setHttpAdapter(new \stdClass()); } + public function testSetConfigStringKey() { + $this->resource->setConfig('a key'); + $config = self::$utils->getProperty($this->resource, 'config'); + $this->assertEquals('a key', $config['key']); + } + /** * @expectedException Exception * @expectedExceptionMessageRegExp /API key/ From 47d840576d7e4c29fc4ae359f5f1adff6ffbc751 Mon Sep 17 00:00:00 2001 From: beardyman Date: Wed, 14 Oct 2015 10:33:36 -0400 Subject: [PATCH 3/3] Changed gettype() to is_string() --- lib/SparkPost/SparkPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 2a554dd..3c75803 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -116,7 +116,7 @@ public function setHttpAdapter($httpAdapter) { */ public function setConfig($settingsConfig) { // if the config map is a string we should assume that its an api key - if (gettype($settingsConfig) === 'string') { + if (is_string($settingsConfig)) { $settingsConfig = ['key'=>$settingsConfig]; }