Skip to content

Commit 0be23bb

Browse files
committed
Added ability to pass in api key as a config
1 parent 20301cd commit 0be23bb

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/SparkPost/SparkPost.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ class SparkPost {
4040
* its just they API Key.
4141
*/
4242
public function __construct($httpAdapter, $settingsConfig) {
43-
// if the config map is a string we should assume that its an api key
44-
if (gettype($settingsConfig) === 'string') {
45-
$settingsConfig = ['key'=>$settingsConfig];
46-
}
47-
4843
//config needs to be setup before adapter because of default adapter settings
4944
$this->setConfig($settingsConfig);
5045
$this->setHttpAdapter($httpAdapter);
@@ -114,10 +109,17 @@ public function setHttpAdapter($httpAdapter) {
114109

115110
/**
116111
* Allows the user to pass in values to override the defaults and set their API key
117-
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
112+
* @param String | Array $settingsConfig - Hashmap that contains config values
113+
* for the SDK to connect to SparkPost. If its a string we assume that
114+
* its just they API Key.
118115
* @throws \Exception
119116
*/
120-
public function setConfig(Array $settingsConfig) {
117+
public function setConfig($settingsConfig) {
118+
// if the config map is a string we should assume that its an api key
119+
if (gettype($settingsConfig) === 'string') {
120+
$settingsConfig = ['key'=>$settingsConfig];
121+
}
122+
121123
// Validate API key because its required
122124
if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
123125
throw new \Exception('You must provide an API key');

test/unit/SparkPostTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function testSetBadHTTPAdapter() {
5151
$this->resource->setHttpAdapter(new \stdClass());
5252
}
5353

54+
public function testSetConfigStringKey() {
55+
$this->resource->setConfig('a key');
56+
$config = self::$utils->getProperty($this->resource, 'config');
57+
$this->assertEquals('a key', $config['key']);
58+
}
59+
5460
/**
5561
* @expectedException Exception
5662
* @expectedExceptionMessageRegExp /API key/

0 commit comments

Comments
 (0)