2
2
namespace SparkPost ;
3
3
4
4
class SparkPost {
5
-
5
+
6
6
private static $ config ;
7
7
private static $ defaults = array (
8
8
'host ' =>'api.sparkpost.com ' ,
9
9
'protocol ' =>'https ' ,
10
10
'port ' =>443 ,
11
11
'strictSSL ' =>true ,
12
12
'key ' =>'' ,
13
- 'version ' =>'v1 '
13
+ 'version ' =>'v1 '
14
14
);
15
-
15
+
16
16
/**
17
17
* Enforce that this object can't be instansiated
18
18
*/
19
19
private function __construct (){}
20
-
20
+
21
21
/**
22
22
* Allows the user to pass in values to override the defaults and set their API key
23
- * @param Array $configMap - Hashmap that contains config values for the SDK to connect to SparkPost
23
+ * @param String | Array $config - if a string it is an api key
24
+ * If an array, is should contain config values for the SDK to connect to SparkPost
24
25
* @throws \Exception
25
26
*/
26
- public static function setConfig (array $ configMap ) {
27
- //check for API key because its required
28
- if (isset ($ configMap ['key ' ])){
29
- $ key = trim ($ configMap ['key ' ]);
27
+ public static function setConfig ($ config ) {
28
+ // if the config map is a string we should assume that its an api key
29
+ if (gettype ($ config ) === 'string ' ) {
30
+ $ config = ['key ' =>$ config ];
31
+ }
32
+
33
+ //check for API key because its required
34
+ if (isset ($ config ['key ' ])){
35
+ $ key = trim ($ config ['key ' ]);
30
36
if (empty ($ key )){
31
37
throw new \Exception ('You must provide an API key ' );
32
38
}
33
39
} else {
34
40
throw new \Exception ('You must provide an API key ' );
35
41
}
36
42
self ::$ config = self ::$ defaults ;
37
- foreach ($ configMap as $ configOption => $ configValue ) {
43
+ foreach ($ config as $ configOption => $ configValue ) {
38
44
if (key_exists ($ configOption , self ::$ config )) {
39
45
self ::$ config [$ configOption ] = $ configValue ;
40
46
}
41
47
}
42
48
}
43
-
49
+
44
50
/**
45
51
* Retrieves the configuration that was previously setup by the user
46
52
* @throws \Exception
47
53
*/
48
54
public static function getConfig () {
49
- if (self ::$ config === null ) {
55
+ if (self ::$ config === null ) {
50
56
throw new \Exception ('No configuration has been provided ' );
51
57
}
52
58
return self ::$ config ;
53
59
}
54
-
60
+
55
61
public static function unsetConfig () {
56
62
self ::$ config = NULL ;
57
63
}
58
64
}
59
65
60
- ?>
66
+ ?>
0 commit comments