blob: 5bcb8152492fe1222e024f9a31fddee57cf80cac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
<?php
namespace SparkPost;
use Ivory\HttpAdapter;
use Ivory\HttpAdapter\HttpAdapterInterface;
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'
);
/**
* Enforce that this object can't be instansiated
*/
private function __construct(){}
/**
* @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;
}
/**
* @desc Convenience function for setting the httpAdapter and config in one step
*
* @param Ivory\HttpAdapter $httpAdapter - an adapter for making http requests
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
*/
public static function configure($httpAdapter, $settingsConfig) {
//need to set the config prior to setting up the adapter because of default settings for the adapter
self::setConfig($settingsConfig);
self::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
* @throws \Exception
*/
public static function setConfig(Array $settingsConfig) {
// Validate API key because its required
if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
throw new \Exception('You must provide an API key');
}
self::$config = self::$defaults;
// set config, overriding defaults
foreach ($settingsConfig as $configOption => $configValue) {
if(key_exists($configOption, self::$config)) {
self::$config[$configOption] = $configValue;
}
}
}
/**
* @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::getConfig()['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;
}
/**
* Retrieves the configuration that was previously setup by the user
* @throws \Exception
*/
public static function getConfig() {
if (self::$config === null) {
throw new \Exception('No configuration has been provided');
}
return self::$config;
}
/**
* TODO: Docs
*/
public static function unsetConfig() {
self::$config = NULL;
}
/**
* TODO: Docs
*/
public static function setHttpAdapter($httpAdapter) {
if (!$httpAdapter instanceOf HttpAdapterInterface) {
throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter');
}
self::$httpAdapter = $httpAdapter;
self::$httpAdapter->setConfiguration(self::getHttpConfig(self::getConfig()));
}
/**
* Retrieves the Http Adapter that was previously setup by the user
* @throws \Exception
*/
public static function getHttpAdapter() {
if (self::$httpAdapter === null) {
throw new \Exception('No Http Adapter has been provided');
}
return self::$httpAdapter;
}
}
?>
|