summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost
diff options
context:
space:
mode:
authorbeardyman <nornholdj@gmail.com>2015-09-16 23:25:03 -0400
committerbeardyman <nornholdj@gmail.com>2015-09-16 23:25:03 -0400
commitb51ce2d0a8fad2577c164e1ab1c8382b1ae23fae (patch)
tree0cfd23aff75b957b0beb7e88e4c0a544c5c107b5 /lib/SparkPost
parent0c4b276fd3c51e938474071fcde5b294a7651256 (diff)
downloadphp-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.zip
php-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.tar.gz
php-sparkpost-b51ce2d0a8fad2577c164e1ab1c8382b1ae23fae.tar.bz2
finished code updates/refactoring updated examples and began refactoring tests
Diffstat (limited to 'lib/SparkPost')
-rw-r--r--lib/SparkPost/APIResource.php43
-rw-r--r--lib/SparkPost/SparkPost.php96
2 files changed, 78 insertions, 61 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index be3d788..260d5e2 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -58,7 +58,10 @@ class APIResource {
}
- protected static function buildRequestModel( $requestConfig, $model=array() ) {
+ /**
+ * TODO: Docs
+ */
+ protected static function buildRequestModel(Array $requestConfig, Array $model=[] ) {
foreach($requestConfig as $key=>$value) {
self::setMappedValue($model, $key, $value);
}
@@ -69,14 +72,14 @@ class APIResource {
* TODO: Docs
*/
public static function create(Array $body=[]) {
- return self::callResource( 'post', '/', ['body'=>$options]);
+ return self::callResource( 'post', null, ['body'=>$body]);
}
/**
* TODO: Docs
*/
- public static function update(String $resourcePath, Array $body=[]) {
- return self::callResource( 'post', $resourcePath, ['body'=>$options]);
+ public static function update( $resourcePath, Array $body=[]) {
+ return self::callResource( 'put', $resourcePath, ['body'=>$body]);
}
/**
@@ -86,8 +89,8 @@ class APIResource {
* @param array $options (optional) query string parameters
* @return array Result set of transmissions found
*/
- public static function get(String $resourcePath=null, Array $options=[] ) {
- return self::callResource( 'get', $resourcePath, $options );
+ public static function get( $resourcePath=null, Array $query=[] ) {
+ return self::callResource( 'get', $resourcePath, ['query'=>$query] );
}
/**
@@ -97,8 +100,8 @@ class APIResource {
* @param array $options (optional) query string parameters
* @return array Result set of transmissions found
*/
- public static function delete(String $resourcePath=null, Array $options=[] ) {
- return self::callResource( 'delete', $resourcePath, $options );
+ public static function delete( $resourcePath=null, Array $query=[] ) {
+ return self::callResource( 'delete', $resourcePath, ['query'=>$query] );
}
/**
@@ -121,10 +124,10 @@ class APIResource {
throw new \Exception('Invalid resource action');
}
- $url = '/'.static::$endpoint;
+ $url = '/' . static::$endpoint . '/';
$body = null;
if (!is_null($resourcePath)){
- $url .= $url.$resourcePath;
+ $url .= $resourcePath;
}
// untested:
@@ -135,7 +138,7 @@ class APIResource {
if( !empty($options['body']) ) {
$model = static::$structure;
- $requestModel = self::buildRequestModel( $requestConfig, $options['body'] );
+ $requestModel = self::buildRequestModel( $options['body'], $model );
$body = json_encode($requestModel);
}
@@ -147,15 +150,15 @@ class APIResource {
}
/*
* Handles 4XX responses
- */
- // catch (HttpAdapterException $exception) {
- // $response = $exception->getResponse();
- // $statusCode = $response->getStatusCode();
- // if($statusCode === 404) {
- // throw new \Exception("The specified resource does not exist", 404);
- // }
- // throw new \Exception("Received bad response from ".ucfirst(static::$endpoint)." API: ". $statusCode );
- // }
+ */
+ catch (HttpAdapterException $exception) {
+ $response = $exception->getBody();
+ $statusCode = $response->getStatusCode();
+ if($statusCode === 404) {
+ throw new \Exception("The specified resource does not exist", 404);
+ }
+ throw new \Exception("Received bad response from ".ucfirst(static::$endpoint)." API: ". $statusCode );
+ }
/*
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
*/
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index 3b54ada..5bcb815 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -1,6 +1,7 @@
<?php
namespace SparkPost;
use Ivory\HttpAdapter;
+use Ivory\HttpAdapter\HttpAdapterInterface;
use Ivory\HttpAdapter\Configuration;
class SparkPost {
@@ -22,47 +23,64 @@ class SparkPost {
*/
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 $configMap - Hashmap that contains config values for the SDK to connect to SparkPost
+ * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
* @throws \Exception
*/
- public static function setConfig($httpAdapter, array $configMap) {
- //check for API key because its required
- if (isset($configMap['key'])){
- $key = trim($configMap['key']);
- if(empty($key)){
- throw new \Exception('You must provide an API key');
- }
- } else {
+ 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');
- }
- // TODO: need to figure out how to enforce this
- // if (!$httpAdapter instanceOf HttpAdapterInterface) {
- // throw new \Exception('First Argument must be a valid Ivory\HttpAdapter');
- // }
+ }
self::$config = self::$defaults;
- self::$httpAdapter = $httpAdapter;
-
- foreach ($configMap as $configOption => $configValue) {
+ // set config, overriding defaults
+ foreach ($settingsConfig as $configOption => $configValue) {
if(key_exists($configOption, self::$config)) {
self::$config[$configOption] = $configValue;
}
}
-
- self::$httpAdapter->setConfiguration(self::getHttpConfig(self::$config));
}
-
/**
* @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::$config['key'],
+ 'Authorization' => self::getConfig()['key'],
'Content-Type' => 'application/json',
];
@@ -75,25 +93,6 @@ class SparkPost {
return $defaultOptions;
}
-
- /**
- * @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;
- }
-
/**
* Retrieves the configuration that was previously setup by the user
* @throws \Exception
@@ -105,16 +104,31 @@ class SparkPost {
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::$config === null) {
+ if (self::$httpAdapter === null) {
throw new \Exception('No Http Adapter has been provided');
}
return self::$httpAdapter;