Skip to content

Commit c753d55

Browse files
committed
moved config setup and management back into the sparkpost object
1 parent a436c49 commit c753d55

File tree

3 files changed

+126
-107
lines changed

3 files changed

+126
-107
lines changed

examples/unwrapped/create_template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
$key = 'YOUR API KEY';
88
$httpAdapter = new Guzzle6HttpAdapter(new Client());
9-
$resource = new \SparkPost\APIResource($httpAdapter, ['key'=>$key]);
9+
$sparky = new SparkPost($httpAdapter, ['key'=>$key]);
1010

1111
try {
1212
// define the endpoint
13-
$resource->endpoint = 'templates';
13+
$sparky->setupUnwrapped('templates');
1414

1515
$templateConfig = [
1616
'name' => 'Summer Sale!',
@@ -21,7 +21,7 @@
2121
'html' => '<b>Check out these deals!</b>'
2222
]
2323
];
24-
$results = $resource->create($templateConfig);
24+
$results = $sparky->templates->create($templateConfig);
2525
echo 'Congrats you can use your SDK!';
2626
} catch (\Exception $exception) {
2727
echo $exception->getMessage();

lib/SparkPost/APIResource.php

Lines changed: 5 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22
namespace SparkPost;
3-
use Ivory\HttpAdapter\Configuration;
4-
use Ivory\HttpAdapter\HttpAdapterInterface;
53
use Ivory\HttpAdapter\HttpAdapterException;
4+
use SparkPost\SparkPost;
65

76
/**
87
* @desc SDK interface for managing SparkPost API endpoints
@@ -29,111 +28,14 @@ class APIResource {
2928
*/
3029
protected static $structure = [];
3130

32-
/**
33-
* @dec connection config for making requests.
34-
*/
35-
private $config;
36-
37-
/**
38-
* @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
39-
*/
40-
protected $httpAdapter;
41-
42-
/**
43-
* @desc Default config values. Passed in values will override these.
44-
*/
45-
private static $apiDefaults = [
46-
'host'=>'api.sparkpost.com',
47-
'protocol'=>'https',
48-
'port'=>443,
49-
'strictSSL'=>true,
50-
'key'=>'',
51-
'version'=>'v1'
52-
];
53-
5431
/**
5532
* @desc Initializes config and httpAdapter for use later.
56-
* @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
57-
* @param $config connection config for making requests.
33+
* @param $sparkpost SparkPost\SparkPost provides api configuration information
5834
*/
59-
public function __construct($httpAdapter, $config) {
60-
//config needs to be setup before adapter because of default adapter settings
61-
$this->setConfig($config);
62-
$this->setHttpAdapter($httpAdapter);
35+
public function __construct(SparkPost $sparkpost) {
36+
$this->sparkpost = $sparkpost;
6337
}
6438

65-
/**
66-
* @desc Merges passed in headers with default headers for http requests
67-
* @return Array - headers to be set on http requests
68-
*/
69-
private function getHttpHeaders(Array $headers = null) {
70-
$defaultOptions = [
71-
'Authorization' => $this->config['key'],
72-
'Content-Type' => 'application/json',
73-
];
74-
75-
// Merge passed in headers with defaults
76-
if (!is_null($headers)) {
77-
foreach ($headers as $header => $value) {
78-
$defaultOptions[$header] = $value;
79-
}
80-
}
81-
return $defaultOptions;
82-
}
83-
84-
85-
/**
86-
* @desc Helper function for getting the configuration for http requests
87-
* @return \Ivory\HttpAdapter\Configuration
88-
*/
89-
private function getHttpConfig($config) {
90-
// get composer.json to extract version number
91-
$composerFile = file_get_contents(dirname(__FILE__) . "/../../composer.json");
92-
$composer = json_decode($composerFile, true);
93-
94-
// create Configuration for http adapter
95-
$httpConfig = new Configuration();
96-
$baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version'];
97-
$httpConfig->setBaseUri($baseUrl);
98-
$httpConfig->setUserAgent('php-sparkpost/' . $composer['version']);
99-
return $httpConfig;
100-
}
101-
102-
/**
103-
* @desc Validates and sets up the httpAdapter
104-
* @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
105-
* @throws \Exception
106-
*/
107-
public function setHttpAdapter($httpAdapter) {
108-
if (!$httpAdapter instanceOf HttpAdapterInterface) {
109-
throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter');
110-
}
111-
112-
$this->httpAdapter = $httpAdapter;
113-
$this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
114-
}
115-
116-
/**
117-
* Allows the user to pass in values to override the defaults and set their API key
118-
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
119-
* @throws \Exception
120-
*/
121-
public function setConfig(Array $settingsConfig) {
122-
// Validate API key because its required
123-
if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
124-
throw new \Exception('You must provide an API key');
125-
}
126-
127-
$this->config = self::$apiDefaults;
128-
129-
// set config, overriding defaults
130-
foreach ($settingsConfig as $configOption => $configValue) {
131-
if(key_exists($configOption, $this->config)) {
132-
$this->config[$configOption] = $configValue;
133-
}
134-
}
135-
}
136-
13739
/**
13840
* @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
13941
*
@@ -282,7 +184,7 @@ private function callResource( $action, $resourcePath=null, $options=[] ) {
282184

283185
//make request
284186
try {
285-
$response = $this->httpAdapter->send($url, $action, $this->getHttpHeaders(), $body);
187+
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
286188
return json_decode($response->getBody()->getContents(), true);
287189
}
288190
/*

lib/SparkPost/SparkPost.php

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
<?php
22
namespace SparkPost;
3+
use Ivory\HttpAdapter\Configuration;
4+
use Ivory\HttpAdapter\HttpAdapterInterface;
5+
36
class SparkPost {
47

58
public $transmission;
69

10+
/**
11+
* @dec connection config for making requests.
12+
*/
13+
private $config;
14+
15+
/**
16+
* @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
17+
*/
18+
public $httpAdapter;
19+
20+
/**
21+
* @desc Default config values. Passed in values will override these.
22+
*/
23+
private static $apiDefaults = [
24+
'host'=>'api.sparkpost.com',
25+
'protocol'=>'https',
26+
'port'=>443,
27+
'strictSSL'=>true,
28+
'key'=>'',
29+
'version'=>'v1'
30+
];
31+
732
/**
833
* @desc sets up httpAdapter and config
934
*
@@ -13,7 +38,99 @@ class SparkPost {
1338
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
1439
*/
1540
public function __construct($httpAdapter, $settingsConfig) {
16-
$this->transmission = new Transmission($httpAdapter, $settingsConfig);
41+
//config needs to be setup before adapter because of default adapter settings
42+
$this->setConfig($settingsConfig);
43+
$this->setHttpAdapter($httpAdapter);
44+
45+
$this->transmission = new Transmission($this);
46+
}
47+
48+
49+
50+
/**
51+
* Creates an unwrapped api interface for endpoints that aren't yet supported.
52+
* The new resource is attached to this object as well as returned
53+
* @return SparkPost\APIResource - the unwrapped resource
54+
*/
55+
public function setupUnwrapped (string $endpoint) {
56+
$this->{$endpoint} = new APIResource($this);
57+
$this->{$endpoint}->endpoint = $endpoint;
58+
59+
return $this->{$endpoint};
60+
}
61+
62+
/**
63+
* @desc Merges passed in headers with default headers for http requests
64+
* @return Array - headers to be set on http requests
65+
*/
66+
public function getHttpHeaders(Array $headers = null) {
67+
$defaultOptions = [
68+
'Authorization' => $this->config['key'],
69+
'Content-Type' => 'application/json',
70+
];
71+
72+
// Merge passed in headers with defaults
73+
if (!is_null($headers)) {
74+
foreach ($headers as $header => $value) {
75+
$defaultOptions[$header] = $value;
76+
}
77+
}
78+
return $defaultOptions;
79+
}
80+
81+
82+
/**
83+
* @desc Helper function for getting the configuration for http requests
84+
* @return \Ivory\HttpAdapter\Configuration
85+
*/
86+
private function getHttpConfig($config) {
87+
// get composer.json to extract version number
88+
$composerFile = file_get_contents(dirname(__FILE__) . "/../../composer.json");
89+
$composer = json_decode($composerFile, true);
90+
91+
// create Configuration for http adapter
92+
$httpConfig = new Configuration();
93+
$baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version'];
94+
$httpConfig->setBaseUri($baseUrl);
95+
$httpConfig->setUserAgent('php-sparkpost/' . $composer['version']);
96+
return $httpConfig;
97+
}
98+
99+
100+
/**
101+
* @desc Validates and sets up the httpAdapter
102+
* @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
103+
* @throws \Exception
104+
*/
105+
public function setHttpAdapter($httpAdapter) {
106+
if (!$httpAdapter instanceOf HttpAdapterInterface) {
107+
throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter');
108+
}
109+
110+
$this->httpAdapter = $httpAdapter;
111+
$this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
112+
}
113+
114+
115+
/**
116+
* 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
118+
* @throws \Exception
119+
*/
120+
public function setConfig(Array $settingsConfig) {
121+
// Validate API key because its required
122+
if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
123+
throw new \Exception('You must provide an API key');
124+
}
125+
126+
$this->config = self::$apiDefaults;
127+
128+
// set config, overriding defaults
129+
foreach ($settingsConfig as $configOption => $configValue) {
130+
if(key_exists($configOption, $this->config)) {
131+
$this->config[$configOption] = $configValue;
132+
}
133+
}
17134
}
18135
}
19136

0 commit comments

Comments
 (0)