summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost
diff options
context:
space:
mode:
Diffstat (limited to 'lib/SparkPost')
-rw-r--r--lib/SparkPost/APIResource.php104
-rw-r--r--lib/SparkPost/APIResponseException.php3
-rw-r--r--lib/SparkPost/SparkPost.php61
-rw-r--r--lib/SparkPost/Transmission.php81
4 files changed, 110 insertions, 139 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index 0677b38..d02b91e 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -4,12 +4,12 @@ namespace SparkPost;
/**
* SDK interface for managing SparkPost API endpoints
*/
-class APIResource
-{
+class APIResource {
/**
- * Name of the API endpoint, mainly used for URL construction.
+ * name of the API endpoint, mainly used for URL construction.
* This is public to provide an interface
+ *
* @var string
*/
public $endpoint;
@@ -33,11 +33,9 @@ class APIResource
/**
* Initializes config and httpAdapter for use later.
- *
* @param $sparkpost \SparkPost\SparkPost provides api configuration information
*/
- public function __construct(SparkPost $sparkpost)
- {
+ public function __construct(SparkPost $sparkpost) {
$this->sparkpost = $sparkpost;
}
@@ -48,14 +46,13 @@ class APIResource
* @param string $mapKey a dot syntax path determining which value to set
* @param mixed $value value for the given path
*/
- protected function setMappedValue(&$model, $mapKey, $value)
- {
- // get mapping
- if (empty(static::$parameterMappings)) {
+ protected function setMappedValue(&$model, $mapKey, $value) {
+ //get mapping
+ if( empty(static::$parameterMappings) ) {
// if parameterMappings is empty we can assume that no wrapper is defined
// for the current endpoint and we will use the mapKey to define the mappings directly
$mapPath = $mapKey;
- } elseif (array_key_exists($mapKey, static::$parameterMappings)) {
+ }elseif(array_key_exists($mapKey, static::$parameterMappings)) {
// use only defined parameter mappings to construct $model
$mapPath = static::$parameterMappings[$mapKey];
} else {
@@ -64,52 +61,47 @@ class APIResource
$path = explode('.', $mapPath);
$temp = &$model;
- foreach ($path as $key) {
- if (!isset($temp[$key])) {
+ foreach( $path as $key ) {
+ if( !isset($temp[$key]) ){
$temp[$key] = null;
}
$temp = &$temp[$key];
}
$temp = $value;
+
}
/**
- * Maps values from the passed in model to those needed for the request
- *
+ * maps values from the passed in model to those needed for the request
* @param array $requestConfig the passed in model
* @param array $model the set of defaults
* @return array A model ready for the body of a request
*/
- protected function buildRequestModel(array $requestConfig, array $model = [])
- {
- foreach ($requestConfig as $key => $value) {
+ protected function buildRequestModel(Array $requestConfig, Array $model=[] ) {
+ foreach($requestConfig as $key => $value) {
$this->setMappedValue($model, $key, $value);
}
-
return $model;
}
/**
- * Posts to the api with a supplied body
- *
+ * posts to the api with a supplied body
* @param array $body post body for the request
* @return array Result of the request
*/
- public function create(array $body = [])
- {
- return $this->callResource('post', null, ['body' => $body]);
+ public function create(Array $body=[]) {
+ return $this->callResource( 'post', null, ['body'=>$body]);
}
/**
* Makes a put request to the api with a supplied body
- *
- * @param string $resourcePath string resource path of specific resource
+ * @param $resourcePath
* @param array $body Put body for the request
* @return array Result of the request
+ * @throws APIResponseException
*/
- public function update($resourcePath, array $body = [])
- {
- return $this->callResource('put', $resourcePath, ['body' => $body]);
+ public function update( $resourcePath, Array $body=[]) {
+ return $this->callResource( 'put', $resourcePath, ['body'=>$body]);
}
/**
@@ -119,9 +111,8 @@ class APIResource
* @param array $query (optional) query string parameters
* @return array Result of the request
*/
- public function get($resourcePath = null, array $query = [])
- {
- return $this->callResource('get', $resourcePath, ['query' => $query]);
+ public function get( $resourcePath=null, Array $query=[] ) {
+ return $this->callResource( 'get', $resourcePath, ['query'=>$query] );
}
/**
@@ -131,48 +122,44 @@ class APIResource
* @param array $query (optional) query string parameters
* @return array Result of the request
*/
- public function delete($resourcePath = null, array $query = [])
- {
- return $this->callResource('delete', $resourcePath, ['query' => $query]);
+ public function delete( $resourcePath=null, Array $query=[] ) {
+ return $this->callResource( 'delete', $resourcePath, ['query'=>$query] );
}
+
/**
- * Assembles a URL for a request
- *
+ * assembles a URL for a request
* @param string $resourcePath path after the initial endpoint
* @param array $options array with an optional value of query with values to build a querystring from.
* @return string the assembled URL
*/
- private function buildUrl($resourcePath, $options)
- {
+ private function buildUrl($resourcePath, $options) {
$url = "/{$this->endpoint}/";
- if (!is_null($resourcePath)) {
+ if (!is_null($resourcePath)){
$url .= $resourcePath;
}
- if (!empty($options['query'])) {
+ if( !empty($options['query'])) {
$queryString = http_build_query($options['query']);
- $url .= '?' . $queryString;
+ $url .= '?'.$queryString;
}
return $url;
}
+
/**
* Prepares a body for put and post requests
- *
* @param array $options array with an optional value of body with values to build a request body from.
* @return string|null A json encoded string or null if no body was provided
*/
- private function buildBody($options)
- {
+ private function buildBody($options) {
$body = null;
- if (!empty($options['body'])) {
+ if( !empty($options['body']) ) {
$model = static::$structure;
- $requestModel = $this->buildRequestModel($options['body'], $model);
+ $requestModel = $this->buildRequestModel( $options['body'], $model );
$body = json_encode($requestModel);
}
-
return $body;
}
@@ -190,14 +177,13 @@ class APIResource
* @return array Result set of action performed on resource
* @throws APIResponseException
*/
- private function callResource($action, $resourcePath = null, $options = [])
- {
+ private function callResource( $action, $resourcePath=null, $options=[] ) {
$action = strtoupper($action); // normalize
$url = $this->buildUrl($resourcePath, $options);
$body = $this->buildBody($options);
- // make request
+ //make request
try {
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
@@ -210,16 +196,20 @@ class APIResource
if ($statusCode === 404) {
throw new APIResponseException('The specified resource does not exist', 404);
}
- throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: ' .
- $statusCode);
+ throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
}
- } catch (\Exception $exception) { // Configuration Errors, and a catch all for other errors
- if ($exception instanceof APIResponseException) {
+ }
+
+ /*
+ * Configuration Errors, and a catch all for other errors
+ */
+ catch (\Exception $exception) {
+ if($exception instanceof APIResponseException) {
throw $exception;
}
- throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: ' .
- $exception->getMessage());
+ throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
}
}
+
}
diff --git a/lib/SparkPost/APIResponseException.php b/lib/SparkPost/APIResponseException.php
index e279e4d..cc0842c 100644
--- a/lib/SparkPost/APIResponseException.php
+++ b/lib/SparkPost/APIResponseException.php
@@ -2,8 +2,7 @@
namespace SparkPost;
-class APIResponseException extends \Exception
-{
+class APIResponseException extends \Exception {
}
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index 13d947c..46623c9 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -1,16 +1,14 @@
<?php
namespace SparkPost;
-
use Ivory\HttpAdapter\Configuration;
use Ivory\HttpAdapter\HttpAdapterInterface;
-class SparkPost
-{
+class SparkPost {
+
public $transmission;
/**
- * Connection config for making requests
- * @var array
+ * Connection config for making requests.
*/
private $config;
@@ -23,12 +21,12 @@ class SparkPost
* Default config values. Passed in values will override these.
*/
private static $apiDefaults = [
- 'host' => 'api.sparkpost.com',
- 'protocol' => 'https',
- 'port' => 443,
- 'strictSSL' => true,
- 'key' => '',
- 'version' => 'v1'
+ 'host'=>'api.sparkpost.com',
+ 'protocol'=>'https',
+ 'port'=>443,
+ 'strictSSL'=>true,
+ 'key'=>'',
+ 'version'=>'v1'
];
/**
@@ -37,12 +35,11 @@ class SparkPost
* Sets up instances of sub libraries.
*
* @param \Ivory\HttpAdapter\HttpAdapterInterface $httpAdapter - An adapter for making http requests
- * @param string | array $settingsConfig - Hashmap that contains config values
+ * @param String | array $settingsConfig - Hashmap that contains config values
* for the SDK to connect to SparkPost. If its a string we assume that
* its just they API Key.
*/
- public function __construct($httpAdapter, $settingsConfig)
- {
+ public function __construct($httpAdapter, $settingsConfig) {
//config needs to be setup before adapter because of default adapter settings
$this->setConfig($settingsConfig);
$this->setHttpAdapter($httpAdapter);
@@ -53,12 +50,10 @@ class SparkPost
/**
* Creates an unwrapped api interface for endpoints that aren't yet supported.
* The new resource is attached to this object as well as returned
- *
* @param string $endpoint
* @return APIResource - the unwrapped resource
*/
- public function setupUnwrapped($endpoint)
- {
+ public function setupUnwrapped ($endpoint) {
$this->{$endpoint} = new APIResource($this);
$this->{$endpoint}->endpoint = $endpoint;
@@ -68,11 +63,10 @@ class SparkPost
/**
* Merges passed in headers with default headers for http requests
*/
- public function getHttpHeaders()
- {
+ public function getHttpHeaders() {
$defaultOptions = [
- 'Authorization' => $this->config['key'],
- 'Content-Type' => 'application/json',
+ 'Authorization' => $this->config['key'],
+ 'Content-Type' => 'application/json',
];
return $defaultOptions;
@@ -80,55 +74,48 @@ class SparkPost
/**
* Helper function for getting the configuration for http requests
- *
* @param array $config
* @return Configuration
*/
- private function getHttpConfig(array $config)
- {
+ private 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'];
+ $baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version'];
$httpConfig->setBaseUri($baseUrl);
$httpConfig->setUserAgent('php-sparkpost/' . $composer['version']);
-
return $httpConfig;
}
+
/**
* Validates and sets up the httpAdapter
- *
* @param $httpAdapter \Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
* @throws \Exception
*/
- public function setHttpAdapter(HttpAdapterInterface $httpAdapter)
- {
+ public function setHttpAdapter(HttpAdapterInterface $httpAdapter) {
$this->httpAdapter = $httpAdapter;
$this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
}
/**
* Allows the user to pass in values to override the defaults and set their API key
- *
- * @param string | array $settingsConfig - Hashmap that contains config values
+ * @param String | array $settingsConfig - Hashmap that contains config values
* for the SDK to connect to SparkPost. If its a string we assume that
* its just they API Key.
* @throws \Exception
*/
- public function setConfig($settingsConfig)
- {
+ public function setConfig($settingsConfig) {
// if the config map is a string we should assume that its an api key
if (is_string($settingsConfig)) {
- $settingsConfig = ['key' => $settingsConfig];
+ $settingsConfig = ['key'=>$settingsConfig];
}
// Validate API key because its required
- if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))) {
+ if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
throw new \Exception('You must provide an API key');
}
@@ -136,7 +123,7 @@ class SparkPost
// set config, overriding defaults
foreach ($settingsConfig as $configOption => $configValue) {
- if (key_exists($configOption, $this->config)) {
+ if(key_exists($configOption, $this->config)) {
$this->config[$configOption] = $configValue;
}
}
diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php
index bbd1ca9..6beb087 100644
--- a/lib/SparkPost/Transmission.php
+++ b/lib/SparkPost/Transmission.php
@@ -1,11 +1,13 @@
<?php
namespace SparkPost;
+use Guzzle\Http\Client;
+use Guzzle\Http\Exception\ClientErrorResponseException;
/**
* SDK interface for managing transmissions
*/
-class Transmission extends APIResource
-{
+class Transmission extends APIResource {
+
public $endpoint = 'transmissions';
/**
@@ -13,24 +15,24 @@ class Transmission extends APIResource
* @var array
*/
protected static $parameterMappings = [
- 'campaign' => 'campaign_id',
- 'metadata' => 'metadata',
- 'substitutionData' => 'substitution_data',
- 'description' => 'description',
- 'returnPath' => 'return_path',
- 'replyTo' => 'content.reply_to',
- 'subject' => 'content.subject',
- 'from' => 'content.from',
- 'html' => 'content.html',
- 'text' => 'content.text',
- 'rfc822' => 'content.email_rfc822',
- 'customHeaders' => 'content.headers',
- 'recipients' => 'recipients',
- 'recipientList' => 'recipients.list_id',
- 'template' => 'content.template_id',
- 'trackOpens' => 'options.open_tracking',
- 'trackClicks' => 'options.click_tracking',
- 'useDraftTemplate' => 'use_draft_template'
+ 'campaign'=>'campaign_id',
+ 'metadata'=>'metadata',
+ 'substitutionData'=>'substitution_data',
+ 'description'=>'description',
+ 'returnPath'=>'return_path',
+ 'replyTo'=>'content.reply_to',
+ 'subject'=>'content.subject',
+ 'from'=>'content.from',
+ 'html'=>'content.html',
+ 'text'=>'content.text',
+ 'rfc822'=>'content.email_rfc822',
+ 'customHeaders'=>'content.headers',
+ 'recipients'=>'recipients',
+ 'recipientList'=>'recipients.list_id',
+ 'template'=>'content.template_id',
+ 'trackOpens'=>'options.open_tracking',
+ 'trackClicks'=>'options.click_tracking',
+ 'useDraftTemplate'=>'use_draft_template'
];
/**
@@ -38,13 +40,13 @@ class Transmission extends APIResource
* @var array
*/
protected static $structure = [
- 'return_path' => 'default@sparkpostmail.com',
- 'content' => [
- 'html' => null,
- 'text' => null,
- 'email_rfc822' => null
+ 'return_path'=>'default@sparkpostmail.com',
+ 'content'=>[
+ 'html'=>null,
+ 'text'=>null,
+ 'email_rfc822'=>null
],
- 'use_draft_template' => false
+ 'use_draft_template'=>false
];
/**
@@ -72,11 +74,10 @@ class Transmission extends APIResource
* 'useDraftTemplate': boolean
*
* @param array $transmissionConfig
- * @return array API response represented as key-value pairs
+ * @return array API repsonse represented as key-value pairs
*/
- public function send(array $transmissionConfig)
- {
- return $this->create($transmissionConfig);
+ public function send( $transmissionConfig ) {
+ return $this->create( $transmissionConfig );
}
/**
@@ -87,28 +88,22 @@ class Transmission extends APIResource
* @param null|string $templateID
* @return array result Set of transmissions
*/
- public function all($campaignID = null, $templateID = null)
- {
+ public function all( $campaignID=null, $templateID=null ) {
$options = [];
- if ($campaignID !== null) {
- $options['campaign_id'] = $campaignID;
- }
- if ($templateID !== null) {
- $options['template_id'] = $templateID;
- }
+ if( $campaignID !== NULL ) $options['campaign_id'] = $campaignID;
+ if( $templateID !== NULL ) $options['template_id'] = $templateID;
- return $this->get(null, $options);
+ return $this->get( null, $options );
}
/**
* Method for retrieving information about a single transmission
- * Wrapper method for a cleaner interface
- *
+ * Wrapper method for a cleaner interface
+ *
* @param string $transmissionID Identifier of the transmission to be found
* @return array result Single transmission represented in key-value pairs
*/
- public function find($transmissionID)
- {
+ public function find($transmissionID) {
return $this->get($transmissionID);
}
}